import {FastifyReply, FastifyRequest} from 'fastify';
import * as animeService from './anime.service';
import {getExtension} from '../../shared/extensions';
import {Anime, AnimeRequest, SearchRequest, WatchStreamRequest} from '../types';

export async function getAnime(req: AnimeRequest, reply: FastifyReply) {
    try {
        const { id } = req.params;
        const source = req.query.source;

        let anime: Anime | { error: string };
        if (source === 'anilist') {
            anime = await animeService.getAnimeById(id);
        } else {
            const ext = getExtension(source);
            anime = await animeService.getAnimeInfoExtension(ext, id)
        }

        return anime;
    } catch (err) {
        return { error: "Database error" };
    }
}

export async function getAnimeEpisodes(req: AnimeRequest, reply: FastifyReply) {
    try {
        const { id } = req.params;
        const source = req.query.source || 'anilist';
        const ext = getExtension(source);

        return await animeService.searchEpisodesInExtension(
            ext,
            source,
            id
        );
    } catch (err) {
        return { error: "Database error" };
    }
}

export async function getTrending(req: FastifyRequest, reply: FastifyReply) {
    try {
        const results = await animeService.getTrendingAnime();
        return { results };
    } catch (err) {
        return { results: [] };
    }
}

export async function getTopAiring(req: FastifyRequest, reply: FastifyReply) {
    try {
        const results = await animeService.getTopAiringAnime();
        return { results };
    } catch (err) {
        return { results: [] };
    }
}

export async function search(req: SearchRequest, reply: FastifyReply) {
    try {
        const query = req.query.q;
        const results = await animeService.searchAnimeLocal(query);

        if (results.length > 0) {
            return { results: results };
        }

    } catch (err) {
        return { results: [] };
    }
}

export async function searchInExtension(req: any, reply: FastifyReply) {
    try {
        const extensionName = req.params.extension;
        const query = req.query.q;

        const ext = getExtension(extensionName);
        if (!ext) return { results: [] };

        const results = await animeService.searchAnimeInExtension(ext, extensionName, query);
        return { results };
    } catch {
        return { results: [] };
    }
}

export async function getWatchStream(req: WatchStreamRequest, reply: FastifyReply) {
    try {
        const { animeId, episode, server, category, ext, source } = req.query;

        const extension = getExtension(ext);
        if (!extension) return { error: "Extension not found" };

        return await animeService.getStreamData(
            extension,
            episode,
            animeId,
            source,
            server,
            category
        );
    } catch (err) {
        const error = err as Error;
        return { error: error.message };
    }
}