import {SelectBoxProps} from "@/shared/interface";
import {BookProps} from "@/lib/models/Book";
import {SessionProps} from "@/lib/models/Session";

export interface Author {
    id: string;
    name: string;
}

export interface UserProps {
    id: string;
    name: string;
    lastName: string;
    username: string;
    authorName?: string;
    email?: string;
    accountVerified?: boolean;
    termsAccepted?: boolean;
    aiUsage: number,
    apiKeys: {
        gemini: boolean
        openai: boolean,
        anthropic: boolean,
    },
    guideTour?: GuideTour[];
    subscription?: Subscription[];
    writingLang: number;
    writingLevel: number;
    ritePoints: number;
    creditsBalance:number;
	groupId: number;
}

export interface GuideTour {
    [key: string]: boolean;
}

export interface Subscription {
    subType: string;
    subTier: number;
    status: boolean;
}

export const writingLevel: SelectBoxProps[] = [
    {value: '0', label: 'Sélectionner un niveau d\'écriture'},
    {value: '1', label: 'Je suis débutant'},
    {value: '2', label: 'Je suis intermédiaire'},
    {value: '3', label: 'Je suis avancé'},
];

export default class User {
    static getCurrentSubscription(user: UserProps | null, type: "quill-sense" | "use-your-keys" | "quill-trial"): Subscription | null {
        if (!user || !user.subscription || user.subscription.length === 0) {
            return null;
        }
        return user.subscription.find((sub: Subscription): boolean => {
            return sub.subType === type && sub.status;
        }) || null;
    }
    static getWritingLevel(level: number): string {
        switch (level) {
            case 1:
                return 'Débutant';
            case 2:
                return 'Intermédiaire';
            case 3:
                return 'Avancé';
            default:
                return 'Débutant';
        }
    }
    
    static guideTourDone(guide: GuideTour[], tour: string): boolean {
        if (!tour) return false;
        
        if (guide && guide.find((guide: GuideTour): boolean => guide[tour]) !== undefined) {
            return false;
        }

        // Vérifier ensuite dans localStorage pour le mode offline
        if (typeof window !== 'undefined' && window.localStorage) {
            const completedGuides = JSON.parse(localStorage.getItem('completedGuides') || '[]');
            if (completedGuides.includes(tour)) {
                return false;
            }
        }

        return true;
    }
    
    static setNewGuideTour(session: SessionProps, tour: string): SessionProps {
        const newGuideTour: { [key: string]: boolean }[] = [
            ...(session?.user?.guideTour ?? []),
            {[tour]: true}
        ];
        return {
            ...session,
            user: {
                ...session?.user as UserProps,
                guideTour: newGuideTour
            }
        }
    }
}
