import { SearchResult, MediaType, VideoLink } from '../../src/types/source.js';

export const QUALITY_MAP: Record<number, string> = {
    89: "REMUX UHD", 57: "REMUX BLURAY", 92: "REMUX DVD",
    17: "Blu-Ray 1080p", 76: "Blu-Ray 1080p (x265)", 16: "Blu-Ray 720p", 18: "Blu-Ray 3D",
    52: "HD 1080p", 31: "HD 720p",
    50: "HDLight 1080p", 86: "HDLight 1080p (x265)", 49: "HDLight 720p",
    60: "Ultra HDLight (x265)", 53: "ULTRA HD (x265)",
    55: "WEB 1080p", 83: "WEB 1080p (x265)", 94: "WEB 1080p Light", 54: "WEB 720p", 4: "WEB",
    62: "HDTV 1080p", 61: "HDTV 720p", 14: "HDTV",
    15: "HDRip", 1: "DVDRIP", 51: "DVDRIP MKV",
    13: "ISO", 12: "IMG", 10: "DVD-R", 11: "Full-DVD",
};

export const LANGUAGE_MAP: Record<number, string> = {
    1: "MULTI", 2: "Arab", 3: "Bengali", 4: "Chinese", 5: "English", 6: "French", 7: "French (Canada)", 
    8: "TrueFrench", 9: "German", 10: "Hindi", 11: "Italian", 12: "Japanese", 13: "Korean", 
    14: "Mandarin", 15: "Portuguese", 16: "Russian", 17: "Spanish", 18: "Turkish", 19: "unknown", 
    23: "Danish", 28: "Finnish", 33: "Swedish", 35: "Bulgarian", 40: "Dutch", 41: "Persian", 
    42: "Indonesian", 43: "Hebrew", 44: "Thai", 49: "Czech", 53: "Albanian", 57: "Greek", 
    61: "Hungarian", 65: "Malaysian", 66: "Norwegian", 68: "Polish", 71: "Lithuanian", 
    78: "Croatian", 84: "Malay", 90: "Romanian", 96: "Ukrainian", 102: "Vietnamese", 
    105: "Sámegiella", 106: "Muet", 108: "Georgian", 110: "Nigerian", 113: "Maasai", 
    117: "Estonian", 120: "Serbian", 123: "Slovak", 124: "Slovenian", 125: "Amharic", 
    126: "Belarusian", 127: "Bosnian", 128: "Burmese", 129: "Dzongkha", 137: "Icelandic", 
    138: "Kazakh", 139: "Kurdish", 140: "Latin", 141: "Latvian", 142: "Macedonian", 143: "Maori", 
    144: "Mongolian", 145: "Norwegian Bokmål", 146: "Serbo-Croatian", 148: "Tagalog", 149: "Tibetan", 
    150: "Walloon", 151: "Wolof", 152: "Yoruba", 154: "Moore", 155: "Quechuan", 156: "Rwanda", 
    160: "Filipino", 161: "VO", 165: "Afrikaans", 171: "Créole", 174: "Gujarati", 175: "Cantonese", 
    177: "FRENCH AD"
};

export const SUB_MAP: Record<number, string> = {
    1: "Arab", 2: "Bengali", 3: "Chinese", 4: "English", 5: "French", 6: "German", 7: "Hindi", 
    8: "Italian", 9: "Japanese", 10: "Korean", 11: "Mandarin", 12: "Portuguese", 13: "Russian", 
    14: "Spanish", 15: "Turkish", 16: "Inconnu", 17: "Multi", 23: "Danish", 28: "Finnish", 
    33: "Swedish", 35: "Bulgare", 36: "Persian", 37: "Hebrew", 40: "Dutch", 42: "Indonesian", 
    50: "Thai", 53: "Greek", 61: "Hungarian", 65: "Malaysian", 66: "Norwegian", 68: "Polish", 
    71: "Lithuanian", 76: "Czech", 82: "Croatian", 88: "Malay", 94: "Romanian", 100: "Ukrainian", 
    106: "Vietnamese", 112: "Sámegiella", 115: "Estonian", 120: "Serbian", 123: "Slovak", 
    127: "Slovenian", 128: "Afrikaans", 129: "Albanian", 130: "Amharic", 131: "Armenian", 
    132: "Azerbaijani", 133: "Basque", 134: "Belarusian", 135: "Bosnian", 136: "Catalan", 
    137: "Cebuano", 138: "Chichewa", 139: "Corsican", 140: "Esperanto", 141: "Frisian", 
    142: "Galician", 143: "Georgian", 144: "Gujarati", 145: "Haitian Creole", 146: "Hausa", 
    147: "Hawaiian", 148: "Icelandic", 149: "Igbo", 150: "Irish", 151: "Javanese", 152: "Kannada", 
    153: "Kazakh", 154: "Khmer", 155: "Kurdish", 156: "Kyrgyz", 157: "Lao", 158: "Latin", 
    159: "Latvian", 160: "Luxembourgish", 161: "Macedonian", 162: "Malagasy", 163: "Maltese", 
    164: "Maori", 165: "Marathi", 166: "Mongolian", 167: "Myanmar", 168: "Nepali", 169: "Pashto", 
    170: "Punjabi", 171: "Sindhi", 172: "Sinhala", 173: "Somali", 174: "Swahili", 175: "Tajik", 
    176: "Tamil", 177: "Telugu", 178: "Uzbek", 179: "Welsh", 180: "Xhosa", 181: "Yiddish", 
    182: "Yoruba", 183: "Zulu", 184: "Filipino"
};

export function getLangs(l: any): string[] {
    if (l.langues_compact && l.langues_compact.length) {
        return l.langues_compact.map((la: any) => la.name || '');
    }
    if (l.langues && Array.isArray(l.langues)) {
        return l.langues.map((id: any) => LANGUAGE_MAP[id] || '');
    }
    return [];
}

export function getSubs(l: any): string[] {
    if (l.subs_compact && l.subs_compact.length) {
        return l.subs_compact.map((la: any) => la.name || '');
    }
    if (l.subs && Array.isArray(l.subs)) {
        return l.subs.map((id: any) => SUB_MAP[id] || '');
    }
    return [];
}

export function formatSize(bytes: number): string {
    if (!bytes || bytes === 0) return 'N/A';
    const gb = bytes / (1024 ** 3);
    if (gb >= 1) return `${gb.toFixed(2)} Go`;
    const mb = bytes / (1024 ** 2);
    return `${mb.toFixed(0)} Mo`;
}

export function parseSearchResults(data: any, mediaType: MediaType): SearchResult[] {
    const EXCLUDED_TYPES = ['games', 'music', 'app', 'ebook', 'emulation'];
    // Accepte à la fois les entrées avec model_type === 'title' et celles sans ce champ
    const results = (data.results || []).filter((r: any) =>
        (!r.model_type || r.model_type === 'title') &&
        !EXCLUDED_TYPES.includes((r.type || '').toLowerCase())
    );

    const filtered = results.filter((r: any) => {
        const rType = (r.type || (r.is_series ? 'series' : 'movie')).toLowerCase();
        if (mediaType === 'movie') {
            return rType === 'movie' || rType === 'animes' || rType === 'anime' || rType === 'doc' || rType === 'other';
        }
        // Pour les séries
        return rType === 'series' || rType === 'serie' || rType === 'animes' || rType === 'anime' || rType === 'doc' || rType === 'other';
    });

    return filtered.map((r: any) => ({
        title: r.name,
        year: r.year || (r.release_date ? r.release_date.substring(0, 4) : 'N/A'),
        image: r.poster || r.image || null,
        hrefPath: String(r.id),
        type: r.type || (r.is_series ? 'series' : 'movie'),
        source: 'hydracker',
        hydrackerId: String(r.id)
    }));
}

export function parseTrendingResults(data: any): SearchResult[] {
    if (!data) return [];
    const results = (data.pagination || {}).data || data.data || [];
    return results.map((r: any) => ({
        title: r.name,
        year: r.year || (r.release_date ? r.release_date.substring(0, 4) : 'N/A'),
        image: r.poster || r.image || null,
        hrefPath: String(r.id),
        type: r.type || (r.is_series ? 'series' : 'movie'),
        source: 'hydracker',
        hydrackerId: String(r.id)
    })).slice(0, 19);
}

export function parseMovieLinks(data: any): VideoLink[] {
    const all: any[] = [];
    if (data.video) all.push(data.video);
    if (Array.isArray(data.alternative_videos)) all.push(...data.alternative_videos);

    return all.filter(l => l.lien).map(l => ({
        id: l.id,
        host: (l.host && l.host.name) ? l.host.name : 'Inconnu',
        url: l.lien || data.directDL,
        size: formatSize(l.taille),
        sizeBytes: l.taille || 0,
        quality: l.quality || QUALITY_MAP[l.qualite] || 'Inconnu',
        langs: getLangs(l),
        subs: getSubs(l),
        releaseName: l.release || l.name || l.titre || l.titre_release || undefined,
    }));
}

export function parseSeasons(result: any): number[] {
    if (result && !result.error) {
        const seasons = result.seasons || (result.pagination || {}).data || [];
        if (Array.isArray(seasons) && seasons.length) {
            return seasons
                .map((s: any) => typeof s === 'object' ? (s.number || s) : s)
                .filter((n: any) => typeof n === 'number' && n > 0)
                .sort((a: number, b: number) => a - b);
        }
    }
    return [];
}

export function parsePremiumLink(body: string): string | null {
    let data;
    try { data = JSON.parse(body); } catch { return null; }

    let lienData = null;
    if (data.liens && Array.isArray(data.liens) && data.liens.length > 0) {
        lienData = data.liens[0];
    } else {
        lienData = data.lien || data;
    }

    return lienData.lien || lienData.url || lienData.link || null;
}
