use crate::DbPool;
use crate::api::ClientVersion;
use crate::db;
use crate::providers::{Provider, report_provider_error, report_provider_error_background};
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::time::parse_time_to_seconds;
use crate::videos::ServerOptions;
use crate::videos::{self, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use futures::future::join_all;
use htmlentity::entity::{ICodedDataTrait, decode};
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use wreq::Client;
use wreq::Version;
use wreq_util::Emulation;

pub const CHANNEL_METADATA: crate::providers::ProviderChannelMetadata =
    crate::providers::ProviderChannelMetadata {
        group_id: "studio-network",
        tags: &["regional", "amateur", "mixed"],
    };

error_chain! {
    foreign_links {
        Io(std::io::Error);
        HttpRequest(wreq::Error);
        JsonError(serde_json::Error);
    }
}

#[derive(Debug, Deserialize, Serialize)]
struct PerverzijaDbEntry {
    url_string: String,
    tags_strings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct PerverzijaProvider {
    url: String,
    tag_map: Arc<RwLock<HashMap<String, String>>>,
}
impl PerverzijaProvider {
    pub fn new() -> Self {
        PerverzijaProvider {
            url: "https://tube.perverzija.com/".to_string(),
            tag_map: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    fn build_channel(&self, clientversion: ClientVersion) -> Channel {
        let _ = clientversion;

        Channel {
            id: "perverzija".to_string(),
            name: "Perverzija".to_string(),
            description: "Free videos from Perverzija".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=tube.perverzija.com"
                .to_string(),
            status: "active".to_string(),
            categories: vec![],
            options: vec![ChannelOption {
                id: "featured".to_string(),
                title: "Featured".to_string(),
                description: "Filter Featured Videos.".to_string(),
                systemImage: "star".to_string(),
                colorName: "red".to_string(),
                options: vec![
                    FilterOption {
                        id: "all".to_string(),
                        title: "No".to_string(),
                    },
                    FilterOption {
                        id: "featured".to_string(),
                        title: "Yes".to_string(),
                    },
                ],
                multiSelect: false,
            }],
            nsfw: true,
            cacheDuration: None,
            ytdlpCommand: Some("yt-dlp --impersonate chrome-120".to_string())
        }
    }

    fn extract_between<'a>(haystack: &'a str, start: &str, end: &str) -> Option<&'a str> {
        let rest = haystack.split(start).nth(1)?;
        Some(rest.split(end).next().unwrap_or_default())
    }

    fn extract_iframe_src(haystack: &str) -> String {
        Self::extract_between(haystack, "iframe src=\"", "\"")
            .or_else(|| Self::extract_between(haystack, "iframe src=&quot;", "&quot;"))
            .unwrap_or_default()
            .to_string()
    }

    fn extract_thumb(haystack: &str) -> String {
        let img_segment = haystack.split("<img").nth(1).unwrap_or_default();
        let mut thumb = Self::extract_between(img_segment, "data-original=\"", "\"")
            .or_else(|| Self::extract_between(img_segment, "data-src=\"", "\""))
            .or_else(|| Self::extract_between(img_segment, "src=\"", "\""))
            .unwrap_or_default()
            .to_string();

        if thumb.starts_with("data:image") {
            thumb.clear();
        } else if thumb.starts_with("//") {
            thumb = format!("https:{thumb}");
        }

        thumb
    }

    fn extract_title(haystack: &str) -> String {
        let mut title = Self::extract_between(haystack, "<h4 class='gv-title'>", "</h4>")
            .or_else(|| Self::extract_between(haystack, "<h4 class=\"gv-title\">", "</h4>"))
            .or_else(|| Self::extract_between(haystack, " title='", "'"))
            .or_else(|| Self::extract_between(haystack, " title=\"", "\""))
            .unwrap_or_default()
            .to_string();
        title = decode(title.as_bytes()).to_string().unwrap_or(title);

        if title.contains('<') && title.contains('>') {
            let mut plain = String::new();
            let mut in_tag = false;
            for c in title.chars() {
                match c {
                    '<' => in_tag = true,
                    '>' => in_tag = false,
                    _ if !in_tag => plain.push(c),
                    _ => {}
                }
            }
            let normalized = plain.split_whitespace().collect::<Vec<&str>>().join(" ");
            if !normalized.is_empty() {
                title = normalized;
            }
        } else {
            title = title.split_whitespace().collect::<Vec<&str>>().join(" ");
        }

        title.trim().to_string()
    }

    fn clip_at_first<'a>(haystack: &'a str, end_markers: &[&str]) -> &'a str {
        let mut end = haystack.len();
        for marker in end_markers {
            if let Some(index) = haystack.find(marker) {
                end = end.min(index);
            }
        }
        &haystack[..end]
    }

    fn listing_item_scope(haystack: &str) -> &str {
        Self::clip_at_first(haystack, &["</article>", "</li>", "<article ", "video-item post"])
    }

    fn detail_meta_section<'a>(text: &'a str, label: &str) -> &'a str {
        let section = text
            .split(label)
            .nth(1)
            .unwrap_or_default();
        Self::clip_at_first(
            section,
            &["</div>", "</p>", "<strong>", "<div class=\"related", "<section", "<aside"],
        )
    }

    fn push_unique(tags: &mut Vec<String>, value: String) {
        let normalized = value.trim();
        if normalized.is_empty() {
            return;
        }
        if !tags
            .iter()
            .any(|existing| existing.eq_ignore_ascii_case(normalized))
        {
            tags.push(normalized.to_string());
        }
    }

    fn parse_href_values(section: &str) -> Vec<String> {
        section
            .split("<a href=\"")
            .skip(1)
            .filter_map(|part| part.split('"').next())
            .map(|value| value.to_string())
            .collect()
    }

    fn normalize_key(value: &str) -> String {
        value
            .trim()
            .to_ascii_lowercase()
            .replace(['_', '-'], " ")
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn humanize_slug(value: &str) -> String {
        value
            .trim_matches('/')
            .replace('-', " ")
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn insert_tag_mapping(&self, kind: &str, slug: &str, title: Option<&str>) {
        let slug = slug.trim().trim_matches('/');
        if slug.is_empty() {
            return;
        }
        let path = format!("{kind}/{slug}");
        if let Ok(mut map) = self.tag_map.write() {
            map.insert(Self::normalize_key(slug), path.clone());
            let normalized_title = Self::normalize_key(title.unwrap_or(slug));
            if !normalized_title.is_empty() {
                map.insert(normalized_title, path);
            }
        }
    }

    fn resolve_query_path(&self, query: &str) -> Option<String> {
        let trimmed = query.trim().trim_start_matches('@');
        if let Some((kind, raw_value)) = trimmed.split_once(':') {
            let kind = kind.trim().to_ascii_lowercase();
            let value = raw_value.trim().trim_matches('/').replace(' ', "-");
            if !value.is_empty() && matches!(kind.as_str(), "studio" | "stars" | "tag" | "genre")
            {
                return Some(format!("{kind}/{value}"));
            }
        }
        let normalized = Self::normalize_key(trimmed);
        if normalized.is_empty() {
            return None;
        }
        self.tag_map.read().ok()?.get(&normalized).cloned()
    }

    async fn get(
        &self,
        cache: VideoCache,
        pool: DbPool,
        page: u8,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let featured = options.featured.clone().unwrap_or("".to_string());
        let mut prefix_uri = "".to_string();
        if featured == "featured" {
            prefix_uri = "featured-scenes/".to_string();
        }
        let mut url_str = format!("{}{}page/{}/", self.url, prefix_uri, page);
        if page == 1 {
            url_str = format!("{}{}", self.url, prefix_uri);
        }

        let old_items = match cache.get(&url_str) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    //println!("Cache hit for URL: {}", url_str);
                    return Ok(items.clone());
                } else {
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };

        let mut requester =
            crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
        let text = match requester.get(&url_str, Some(Version::HTTP_2)).await {
            Ok(text) => text,
            Err(e) => {
                report_provider_error(
                    "perverzija",
                    "get.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };
        let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone(), pool);
        if !video_items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.clone(), video_items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(video_items)
    }
    async fn query(
        &self,
        cache: VideoCache,
        pool: DbPool,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let mut query_parse = true;
        let search_string = query.replace(" ", "+");
        let mut url_str = format!("{}page/{}/?s={}", self.url, page, search_string);
        if page == 1 {
            url_str = format!("{}?s={}", self.url, search_string);
        }

        if let Some(path) = self.resolve_query_path(query) {
            url_str = format!("{}/{}/page/{}/", self.url.trim_end_matches('/'), path, page);
            query_parse = false;
        }
        url_str = url_str.replace("page/1/", "");
        // Check our Video Cache. If the result is younger than 1 hour, we return it.
        let old_items = match cache.get(&url_str) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    return Ok(items.clone());
                } else {
                    let _ = cache.check().await;
                    return Ok(items.clone());
                }
            }
            None => {
                vec![]
            }
        };

        let mut requester =
            crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
        let text = match requester.get(&url_str, Some(Version::HTTP_2)).await {
            Ok(text) => text,
            Err(e) => {
                report_provider_error(
                    "perverzija",
                    "query.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };
        let video_items: Vec<VideoItem> = match query_parse {
            true => {
                self.get_video_items_from_html_query(text.clone(), pool)
                    .await
            }
            false => self.get_video_items_from_html(text.clone(), pool),
        };
        if !video_items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.clone(), video_items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(video_items)
    }

    fn get_video_items_from_html(&self, html: String, pool: DbPool) -> Vec<VideoItem> {
        if html.is_empty() {
            report_provider_error_background(
                "perverzija",
                "get_video_items_from_html.empty_html",
                "empty html response",
            );
            return vec![];
        }
        let mut items: Vec<VideoItem> = Vec::new();
        let video_listing_content = html.split("video-listing-content").nth(1).unwrap_or(&html);
        let raw_videos: Vec<&str> = video_listing_content
            .split("video-item post")
            .skip(1)
            .collect();

        if raw_videos.is_empty() {
            report_provider_error_background(
                "perverzija",
                "get_video_items_from_html.no_segments",
                &format!("html_len={}", html.len()),
            );
            return vec![];
        }

        for raw_video_segment in raw_videos {
            let video_segment = Self::listing_item_scope(raw_video_segment);
            let title = Self::extract_title(video_segment);

            let embed_html_raw = Self::extract_between(video_segment, "data-embed='", "'")
                .or_else(|| Self::extract_between(video_segment, "data-embed=\"", "\""))
                .unwrap_or_default()
                .to_string();
            let embed_html = decode(embed_html_raw.as_bytes())
                .to_string()
                .unwrap_or(embed_html_raw.clone());

            let mut url_str = Self::extract_iframe_src(&embed_html);
            if url_str.is_empty() {
                url_str = Self::extract_iframe_src(video_segment);
            }
            if url_str.is_empty() {
                report_provider_error_background(
                    "perverzija",
                    "get_video_items_from_html.url_missing",
                    "missing iframe src in segment",
                );
                continue;
            }
            url_str = url_str.replace("index.php", "xs1.php");
            if url_str.starts_with("https://streamtape.com/") {
                continue; // Skip Streamtape links
            }

            let id_url = Self::extract_between(video_segment, "data-url='", "'")
                .or_else(|| Self::extract_between(video_segment, "data-url=\"", "\""))
                .unwrap_or_default()
                .to_string();

            let mut id = url_str
                .split("data=")
                .nth(1)
                .unwrap_or_default()
                .split('&')
                .next()
                .unwrap_or_default()
                .to_string();
            if id.is_empty() {
                id = id_url
                    .trim_end_matches('/')
                    .rsplit('/')
                    .next()
                    .unwrap_or_default()
                    .to_string();
            }

            let raw_duration = Self::extract_between(video_segment, "time_dur\">", "<")
                .or_else(|| Self::extract_between(video_segment, "class=\"time\">", "<"))
                .unwrap_or("00:00")
                .to_string();
            let duration = parse_time_to_seconds(&raw_duration).unwrap_or(0) as u32;
            let thumb = Self::extract_thumb(video_segment);

            match pool.get() {
                Ok(mut conn) => {
                    if !id_url.is_empty() {
                        let _ = db::insert_video(&mut conn, &id_url, &url_str);
                    }
                }
                Err(e) => {
                    report_provider_error_background(
                        "perverzija",
                        "get_video_items_from_html.insert_video.pool_get",
                        &e.to_string(),
                    );
                }
            }
            let referer_url = "https://xtremestream.xyz/".to_string();

            let mut tags: Vec<String> = Vec::new();

            let studios_parts = video_segment.split("a href=\"").collect::<Vec<&str>>();
            for studio in studios_parts.iter().skip(1) {
                if studio.starts_with("https://tube.perverzija.com/studio/") {
                    let slug = studio
                        .split("/\"")
                        .collect::<Vec<&str>>()
                        .first()
                        .copied()
                        .unwrap_or_default()
                        .replace("https://tube.perverzija.com/studio/", "");
                    self.insert_tag_mapping("studio", &slug, None);
                    Self::push_unique(
                        &mut tags,
                        Self::humanize_slug(&slug),
                    );
                }
            }

            for tag in video_segment.split_whitespace() {
                let token =
                    tag.trim_matches(|c: char| c == '"' || c == '\'' || c == '>' || c == '<');
                if token.starts_with("stars-") {
                    let tag_name = token
                        .split("stars-")
                        .nth(1)
                        .unwrap_or_default()
                        .split('"')
                        .next()
                        .unwrap_or_default()
                        .to_string();
                    if !tag_name.is_empty() {
                        self.insert_tag_mapping("stars", &tag_name, None);
                        Self::push_unique(&mut tags, Self::humanize_slug(&tag_name));
                    }
                }
            }

            for tag in video_segment.split_whitespace() {
                let token =
                    tag.trim_matches(|c: char| c == '"' || c == '\'' || c == '>' || c == '<');
                if token.starts_with("tag-") {
                    let tag_name = token.split("tag-").nth(1).unwrap_or_default().to_string();
                    if !tag_name.is_empty() {
                        Self::push_unique(&mut tags, tag_name.replace("-", " ").to_string());
                    }
                }
            }
            let page_url = if id_url.is_empty() {
                url_str.clone()
            } else {
                id_url.clone()
            };
            let mut video_item = VideoItem::new(
                id,
                title,
                page_url,
                "perverzija".to_string(),
                thumb,
                duration,
            )
            .tags(tags);
            // .embed(embed.clone());
            let mut format =
                videos::VideoFormat::new(url_str.clone(), "1080".to_string(), "m3u8".to_string());
            format.add_http_header("Referer".to_string(), referer_url.clone());
            if let Some(formats) = video_item.formats.as_mut() {
                formats.push(format);
            } else {
                video_item.formats = Some(vec![format]);
            }
            items.push(video_item);
        }

        return items;
    }

    async fn get_video_items_from_html_query(&self, html: String, pool: DbPool) -> Vec<VideoItem> {
        let raw_videos: Vec<&str> = html.split("video-item post").skip(1).collect();
        if raw_videos.is_empty() {
            report_provider_error_background(
                "perverzija",
                "get_video_items_from_html_query.no_segments",
                &format!("html_len={}", html.len()),
            );
            return vec![];
        }
        let futures = raw_videos
            .into_iter()
            .map(|el| self.get_video_item(el, pool.clone()));
        let results: Vec<Result<VideoItem>> = join_all(futures).await;
        let items: Vec<VideoItem> = results.into_iter().filter_map(Result::ok).collect();

        return items;
    }

    async fn get_video_item(&self, snippet: &str, pool: DbPool) -> Result<VideoItem> {
        if snippet.trim().is_empty() {
            report_provider_error_background(
                "perverzija",
                "get_video_item.empty_snippet",
                "snippet is empty",
            );
            return Err("empty snippet".into());
        }

        let title = Self::extract_title(snippet);

        let thumb = Self::extract_thumb(snippet);
        let duration = 0;

        let lookup_url = Self::extract_between(snippet, " href=\"", "\"")
            .or_else(|| Self::extract_between(snippet, "data-url='", "'"))
            .unwrap_or_default()
            .to_string();
        if lookup_url.is_empty() {
            report_provider_error_background(
                "perverzija",
                "get_video_item.lookup_url_missing",
                "missing lookup url in snippet",
            );
            return Err("Failed to parse lookup url".into());
        }
        let referer_url = "https://xtremestream.xyz/".to_string();

        let mut conn = match pool.get() {
            Ok(conn) => conn,
            Err(e) => {
                report_provider_error("perverzija", "get_video_item.pool_get", &e.to_string())
                    .await;
                return Err("couldn't get db connection from pool".into());
            }
        };
        let db_result = db::get_video(&mut conn, lookup_url.clone());
        match db_result {
            Ok(Some(entry)) => {
                if entry.starts_with("{") {
                    // replace old urls with new json objects
                    let entry = serde_json::from_str::<PerverzijaDbEntry>(entry.as_str())?;
                    let url_str = entry.url_string;
                    let tags = entry.tags_strings;
                    if url_str.starts_with("!") {
                        return Err("Video was removed".into());
                    }
                    let mut id = url_str
                        .split("data=")
                        .collect::<Vec<&str>>()
                        .get(1)
                        .copied()
                        .unwrap_or_default()
                        .to_string();
                    if id.contains("&") {
                        id = id
                            .split("&")
                            .collect::<Vec<&str>>()
                            .get(0)
                            .copied()
                            .unwrap_or_default()
                            .to_string()
                    }
                    let mut video_item = VideoItem::new(
                        id,
                        title,
                        lookup_url.clone(),
                        "perverzija".to_string(),
                        thumb,
                        duration,
                    )
                    .tags(tags);
                    let mut format = videos::VideoFormat::new(
                        url_str.clone(),
                        "1080".to_string(),
                        "m3u8".to_string(),
                    );
                    format.add_http_header("Referer".to_string(), referer_url.clone());
                    if let Some(formats) = video_item.formats.as_mut() {
                        formats.push(format);
                    } else {
                        video_item.formats = Some(vec![format]);
                    }
                    return Ok(video_item);
                } else {
                    let _ = db::delete_video(&mut conn, lookup_url.clone());
                };
            }
            Ok(None) => {}
            Err(e) => {
                println!("Error fetching video from database: {}", e);
                // return Err(format!("Error fetching video from database: {}", e).into());
            }
        }
        drop(conn);

        let client = Client::builder().emulation(Emulation::Firefox136).build()?;

        let response = client.get(lookup_url.clone()).send().await?;
        let text = match response.status().is_success() {
            true => response.text().await?,
            false => {
                println!("Failed to fetch video details");
                return Err("Failed to fetch video details".into());
            }
        };

        let mut url_str = text
            .split("<iframe src=\"")
            .collect::<Vec<&str>>()
            .get(1)
            .copied()
            .unwrap_or_default()
            .split("\"")
            .collect::<Vec<&str>>()
            .get(0)
            .copied()
            .unwrap_or_default()
            .to_string()
            .replace("index.php", "xs1.php");
        if !url_str.contains("xtremestream.xyz") {
            url_str = "!".to_string()
        }

        let mut tags: Vec<String> = Vec::new();

        let studios_section = Self::detail_meta_section(&text, "<strong>Studio: </strong>");
        for href in Self::parse_href_values(studios_section) {
            if href.starts_with("https://tube.perverzija.com/studio/") {
                let studio_slug = href
                    .trim_end_matches('/')
                    .replace("https://tube.perverzija.com/studio/", "");
                self.insert_tag_mapping("studio", &studio_slug, None);
                Self::push_unique(&mut tags, Self::humanize_slug(&studio_slug));
            }
        }

        let stars_section = Self::detail_meta_section(&text, "<strong>Stars: </strong>");
        for href in Self::parse_href_values(stars_section) {
            if href.starts_with("https://tube.perverzija.com/stars/") {
                let star_slug = href
                    .trim_end_matches('/')
                    .replace("https://tube.perverzija.com/stars/", "");
                self.insert_tag_mapping("stars", &star_slug, None);
                Self::push_unique(&mut tags, Self::humanize_slug(&star_slug));
            }
        }

        let tags_section = if text.contains("<strong>Tags: </strong>") {
            Self::detail_meta_section(&text, "<strong>Tags: </strong>")
        } else {
            Self::detail_meta_section(&text, "<strong>Genres: </strong>")
        };
        for href in Self::parse_href_values(tags_section) {
            if href.starts_with("https://tube.perverzija.com/stars/") {
                let star_slug = href
                    .trim_end_matches('/')
                    .replace("https://tube.perverzija.com/stars/", "");
                self.insert_tag_mapping("stars", &star_slug, None);
                Self::push_unique(&mut tags, Self::humanize_slug(&star_slug));
                continue;
            }
            if href.starts_with("https://tube.perverzija.com/tag/") {
                let tag_slug = href
                    .trim_end_matches('/')
                    .replace("https://tube.perverzija.com/tag/", "");
                self.insert_tag_mapping("tag", &tag_slug, None);
                Self::push_unique(&mut tags, Self::humanize_slug(&tag_slug));
                continue;
            }
            if href.starts_with("https://tube.perverzija.com/genre/") {
                let genre_slug = href
                    .trim_end_matches('/')
                    .replace("https://tube.perverzija.com/genre/", "");
                self.insert_tag_mapping("genre", &genre_slug, None);
                Self::push_unique(&mut tags, Self::humanize_slug(&genre_slug));
            }
        }

        let perverzija_db_entry = PerverzijaDbEntry {
            url_string: url_str.clone(),
            tags_strings: tags.clone(),
        };
        match pool.get() {
            Ok(mut conn) => {
                let insert_result = db::insert_video(
                    &mut conn,
                    &lookup_url,
                    &serde_json::to_string(&perverzija_db_entry)?,
                );
                if let Err(e) = insert_result {
                    report_provider_error(
                        "perverzija",
                        "get_video_item.insert_video",
                        &e.to_string(),
                    )
                    .await;
                }
            }
            Err(e) => {
                report_provider_error(
                    "perverzija",
                    "get_video_item.insert_video.pool_get",
                    &e.to_string(),
                )
                .await;
            }
        }
        if !url_str.contains("xtremestream.xyz") {
            return Err("Video URL does not contain xtremestream.xyz".into());
        }
        let mut id = url_str
            .split("data=")
            .collect::<Vec<&str>>()
            .get(1)
            .copied()
            .unwrap_or_default()
            .to_string();
        if id.contains("&") {
            id = id
                .split("&")
                .collect::<Vec<&str>>()
                .get(0)
                .copied()
                .unwrap_or_default()
                .to_string()
        }
        // if !vid[6].contains(" src=\""){
        //     for (index,line) in vid.iter().enumerate() {
        //         println!("Line {}: {}", index, line.to_string().trim());
        //     }
        // }
        // for (index, line) in vid.iter().enumerate() {
        //     println!("Line {}: {}", index, line.to_string().trim());
        // }

        let mut video_item = VideoItem::new(
            id,
            title,
            lookup_url.clone(),
            "perverzija".to_string(),
            thumb,
            duration,
        )
        .tags(tags);
        // .embed(embed.clone());
        let mut format =
            videos::VideoFormat::new(url_str.clone(), "1080".to_string(), "m3u8".to_string());
        format.add_http_header("Referer".to_string(), referer_url.clone());
        if let Some(formats) = video_item.formats.as_mut() {
            formats.push(format);
        } else {
            video_item.formats = Some(vec![format]);
        }
        return Ok(video_item);
    }
}

#[async_trait]
impl Provider for PerverzijaProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        pool: DbPool,
        sort: String,
        query: Option<String>,
        page: String,
        per_page: String,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let _ = per_page;
        let _ = sort;
        let videos: std::result::Result<Vec<VideoItem>, Error> = match query {
            Some(q) => {
                self.query(cache, pool, page.parse::<u8>().unwrap_or(1), &q, options)
                    .await
            }
            None => {
                self.get(cache, pool, page.parse::<u8>().unwrap_or(1), options)
                    .await
            }
        };
        match videos {
            Ok(v) => v,
            Err(e) => {
                println!("Error fetching videos: {}", e);
                vec![]
            }
        }
    }

    fn get_channel(&self, clientversion: ClientVersion) -> Option<Channel> {
        Some(self.build_channel(clientversion))
    }
}
