import { FastifyRequest, FastifyReply } from 'fastify';

export interface AnimeTitle {
    romaji: string;
    english: string | null;
    native: string | null;
    userPreferred?: string;
}

export interface CoverImage {
    extraLarge?: string;
    large: string;
    medium?: string;
    color?: string;
}

export interface StartDate {
    year: number;
    month: number;
    day: number;
}

export interface Anime {
    updatedAt: any;
    id: number | string;
    title: AnimeTitle;
    coverImage: CoverImage;
    bannerImage?: string;
    description?: string;
    averageScore: number | null;
    format: string;
    seasonYear: number | null;
    startDate?: StartDate;
    synonyms?: string[];
    extensionName?: string;
    isExtensionResult?: boolean;
}

export interface Book {
    id: number | string;
    title: AnimeTitle;
    coverImage: CoverImage;
    bannerImage?: string;
    description?: string;
    averageScore: number | null;
    format: string;
    seasonYear: number | null;
    startDate?: StartDate;
    synonyms?: string[];
    extensionName?: string;
    isExtensionResult?: boolean;
}

export interface ExtensionSearchOptions {
    query: string;
    dub?: boolean;
    media?: {
        romajiTitle: string;
        englishTitle: string;
        startDate: StartDate;
    };
}

export interface ExtensionSearchResult {
    format: string;
    headers: any;
    id: string;
    title: string;
    image?: string;
    rating?: number;
    score?: number;
}

export interface Episode {
    url: string;
    id: string;
    number: number;
    title?: string;
}

export interface Chapter {
    index: number;
    id: string;
    number: string | number;
    title?: string;
    releaseDate?: string;
}

export interface ChapterWithProvider extends Chapter {
    provider: string;
    date?: string;
}

export interface Extension {
    getMetadata: any;
    type: 'anime-board' | 'book-board' | 'manga-board';
    mediaType?: 'manga' | 'ln';
    search?: (options: ExtensionSearchOptions) => Promise<ExtensionSearchResult[]>;
    findEpisodes?: (id: string) => Promise<Episode[]>;
    findEpisodeServer?: (episode: Episode, server: string) => Promise<any>;
    findChapters?: (id: string) => Promise<Chapter[]>;
    findChapterPages?: (chapterId: string) => Promise<any>;
    getSettings?: () => ExtensionSettings;
}

export interface ExtensionSettings {
    episodeServers: string[];
    supportsDub: boolean;
}

export interface StreamData {
    url?: string;
    sources?: any[];
    subtitles?: any[];
}

export interface MangaChapterContent {
    type: 'manga';
    chapterId: string;
    title: string | null;
    number: number;
    provider: string;
    pages: any[];
}

export interface LightNovelChapterContent {
    type: 'ln';
    chapterId: string;
    title: string | null;
    number: number;
    provider: string;
    content: any;
}

export type ChapterContent = MangaChapterContent | LightNovelChapterContent;

export interface AnimeParams {
    id: string;
}

export interface AnimeQuery {
    source?: string;
}

export interface SearchQuery {
    q: string;
}

export interface ExtensionNameParams {
    name: string;
}

export interface WatchStreamQuery {
    source: string;
    animeId: string;
    episode: string;
    server?: string;
    category?: string;
    ext: string;
}

export interface BookParams {
    id: string;
}

export interface BookQuery {
    ext?: string;
}

export interface ChapterParams {
    bookId: string;
    chapter: string;
    provider: string;
}

export interface ProxyQuery {
    url: string;
    referer?: string;
    origin?: string;
    userAgent?: string;
}

export type AnimeRequest = FastifyRequest<{
    Params: AnimeParams;
    Querystring: AnimeQuery;
}>;

export type SearchRequest = FastifyRequest<{
    Querystring: SearchQuery;
}>;

export type ExtensionNameRequest = FastifyRequest<{
    Params: ExtensionNameParams;
}>;

export type WatchStreamRequest = FastifyRequest<{
    Querystring: WatchStreamQuery;
}>;

export type BookRequest = FastifyRequest<{
    Params: BookParams;
    Querystring: BookQuery;
}>;

export type ChapterRequest = FastifyRequest<{
    Params: ChapterParams;
}>;

export type ProxyRequest = FastifyRequest<{
    Querystring: ProxyQuery;
}>;

export interface GalleryItemPreview {
    id: string;
    image: string;
    tags: string[];
    type: 'preview';
    provider?: string;
}

export interface GallerySearchResult {
    total: number;
    next: number;
    previous: number;
    pages: number;
    page: number;
    hasNextPage: boolean;
    results: GalleryItemPreview[];
}

export interface GalleryInfo {
    id: string;
    fullImage: string;
    resizedImageUrl: string;
    tags: string[];
    createdAt: string | null;
    publishedBy: string;
    rating: string;
    comments: any[];
    provider?: string;
}

export interface GalleryExtension {
    type: 'gallery';
    search: (query: string, page: number, perPage: number) => Promise<GallerySearchResult>;
    getInfo: (id: string) => Promise<Omit<GalleryInfo, 'provider'>>;
}

export interface GallerySearchRequest extends FastifyRequest {
    query: {
        q?: string;
        page?: string;
        perPage?: string;
    };
}

export interface GalleryInfoRequest extends FastifyRequest {
    params: {
        id: string;
    };
    query: {
        provider?: string;
    };
}

export interface AddFavoriteBody {
    id: string;
    title: string;
    image_url: string;
    thumbnail_url: string;
    tags?: string;
    provider: string;
    headers: string;
}

export interface RemoveFavoriteParams {
    id: string;
}

export interface Favorite {
    id: string;
    title: string;
    image_url: string;
    thumbnail_url: string;
    tags: string;
    provider: string;
    headers: string;
}

export interface FavoriteResult {
    success: boolean;
    error?: string;
    id?: string;
}