import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';

interface DesireSet {
    wants: string[];
    accepts: string[];
    noGoes: string[];
    afraidToAsk: string;
}




interface Decision {
    goTo: string;
    alsoGood: string;
    considerable: string;
    noGoes: string;
    needsDiscussion: string;
}

export class LLMService {
    private genAI: GoogleGenerativeAI;
    private model: GenerativeModel;

    constructor(apiKey: string) {
        this.genAI = new GoogleGenerativeAI(apiKey);
        this.model = this.genAI.getGenerativeModel({ model: "gemini-2.5-flash-lite" });
    }

    async analyzeDesires(desireSets: DesireSet[]): Promise<Decision> {
        const prompt = `
        You are an AI assistant that analyzes and synthesizes cooperative decisions from a group's desires. Given a list of desire sets from multiple participants, your task is to generate a concise, synthesized text for each of the following categories, reflecting the collective opinion.

        Each participant's desire set includes 'wants', 'accepts', 'noGoes', and an 'afraidToAsk' field. The 'afraidToAsk' field contains a sensitive idea that the participant is hesitant to express publicly.

        Here are the rules for categorization and synthesis, with special handling for 'afraidToAsk' ideas:
        - "goTo": Synthesize a text describing what ALL participants want without contradictions. This should include 'afraidToAsk' ideas that semantically match all other participant's 'wants' or 'afraidToAsk'. If an 'afraidToAsk' idea matches, it should be treated as a 'want' for the submitting participant. Use the more specific opinions and keep all the specific options if they do not contradict each other drastically and are not 'noGoes'.
        - "alsoGood": Synthesize a text describing what at least one participant wants (including matched 'afraidToAsk' ideas), not everyone wants but all other participants at least accept, and is not a "noGoes" for anyone. This should reflect a generally agreeable outcome. Use the more specific opinions and keep all the specific options if they do not contradict each other drastically and are not 'noGoes'.
        - "considerable": Synthesize a text describing what is wanted or accepted by some, but not all, participants (including matched 'afraidToAsk' ideas), and is not a "noGoes" for anyone. This should highlight areas of partial agreement or options that could be explored. Use the more specific opinions and keep all the specific options if they do not contradict each other drastically and are not 'noGoes'.
        - "noGoes": Synthesize a text describing what at least ONE participant does not want. This should clearly state the collective exclusions. Use the more broad opinions summarizing more specific options if they do not contradict each other drastically.
        - "needsDiscussion": Synthesize a text describing where there is a direct conflict (e.g., one participant wants it, another does not want it). This should highlight areas requiring further negotiation. Do not include 'afraidToAsk' in this category.

        'AfraidToAsk' ideas that do NOT semantically match any other participant's 'wants' or 'accepts' very closely should remain private and NOT be included in any of the synthesized categories. Matching must use minimal level of generalization.

        Formulate common ideas from the point of 'us', e.g. "We are going to...", or "We want to...", or "We think...", or "We do not...".

        The input will be a JSON object containing a list of desire sets. Each desire set has a participantId (implicitly handled by the array index) and four arrays/strings: "wants", "accepts", "noGoes", and "afraidToAsk".

        The output should be a JSON object with the following structure, where each category contains a single synthesized text:
        {
          "goTo": "Synthesized text for go-to items.",
          "alsoGood": "Synthesized text for also good items.",
          "considerable": "Synthesized text for considerable items.",
          "noGoes": "Synthesized text for no-goes items.",
          "needsDiscussion": "Synthesized text for needs discussion items."
        }

        Here is the input data:
        ${JSON.stringify(desireSets)}
        `;

        try {
            const result = await this.model.generateContent(prompt);
            const response = result.response;
            let text = response.text();

            // Clean the response to ensure it is valid JSON
            const jsonMatch = text.match(/\{.*?\}/s);
            if (jsonMatch) {
                text = jsonMatch[0];
            } else {
                // Handle cases where no JSON is found
                console.error("LLM did not return a valid JSON object. Response:", text);
                throw new Error('Failed to parse LLM response as JSON.');
            }

            return JSON.parse(text);
        } catch (error) {
            console.error("Error calling Gemini API or parsing response:", error);
            throw error;
        }
    }

    async checkForInnerContradictions(desireSet: DesireSet): Promise<string | null> {
        const prompt = `
        You are an AI assistant that detects contradictions in a list of desires. Given a JSON object with three lists of desires (wants, accepts, noGoes), determine if there are any contradictions WITHIN each list and across the lists. For example, "I want a dog" and "I don't want any pets" in the same "wants" list is a contradiction; "Pizza" in "wants" and "food" in "do not want" is a contradiction.

        If a contradiction is found, respond with a concise, single-sentence description of the contradiction. If no contradiction is found, respond with "null".

        Here is the desire set:
        ${JSON.stringify(desireSet)}
        `;

        try {
            const result = await this.model.generateContent(prompt);
            const response = result.response;
            const text = response.text().trim();

            if (text.toLowerCase() === 'null') {
                return null;
            } else {
                return text;
            }
        } catch (error) {
            console.error("Error calling Gemini API for contradiction check:", error);
            throw error;
        }
    }
}
