/// <reference types="jest-fetch-mock" />
import 'jest-fetch-mock';
import { FetchMock } from 'jest-fetch-mock';
import { LLMService } from '../src/services/LLMService';
require('dotenv').config();

const fetchMock = fetch as FetchMock;

describe('LLMService Refactor', () => {
    let llmService: LLMService;

    beforeAll(() => {
        fetchMock.enableMocks(); // Enable mocks
        const apiKey = process.env.GEMINI_API_KEY;
        if (!apiKey) {
            throw new Error('GEMINI_API_KEY is not defined in .env file');
        }
        llmService = new LLMService(apiKey);
    });

    afterEach(() => {
        fetchMock.resetMocks(); // Reset mocks after each test
    });

    it('should correctly categorize desires based on the rules', async () => {
        fetchMock.mockResponseOnce(JSON.stringify({
            candidates: [{
                content: {
                    parts: [{
                        text: JSON.stringify({
                            goTo: ['Pizza'],
                            alsoGood: ['Pasta'],
                            noGoes: ['Salad', 'Tacos'],
                            needsDiscussion: [],
                            considerable: []
                        })
                    }]
                }
            }]
        }));

        const desireSets = [
            { participantId: '1', wants: ['Pizza'], accepts: ['Pasta'], noGoes: ['Salad'] },
            { participantId: '2', wants: ['Pizza'], accepts: ['Pasta'], noGoes: ['Tacos'] },
        ];

        const result = await llmService.analyzeDesires(desireSets as any);

        expect(result.goTo).toContain('Pizza');
        expect(result.alsoGood).toContain('Pasta');
        expect(result.noGoes).toEqual(expect.arrayContaining(['Salad', 'Tacos']));
    });

    it('should detect inner contradictions in a desire set', async () => {
        fetchMock.mockResponseOnce(JSON.stringify({
            candidates: [{
                content: {
                    parts: [{
                        text: '"Ice Cream" and "No desserts" are contradictory.'
                    }]
                }
            }]
        }));

        const desireSet = { wants: ['Ice Cream', 'No desserts'], accepts: [], noGoes: [] };
        const hasContradictions = await llmService.checkForInnerContradictions(desireSet as any);
        expect(hasContradictions).not.toBeNull();
    });
});
