'use server';

import { createClient } from '@/lib/supabase/server';
import { trackApiUsage } from '@/services/track-api-usage';
import { deductCredits } from '@/services/credit-service';
import { getAllSystemTags } from '@/services/tags';
import { getAIProvider, getOpenRouterClient, OPENROUTER_PROVIDER_PREFERENCES } from '@/lib/openrouter';
import { getEnrichmentCache, saveEnrichmentCache, incrementCacheHit } from '@/services/cache-enrichment';

const ENRICHMENT_MODEL = 'google/gemma-3-27b-it';

/**
 * Sleep helper for retry delays
 */
function sleep(ms: number): Promise<void> {
    return new Promise(resolve => setTimeout(resolve, ms));
}

/**
 * Enrich with OpenRouter
 */
async function enrichWithOpenRouter(instruction: string): Promise<{ data: any; apiTime: number; responseText: string }> {
    const client = getOpenRouterClient();
    const startApi = performance.now();
    const maxRetries = 3;
    let lastError: any = null;

    for (let attempt = 1; attempt <= maxRetries; attempt++) {
        try {
            const response = await client.chat.completions.create({
                model: ENRICHMENT_MODEL,
                messages: [
                    {
                        role: 'user',
                        content: instruction + `\n\nRespond ONLY with valid JSON in this format:
{
  "suggested_tags": ["tag1", "tag2"],
  "suggested_custom_tags": ["custom1"],
  "search_string": "Distillery Name Age"
}`
                    },
                ],
                temperature: 0.3,
                max_tokens: 512,
                // @ts-ignore - OpenRouter-specific field
                provider: OPENROUTER_PROVIDER_PREFERENCES,
            });

            const endApi = performance.now();
            const content = response.choices[0]?.message?.content || '{}';

            // Extract JSON from response
            let jsonStr = content;
            const jsonMatch = content.match(/```(?:json)?\s*([\s\S]*?)```/);
            if (jsonMatch) {
                jsonStr = jsonMatch[1].trim();
            }

            return {
                data: JSON.parse(jsonStr),
                apiTime: endApi - startApi,
                responseText: content
            };

        } catch (error: any) {
            lastError = error;
            const status = error?.status || error?.response?.status;

            if (status === 429 || status === 503) {
                const delay = Math.pow(2, attempt) * 1000;
                console.log(`[Enrichment] Rate limited (${status}), retry ${attempt}/${maxRetries} in ${delay}ms...`);
                await sleep(delay);
                continue;
            }

            throw error;
        }
    }

    throw lastError || new Error('OpenRouter enrichment failed after retries');
}

export async function enrichData(name: string, distillery: string, availableTags?: string, language: string = 'de') {
    const provider = getAIProvider();

    // Check API key
    if (!process.env.OPENROUTER_API_KEY) {
        return { success: false, error: 'OPENROUTER_API_KEY is not configured.' };
    }

    // ========================================
    // CACHE CHECK - Return cached data if available
    // ========================================
    if (distillery?.trim()) {
        try {
            const cached = await getEnrichmentCache(distillery);
            if (cached) {
                console.log(`[EnrichData] ✅ CACHE HIT for: ${distillery}`);
                // Fire and forget: increment hit counter
                incrementCacheHit(distillery).catch(() => { });
                return {
                    success: true,
                    data: cached,
                    cached: true,
                    provider: 'cache',
                    perf: { apiDuration: 0 }
                };
            }
            console.log(`[EnrichData] ❌ Cache MISS for: ${distillery}`);
        } catch (cacheError) {
            console.warn('[EnrichData] Cache lookup failed:', cacheError);
            // Continue with AI query
        }
    }

    let supabase;
    try {
        let tagsToUse = availableTags;
        if (!tagsToUse) {
            const systemTags = await getAllSystemTags();
            tagsToUse = systemTags.map(t => t.name).join(', ');
        }

        supabase = await createClient();
        const { data: { user } } = await supabase.auth.getUser();

        if (!user) {
            return { success: false, error: 'Nicht autorisiert.' };
        }

        const userId = user.id;

        const instruction = `Identify the sensory profile for the following whisky.
Whisky: ${name} (${distillery})
Language: ${language}
Available system tags (pick relevant ones): ${tagsToUse}

Instructions:
1. Select the most appropriate sensory tags from the "Available system tags" list.
2. If there are dominant notes that are NOT in the system list, add them to "suggested_custom_tags".
3. Provide a clean "search_string" for Whiskybase (e.g., "Distillery Name Age").`;

        console.log(`[EnrichData] Using provider: ${provider}`);

        const result = await enrichWithOpenRouter(instruction);

        console.log('[EnrichData] Response:', result.data);

        // ========================================
        // SAVE TO CACHE - Store for future lookups
        // ========================================
        if (distillery?.trim()) {
            saveEnrichmentCache(distillery, result.data).catch((err) => {
                console.warn('[EnrichData] Cache save failed:', err);
            });
        }

        // Track usage
        await trackApiUsage({
            userId: userId,
            apiType: 'gemini_ai',
            endpoint: `enrichData_${provider}`,
            success: true,
            provider: provider,
            model: ENRICHMENT_MODEL,
            responseText: result.responseText
        });

        await deductCredits(userId, 'gemini_ai', `Data enrichment (${provider})`);

        return {
            success: true,
            data: result.data,
            cached: false,
            provider,
            perf: {
                apiDuration: result.apiTime
            }
        };

    } catch (error) {
        console.error('Enrich Data Error:', error);
        return {
            success: false,
            error: error instanceof Error ? error.message : 'Fehler bei der Daten-Anreicherung.',
        };
    }
}
