'use server';

import { getOpenRouterClient, OPENROUTER_VISION_MODEL, OPENROUTER_PROVIDER_PREFERENCES } from '@/lib/openrouter';
import { getSystemPrompt } from '@/lib/ai-prompts';
import { BottleMetadataSchema, AnalysisResponse, BottleMetadata } from '@/types/whisky';
import { createClient } from '@/lib/supabase/server';
import { createHash } from 'crypto';
import { trackApiUsage } from './track-api-usage';
import { checkCreditBalance, deductCredits } from './credit-service';

// WICHTIG: Wir akzeptieren jetzt FormData statt Strings
export async function analyzeBottleOpenRouter(input: any): Promise<AnalysisResponse & { search_string?: string }> {
    if (!process.env.OPENROUTER_API_KEY) {
        return { success: false, error: 'OPENROUTER_API_KEY is not configured.' };
    }

    let supabase;
    try {
        // Helper to get value from either FormData or POJO
        const getValue = (obj: any, key: string): any => {
            if (obj && typeof obj.get === 'function') return obj.get(key);
            if (obj && typeof obj[key] !== 'undefined') return obj[key];
            return null;
        };

        // 1. Daten extrahieren
        const file = getValue(input, 'file') as File;
        const tagsString = getValue(input, 'tags') as string;
        const locale = getValue(input, 'locale') || 'de';

        if (!file) {
            return { success: false, error: 'Kein Bild empfangen.' };
        }

        const tags = tagsString ? (typeof tagsString === 'string' ? JSON.parse(tagsString) : tagsString) : [];

        // 2. Auth & Credits
        supabase = await createClient();
        const { data: { user } } = await supabase.auth.getUser();
        if (!user) {
            return { success: false, error: 'Nicht autorisiert oder Session abgelaufen.' };
        }

        const userId = user.id;
        const creditCheck = await checkCreditBalance(userId, 'gemini_ai');
        if (!creditCheck.allowed) {
            return {
                success: false,
                error: `Nicht genügend Credits. Benötigt: ${creditCheck.cost}, Verfügbar: ${creditCheck.balance}.`
            };
        }

        // 3. Datei in Buffer umwandeln
        const arrayBuffer = await file.arrayBuffer();
        const buffer = Buffer.from(arrayBuffer);

        // 4. Hash für Cache erstellen
        const imageHash = createHash('sha256').update(buffer).digest('hex');

        // Cache Check
        const { data: cachedResult } = await supabase
            .from('vision_cache')
            .select('result')
            .eq('hash', imageHash)
            .maybeSingle();

        if (cachedResult) {
            return {
                success: true,
                data: cachedResult.result as any,
                perf: {
                    apiDuration: 0,
                    parseDuration: 0,
                    uploadSize: buffer.length
                }
            };
        }

        // 5. Für OpenRouter vorbereiten
        const client = getOpenRouterClient();
        const base64Data = buffer.toString('base64');
        const mimeType = file.type || 'image/webp';
        const dataUrl = `data:${mimeType};base64,${base64Data}`;
        const uploadSize = buffer.length;

        const prompt = getSystemPrompt(tags.length > 0 ? tags.join(', ') : 'Keine Tags verfügbar', locale);

        try {
            const startApi = performance.now();
            const chatResponse = await client.chat.completions.create({
                model: OPENROUTER_VISION_MODEL,
                messages: [
                    {
                        role: 'user',
                        content: [
                            { type: 'text', text: prompt },
                            { type: 'image_url', image_url: { url: dataUrl } }
                        ]
                    } as any
                ],
                response_format: { type: 'json_object' },
                temperature: 0.1,
                extra_body: {
                    provider: OPENROUTER_PROVIDER_PREFERENCES
                }
            } as any);
            const endApi = performance.now();

            const startParse = performance.now();
            const rawContent = chatResponse.choices?.[0].message.content;
            if (!rawContent) throw new Error("Keine Antwort von OpenRouter");

            let jsonData;
            try {
                jsonData = JSON.parse(rawContent as string);
            } catch (e) {
                const cleanedText = (rawContent as string).replace(/```json/g, '').replace(/```/g, '');
                jsonData = JSON.parse(cleanedText);
            }

            if (Array.isArray(jsonData)) jsonData = jsonData[0];
            console.log('[OpenRouter AI] JSON Response:', jsonData);

            const searchString = jsonData.search_string;
            delete jsonData.search_string;

            if (typeof jsonData.abv === 'string') {
                jsonData.abv = parseFloat(jsonData.abv.replace(/%/g, '').trim());
            }

            if (jsonData.age) jsonData.age = parseInt(jsonData.age);
            if (jsonData.vintage) jsonData.vintage = parseInt(jsonData.vintage);

            const validatedData = BottleMetadataSchema.parse(jsonData);
            const endParse = performance.now();

            await trackApiUsage({
                userId: userId,
                apiType: 'gemini_ai',
                endpoint: 'openrouter/gemma',
                success: true,
                provider: 'openrouter',
                model: OPENROUTER_VISION_MODEL,
                responseText: rawContent as string
            });

            await deductCredits(userId, 'gemini_ai', 'Gemma AI analysis via OpenRouter');

            await supabase
                .from('vision_cache')
                .insert({ hash: imageHash, result: validatedData });

            return {
                success: true,
                data: validatedData,
                search_string: searchString,
                perf: {
                    apiDuration: endApi - startApi,
                    parseDuration: endParse - startParse,
                    uploadSize: uploadSize
                },
                raw: jsonData
            };

        } catch (aiError: any) {
            console.warn('[OpenRouterAnalysis] AI Analysis failed, providing fallback path:', aiError.message);

            await trackApiUsage({
                userId: userId,
                apiType: 'gemini_ai',
                endpoint: 'openrouter/gemma',
                success: false,
                errorMessage: aiError.message,
                provider: 'openrouter',
                model: OPENROUTER_VISION_MODEL
            });

            return {
                success: false,
                isAiError: true,
                error: aiError.message,
                imageHash: imageHash
            } as any;
        }

    } catch (error) {
        console.error('OpenRouter Analysis Global Error:', error);
        return {
            success: false,
            error: error instanceof Error ? error.message : 'OpenRouter AI analysis failed.',
        };
    }
}
