async function searchResults(keyword) {
    const results = [];
    const baseUrl = "https://www.beatz-anime.net";
    try {
        const response = await fetchv2("https://www.beatz-anime.net/busqueda.php");
        const html = await response.text();

        const cardRegex = /<div class="col-lg-2 mb-4">([\s\S]*?)<\/div>\s*<\/div>/gs;
        let cardMatch;
        while ((cardMatch = cardRegex.exec(html)) !== null) {
            const cardHTML = cardMatch[1];

            const infoRegex = /<a href="([^"]+)">.*?<span class="titulo"[^>]*>([^<]+)<\/span>.*?<img[^>]+src="([^"]+)"/s;
            const infoMatch = infoRegex.exec(cardHTML);
            if (infoMatch) {
                const title = infoMatch[2].trim();
                if (title.toLowerCase().includes(keyword.toLowerCase())) {
                    results.push({
                        href: baseUrl + infoMatch[1].trim(),
                        title: title,
                        image: baseUrl + infoMatch[3].trim()
                    });
                }
            }
        }

        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            title: "Error",
            image: "Error",
            href: "Error"
        }]);
    }
}

async function extractDetails(url) {
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const pMatch = html.match(/<p class="post-text"[^>]*>([\s\S]*?)<\/p>/);
        let description = "N/A";
        let aliases = "N/A";

        if (pMatch) {
            const content = pMatch[1];

            const descMatch = content.split(/<span class="mr-3"/)[0].replace(/<br\s*\/?>/gi, "\n").trim();
            if (descMatch) description = descMatch;

            const aliasMatch = content.match(/<b>Sinónimos:<\/b>\s*([^<]+)/);
            if (aliasMatch) aliases = aliasMatch[1].trim();
        }

        return JSON.stringify([{
            description: description,
            aliases: aliases,
            airdate: "N/A"
        }]);
    } catch (err) {
        return JSON.stringify([{
            description: "Error",
            aliases: "Error",
            airdate: "Error"
        }]);
    }
}

async function extractEpisodes(url) {
    const results = [];
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const rows = html.match(/<tbody>([\s\S]*?)<\/tbody>/)?.[1].match(/<tr>[\s\S]*?<\/tr>/g) || [];

        rows.forEach((row, index) => {
            const hrefMatch = row.match(/<td class="text-center"><a href="([^"]+)"/);
            const href = hrefMatch ? hrefMatch[1] : "N/A";

            results.push({
                href: href,
                number: index + 1
            });
        });

        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            href: "Error",
            number: "Error"
        }]);
    }
}

async function extractStreamUrl(url) {
    try {
        return url;
    } catch (err) {
        return "https://error.org/";
    }
}
