/**
 * Appels réseau pour le plugin ZT.
 * Toutes les fonctions fetch sont ici ; le parsing reste dans parser.ts.
 */

export async function fetchSearchResults(baseUrl: string, query: string): Promise<string> {
    const url = `${baseUrl}/engine/ajax/controller.php?mod=filter&catid=0&q=${encodeURIComponent(query)}&art=0&AiffchageMode=0&inputTirePar=0&cstart=0`;
    const res = await fetch(url, {
        headers: {
            'User-Agent': 'Mozilla/5.0',
            'Accept': 'text/html, */*',
            'X-Requested-With': 'XMLHttpRequest',
            'Referer': baseUrl
        }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return res.text();
}

export async function fetchTrendingMovies(baseUrl: string): Promise<string> {
    const res = await fetch(`${baseUrl}/engine/ajax/controller.php?mod=filter&catid=3&q=&art=0&AiffchageMode=0&inputTirePar=0&cstart=0`, {
        headers: { 'User-Agent': 'Mozilla/5.0' }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return res.text();
}

export async function fetchTrendingSeries(baseUrl: string): Promise<string> {
    const url = `${baseUrl}/engine/ajax/controller.php?mod=filter&catid=15&q=&art=0&AiffchageMode=0&inputTirePar=1&cstart=0`;
    const res = await fetch(url, {
        headers: { 'User-Agent': 'Mozilla/5.0' }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return res.text();
}

export async function fetchContentPage(pageUrl: string): Promise<string> {
    const res = await fetch(pageUrl, {
        headers: { 'User-Agent': 'Mozilla/5.0' }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return res.text();
}

export async function fetchResolvedLink(zoneursUrl: string): Promise<string> {
    const url = zoneursUrl.startsWith('//') ? `https:${zoneursUrl}` : zoneursUrl;
    const res = await fetch(url, {
        headers: {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
        }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status} sur ${url}`);
    return res.text();
}

export async function fetchRecent(baseUrl: string): Promise<string> {
    const url = `${baseUrl}/engine/ajax/controller.php?mod=filter&catid=55&q=&art=0&AiffchageMode=0&inputTirePar=0&cstart=0`;
    const res = await fetch(url, {
        headers: { 'User-Agent': 'Mozilla/5.0' }
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return res.text();
}


