// Server-only: Gemini AI service
// This file is only imported by API routes, never by client code

import { GoogleGenAI, Type } from "@google/genai";
import { BookProject, UserProfile } from "./types";

const truncate = (str: string, length: number) => {
    if (!str) return "";
    return str.length > length ? str.substring(0, length) + "..." : str;
};

const checkUsage = (user: UserProfile) => {
    if (user.subscription.plan === 'master') return true;
    return user.usage.aiActionsCurrent < user.usage.aiActionsLimit;
};

const buildContextPrompt = (project: BookProject, currentChapterId: string, instruction: string) => {
    const currentChapterIndex = project.chapters.findIndex(c => c.id === currentChapterId);
    const previousSummaries = project.chapters
        .slice(0, currentChapterIndex)
        .map((c, i) => `Chapitre ${i + 1} (${c.title}): ${c.summary || truncate(c.content.replace(/<[^>]*>?/gm, ''), 200)}`)
        .join('\n');

    const entitiesContext = project.entities
        .map(e => {
            const base = `[${e.type}] ${e.name}: ${truncate(e.description, 150)}`;
            const context = e.storyContext ? `\n   - VÉCU/ÉVOLUTION DANS L'HISTOIRE: ${truncate(e.storyContext, 500)}` : '';
            return base + context;
        })
        .join('\n');

    const ideasContext = (project.ideas || [])
        .map(i => {
            const statusMap: Record<string, string> = { todo: 'À FAIRE', progress: 'EN COURS', done: 'TERMINÉ' };
            return `[IDÉE - ${statusMap[i.status]}] ${i.title}: ${truncate(i.description, 100)}`;
        })
        .join('\n');

    const currentContent = project.chapters[currentChapterIndex]?.content.replace(/<[^>]*>?/gm, '') || "";
    const s = project.settings;
    const settingsPrompt = s ? `
PARAMÈTRES DU ROMAN:
- Genre: ${s.genre} ${s.subGenre ? `(${s.subGenre})` : ''}
- Public: ${s.targetAudience}
- Ton: ${s.tone}
- Narration: ${s.pov}
- Temps: ${s.tense}
- Thèmes: ${s.themes}
- Synopsis Global: ${truncate(s.synopsis || '', 500)}
` : "";

    return `
Tu es un assistant éditorial expert et un co-auteur créatif.
L'utilisateur écrit un livre intitulé "${project.title}".

${settingsPrompt}

CONTEXTE DE L'HISTOIRE (Résumé des chapitres précédents):
${previousSummaries || "Aucun chapitre précédent."}

BIBLE DU MONDE (Personnages et Lieux):
${entitiesContext || "Aucune fiche créée."}

BOÎTE À IDÉES & NOTES (Pistes de l'auteur):
${ideasContext || "Aucune note."}

CHAPITRE ACTUEL (Texte brut):
${truncate(currentContent, 3000)}

STYLE D'ÉCRITURE SPÉCIFIQUE (Instruction de l'auteur):
${project.styleGuide || "Standard, neutre."}

TA MISSION:
${instruction}
  `;
};

export const generateStoryContent = async (
    project: BookProject,
    currentChapterId: string,
    userPrompt: string,
    user: UserProfile,
): Promise<{ text: string; type: 'draft' | 'reflection' }> => {
    if (!checkUsage(user)) {
        return { text: "Limite d'actions IA atteinte pour ce mois. Passez au plan Pro !", type: 'reflection' };
    }

    try {
        const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
        const finalPrompt = buildContextPrompt(project, currentChapterId, userPrompt);

        const modelName = user.subscription.plan === 'master' ? 'gemini-3-pro-preview' : 'gemini-3-flash-preview';

        const response = await ai.models.generateContent({
            model: modelName,
            contents: finalPrompt,
            config: {
                temperature: 0.7,
                responseMimeType: "application/json",
                responseSchema: {
                    type: Type.OBJECT,
                    properties: {
                        responseType: {
                            type: Type.STRING,
                            enum: ["draft", "reflection"]
                        },
                        content: {
                            type: Type.STRING
                        }
                    }
                }
            }
        });

        const result = JSON.parse(response.text || "{}");
        return {
            text: result.content || "Erreur de génération.",
            type: result.responseType || "reflection"
        };
    } catch (error) {
        console.error("AI Generation Error:", error);
        return { text: "Erreur lors de la communication avec l'IA.", type: 'reflection' };
    }
};

export const transformTextServer = async (
    text: string,
    mode: 'correct' | 'rewrite' | 'expand' | 'continue',
    context: string,
    user: UserProfile,
): Promise<string> => {
    if (!checkUsage(user)) return "Limite d'actions IA atteinte.";
    try {
        const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
        const prompt = `Action: ${mode}. Texte: ${text}. Contexte: ${truncate(context, 1000)}. Renvoie juste le texte transformé.`;
        const response = await ai.models.generateContent({ model: 'gemini-3-flash-preview', contents: prompt });
        return response.text?.trim() || text;
    } catch {
        return text;
    }
};

export const analyzeStyle = async (_text: string) => "Style analysé";
export const summarizeText = async (_text: string) => "Résumé généré";
