"use client";

import React, { useRef, useState, useEffect } from "react";
import { useStore, ReferenceCategory } from "@/lib/store";
import { cn } from "@/lib/utils";
import { Sparkles, Image as ImageIcon, X, Hash, AlertTriangle, Upload, Zap, Brain } from "lucide-react";

const IMAGE_COUNTS = [1, 2, 4];

export function PromptHero() {
    const {
        prompt, setPrompt, addToGallery,
        settings, setSettings,
        references, setReference, addReference, removeReference, clearReferences,
        setSelectionMode, setCurrentView,
        history, setHistory
    } = useStore();

    const [isGenerating, setIsGenerating] = useState(false);
    const [uploadingRefs, setUploadingRefs] = useState<Record<string, boolean>>({});
    const [errorNotification, setErrorNotification] = useState<{ message: string; type: 'error' | 'warning' } | null>(null);
    const textareaRef = useRef<HTMLTextAreaElement>(null);

    // File input refs for each reference category
    const fileInputRefs = {
        subject: useRef<HTMLInputElement>(null),
        scene: useRef<HTMLInputElement>(null),
        style: useRef<HTMLInputElement>(null),
        video: useRef<HTMLInputElement>(null),
    };

    // Auto-resize textarea
    useEffect(() => {
        if (textareaRef.current) {
            textareaRef.current.style.height = 'auto';
            textareaRef.current.style.height = Math.min(textareaRef.current.scrollHeight, 200) + 'px';
        }
    }, [prompt]);

    const handleGenerate = async () => {
        let finalPrompt = prompt.trim();
        if (!finalPrompt || isGenerating) return;

        // Try to parse JSON if it looks like it
        if (finalPrompt.startsWith('{') && finalPrompt.endsWith('}')) {
            try {
                const json = JSON.parse(finalPrompt);
                if (json.prompt || json.text || json.positive) {
                    finalPrompt = json.prompt || json.text || json.positive;
                    setPrompt(finalPrompt);
                }
            } catch (e) {
                // Ignore parse errors
            }
        }

        setIsGenerating(true);

        try {
            // Route to the selected provider
            const provider = settings.provider || 'whisk';
            let res: Response;

            if (provider === 'grok') {
                // Grok API
                res = await fetch('/api/grok/generate', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        prompt: finalPrompt,
                        apiKey: settings.grokApiKey,
                        cookies: settings.grokCookies,
                        imageCount: settings.imageCount
                    })
                });
            } else if (provider === 'meta') {
                // Meta AI
                res = await fetch('/api/meta/generate', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        prompt: finalPrompt,
                        cookies: settings.metaCookies,
                        imageCount: settings.imageCount
                    })
                });
            } else {
                // Default: Whisk (Google ImageFX)
                const refsForApi = {
                    subject: references.subject?.map(r => r.id) || [],
                    scene: references.scene?.map(r => r.id) || [],
                    style: references.style?.map(r => r.id) || [],
                };

                res = await fetch('/api/generate', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        prompt: finalPrompt,
                        aspectRatio: settings.aspectRatio,
                        preciseMode: settings.preciseMode,
                        imageCount: settings.imageCount,
                        cookies: settings.whiskCookies,
                        refs: refsForApi
                    })
                });
            }

            const responseText = await res.text();
            let data;
            try {
                data = JSON.parse(responseText);
            } catch (e) {
                console.error("API Error (Non-JSON response):", responseText.substring(0, 500));
                throw new Error(`Server Error: ${res.status} ${res.statusText}`);
            }

            if (data.error) throw new Error(data.error);

            if (data.images) {
                // Add images one by one with createdAt
                for (const img of data.images) {
                    await addToGallery({
                        data: img.data,
                        prompt: img.prompt,
                        aspectRatio: img.aspectRatio || settings.aspectRatio,
                        createdAt: Date.now()
                    });
                }
            }

        } catch (e: any) {
            console.error(e);
            const errorMessage = e.message || '';

            // Check for various Google safety policy errors
            if (errorMessage.includes('PROMINENT_PEOPLE_FILTER_FAILED') ||
                errorMessage.includes('prominent_people')) {
                setErrorNotification({
                    message: '🚫 Content Policy: The reference image contains a recognizable person. Google blocks generating images of real/famous people. Try using a different reference image without identifiable faces.',
                    type: 'warning'
                });
            } else if (errorMessage.includes('Safety Filter') ||
                errorMessage.includes('SAFETY_FILTER') ||
                errorMessage.includes('content_policy')) {
                setErrorNotification({
                    message: '⚠️ Content Moderation: Your prompt or image was flagged by Google\'s safety filters. Try using different wording or a safer subject.',
                    type: 'warning'
                });
            } else if (errorMessage.includes('NSFW') ||
                errorMessage.includes('nsfw_filter')) {
                setErrorNotification({
                    message: '🔞 Content Policy: The request was blocked for NSFW content. Please use appropriate prompts and images.',
                    type: 'warning'
                });
            } else if (errorMessage.includes('CHILD_SAFETY') ||
                errorMessage.includes('child_safety')) {
                setErrorNotification({
                    message: '⛔ Content Policy: Request blocked for child safety concerns.',
                    type: 'error'
                });
            } else if (errorMessage.includes('RATE_LIMIT') ||
                errorMessage.includes('429') ||
                errorMessage.includes('quota')) {
                setErrorNotification({
                    message: '⏳ Rate Limited: Too many requests. Please wait a moment and try again.',
                    type: 'warning'
                });
            } else if (errorMessage.includes('401') ||
                errorMessage.includes('Unauthorized') ||
                errorMessage.includes('cookies not found')) {
                setErrorNotification({
                    message: '🔐 Authentication Error: Your Whisk cookies may have expired. Please update them in Settings.',
                    type: 'error'
                });
            } else {
                setErrorNotification({
                    message: errorMessage || 'Generation failed. Please try again.',
                    type: 'error'
                });
            }
            // Auto-dismiss after 8 seconds
            setTimeout(() => setErrorNotification(null), 8000);
        } finally {
            setIsGenerating(false);
        }
    };

    const handleKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
            e.preventDefault();
            handleGenerate();
        }
    };

    const handlePaste = (e: React.ClipboardEvent) => {
        try {
            const text = e.clipboardData.getData('text');
            if (text.trim().startsWith('{')) {
                const json = JSON.parse(text);
                const cleanPrompt = json.prompt || json.text || json.positive || json.caption;

                if (cleanPrompt && typeof cleanPrompt === 'string') {
                    e.preventDefault();
                    setPrompt(cleanPrompt);
                }
            }
        } catch (e) {
            // Not JSON
        }
    };

    const handleDragOver = (e: React.DragEvent) => {
        e.preventDefault();
        e.stopPropagation();
    };

    const handleDrop = async (e: React.DragEvent, category: ReferenceCategory) => {
        e.preventDefault();
        e.stopPropagation();

        if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
            const file = e.dataTransfer.files[0];
            if (!file.type.startsWith('image/')) return;
            await uploadReference(file, category);
        }
    };

    const uploadReference = async (file: File, category: ReferenceCategory) => {
        if (!settings.whiskCookies) {
            alert("Please set your Whisk Cookies in Settings first!");
            return;
        }
        setUploadingRefs(prev => ({ ...prev, [category]: true }));
        try {
            const reader = new FileReader();
            reader.onload = async (e) => {
                const base64 = e.target?.result as string;
                if (!base64) return;

                const res = await fetch('/api/references/upload', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        imageBase64: base64,
                        mimeType: file.type,
                        category: category,
                        cookies: settings.whiskCookies
                    })
                });

                const data = await res.json();
                if (data.id) {
                    // Add to array (supports multiple refs per category)
                    addReference(category, { id: data.id, thumbnail: base64 });

                    // Add to history
                    const newItem = {
                        id: data.id,
                        url: base64, // For local display history we use base64. Ideally we'd valid URL but this works for session.
                        category: category,
                        originalName: file.name
                    };
                    // exist check?
                    const exists = history.find(h => h.id === data.id);
                    if (!exists) {
                        setHistory([newItem, ...history]);
                    }

                } else {
                    console.error("Upload failed details:", JSON.stringify(data));
                    alert(`Upload failed: ${data.error}\n\nDetails: ${JSON.stringify(data) || 'Check console'}`);
                }
                setUploadingRefs(prev => ({ ...prev, [category]: false }));
            };
            reader.readAsDataURL(file);
        } catch (error) {
            console.error(error);
            setUploadingRefs(prev => ({ ...prev, [category]: false }));
        }
    };

    // Handle file input change for click-to-upload (supports multiple files)
    const handleFileInputChange = (e: React.ChangeEvent<HTMLInputElement>, category: ReferenceCategory) => {
        const files = e.target.files;
        if (files && files.length > 0) {
            // Upload each selected file
            Array.from(files).forEach(file => {
                if (file.type.startsWith('image/')) {
                    uploadReference(file, category);
                }
            });
        }
        // Reset input value so same file can be selected again
        e.target.value = '';
    };

    // Open file picker for a category
    const openFilePicker = (category: ReferenceCategory) => {
        const inputRef = fileInputRefs[category];
        if (inputRef.current) {
            inputRef.current.click();
        }
    };

    const toggleReference = (category: ReferenceCategory) => {
        // Always open file picker to add more references
        // Users can remove individual refs or clear all via the X button
        openFilePicker(category);
    };

    const nextAspectRatio = () => {
        const ratios = ['1:1', '16:9', '9:16', '4:3', '3:4'];
        const currentIdx = ratios.indexOf(settings.aspectRatio);
        setSettings({ aspectRatio: ratios[(currentIdx + 1) % ratios.length] });
    };

    const cycleImageCount = () => {
        const currentIdx = IMAGE_COUNTS.indexOf(settings.imageCount);
        setSettings({ imageCount: IMAGE_COUNTS[(currentIdx + 1) % IMAGE_COUNTS.length] });
    };

    // Whisk-style gradient button helper
    const GradientButton = ({ onClick, disabled, children, className }: any) => (
        <button
            onClick={onClick}
            disabled={disabled}
            className={cn(
                "relative group overflow-hidden rounded-full px-6 py-2.5 font-semibold text-sm transition-all shadow-lg",
                disabled
                    ? "bg-white/5 text-white/30 cursor-not-allowed shadow-none"
                    : "text-white shadow-purple-500/20 hover:shadow-purple-500/40 hover:scale-[1.02]",
                className
            )}
        >
            <div className={cn(
                "absolute inset-0 bg-gradient-to-r from-amber-500 to-purple-600 transition-opacity",
                disabled ? "opacity-0" : "opacity-100 group-hover:opacity-90"
            )} />
            <div className="relative flex items-center gap-2">
                {children}
            </div>
        </button>
    );

    return (
        <div className="w-full max-w-4xl mx-auto my-8 md:my-12 px-4">
            {/* Error/Warning Notification Toast */}
            {errorNotification && (
                <div className={cn(
                    "mb-4 p-4 rounded-xl border flex items-start gap-3 animate-in slide-in-from-top-4 duration-300",
                    errorNotification.type === 'warning'
                        ? "bg-amber-500/10 border-amber-500/30 text-amber-200"
                        : "bg-red-500/10 border-red-500/30 text-red-200"
                )}>
                    <AlertTriangle className={cn(
                        "h-5 w-5 shrink-0 mt-0.5",
                        errorNotification.type === 'warning' ? "text-amber-400" : "text-red-400"
                    )} />
                    <div className="flex-1">
                        <p className="text-sm font-medium">
                            {errorNotification.type === 'warning' ? '⚠️ Content Moderation' : '❌ Generation Error'}
                        </p>
                        <p className="text-xs mt-1 opacity-80">{errorNotification.message}</p>
                    </div>
                    <button
                        onClick={() => setErrorNotification(null)}
                        className="p-1 hover:bg-white/10 rounded-full transition-colors"
                    >
                        <X className="h-4 w-4" />
                    </button>
                </div>
            )}

            <div className={cn(
                "relative flex flex-col gap-4 rounded-3xl bg-[#1A1A1E]/90 bg-gradient-to-b from-white/[0.02] to-transparent p-6 shadow-2xl border border-white/5 backdrop-blur-sm transition-all",
                isGenerating && "ring-1 ring-purple-500/30"
            )}>



                {/* Header / Title + Provider Toggle */}
                <div className="flex items-center justify-between mb-4">
                    <div className="flex items-center gap-4">
                        <div className="h-12 w-12 rounded-2xl bg-gradient-to-br from-amber-500/20 to-purple-600/20 border border-white/5 flex items-center justify-center">
                            {settings.provider === 'grok' ? (
                                <Zap className="h-6 w-6 text-yellow-400" />
                            ) : settings.provider === 'meta' ? (
                                <Brain className="h-6 w-6 text-blue-400" />
                            ) : (
                                <Sparkles className="h-6 w-6 text-amber-300" />
                            )}
                        </div>
                        <div>
                            <h2 className="text-xl font-bold text-white tracking-tight">Create & Remix</h2>
                            <p className="text-xs text-white/50 font-medium">
                                Powered by <span className={cn(
                                    settings.provider === 'grok' ? "text-yellow-400" :
                                        settings.provider === 'meta' ? "text-blue-400" :
                                            "text-amber-300"
                                )}>
                                    {settings.provider === 'grok' ? 'Grok (xAI)' :
                                        settings.provider === 'meta' ? 'Meta AI' :
                                            'Google Whisk'}
                                </span>
                            </p>
                        </div>
                    </div>

                    {/* Provider Toggle */}
                    <div className="flex bg-black/40 p-1 rounded-xl border border-white/10 backdrop-blur-md">
                        <button
                            onClick={() => setSettings({ provider: 'whisk' })}
                            className={cn(
                                "flex items-center gap-2 px-3 py-1.5 rounded-lg text-xs font-medium transition-all",
                                settings.provider === 'whisk' || !settings.provider
                                    ? "bg-white/10 text-white shadow-sm"
                                    : "text-white/40 hover:text-white/70 hover:bg-white/5"
                            )}
                            title="Google Whisk"
                        >
                            <Sparkles className="h-3.5 w-3.5" />
                            <span className="hidden sm:inline">Whisk</span>
                        </button>
                        <button
                            onClick={() => setSettings({ provider: 'grok' })}
                            className={cn(
                                "flex items-center gap-2 px-3 py-1.5 rounded-lg text-xs font-medium transition-all",
                                settings.provider === 'grok'
                                    ? "bg-white/10 text-white shadow-sm"
                                    : "text-white/40 hover:text-white/70 hover:bg-white/5"
                            )}
                            title="Grok (xAI)"
                        >
                            <Zap className="h-3.5 w-3.5" />
                            <span className="hidden sm:inline">Grok</span>
                        </button>
                        <button
                            onClick={() => setSettings({ provider: 'meta' })}
                            className={cn(
                                "flex items-center gap-2 px-3 py-1.5 rounded-lg text-xs font-medium transition-all",
                                settings.provider === 'meta'
                                    ? "bg-white/10 text-white shadow-sm"
                                    : "text-white/40 hover:text-white/70 hover:bg-white/5"
                            )}
                            title="Meta AI"
                        >
                            <Brain className="h-3.5 w-3.5" />
                            <span className="hidden sm:inline">Meta</span>
                        </button>
                    </div>
                </div>

                {/* Input Area */}
                <div className="relative group">
                    <div className="absolute -inset-0.5 bg-gradient-to-r from-amber-500/20 to-purple-600/20 rounded-2xl blur opacity-0 group-hover:opacity-100 transition duration-500" />
                    <textarea
                        ref={textareaRef}
                        value={prompt}
                        onChange={(e) => setPrompt(e.target.value)}
                        onKeyDown={handleKeyDown}
                        onPaste={handlePaste}
                        placeholder="Describe your imagination... (e.g. 'A futuristic city with flying cars')"
                        className="relative w-full resize-none bg-[#0E0E10] rounded-xl p-5 text-base md:text-lg text-white placeholder:text-white/20 outline-none min-h-[120px] border border-white/10 focus:border-purple-500/50 transition-all shadow-inner"
                    />
                </div>

                {/* Controls Area */}
                <div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-6 pt-2">

                    {/* Left Controls: References */}
                    <div className="flex flex-wrap gap-2">
                        {(['subject', 'scene', 'style'] as ReferenceCategory[]).map((cat) => {
                            const refs = references[cat] || [];
                            const hasRefs = refs.length > 0;
                            const isUploading = uploadingRefs[cat];
                            return (
                                <div key={cat} className="relative group">
                                    <button
                                        onClick={() => toggleReference(cat)}
                                        onDragOver={handleDragOver}
                                        onDrop={(e) => handleDrop(e, cat)}
                                        className={cn(
                                            "flex items-center gap-2 rounded-full px-4 py-2 text-xs font-medium transition-all border relative overflow-hidden",
                                            hasRefs
                                                ? "bg-purple-500/10 text-purple-200 border-purple-500/30 hover:bg-purple-500/20"
                                                : "bg-white/5 text-white/40 border-white/5 hover:bg-white/10 hover:text-white/70 hover:border-white/10",
                                            isUploading && "animate-pulse cursor-wait"
                                        )}
                                    >
                                        {isUploading ? (
                                            <div className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
                                        ) : hasRefs ? (
                                            <div className="flex -space-x-2">
                                                {refs.slice(0, 4).map((ref, idx) => (
                                                    <img
                                                        key={ref.id}
                                                        src={ref.thumbnail}
                                                        alt=""
                                                        className="h-5 w-5 rounded-sm object-cover ring-1 ring-white/20"
                                                        style={{ zIndex: 10 - idx }}
                                                    />
                                                ))}
                                                {refs.length > 4 && (
                                                    <div className="h-5 w-5 rounded-sm bg-purple-500/50 flex items-center justify-center text-[9px] font-bold ring-1 ring-white/20">
                                                        +{refs.length - 4}
                                                    </div>
                                                )}
                                            </div>
                                        ) : (
                                            <Upload className="h-4 w-4" />
                                        )}
                                        <span className="capitalize tracking-wide">{cat}</span>
                                        {refs.length > 0 && (
                                            <span className="text-[10px] bg-purple-500/30 text-purple-100 rounded-full px-1.5 h-4 flex items-center">{refs.length}</span>
                                        )}
                                    </button>
                                    {/* Clear all button */}
                                    {hasRefs && !isUploading && (
                                        <button
                                            className="absolute -top-1 -right-1 p-1 rounded-full bg-red-500/80 text-white opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-500"
                                            onClick={(e) => { e.stopPropagation(); clearReferences(cat); }}
                                            title={`Clear all ${cat} references`}
                                        >
                                            <X className="h-2.5 w-2.5" />
                                        </button>
                                    )}
                                </div>
                            );
                        })}
                    </div>

                    {/* Hidden file inputs for upload */}
                    <input
                        type="file"
                        ref={fileInputRefs.subject}
                        accept="image/*"
                        multiple
                        className="hidden"
                        onChange={(e) => handleFileInputChange(e, 'subject')}
                    />
                    <input
                        type="file"
                        ref={fileInputRefs.scene}
                        accept="image/*"
                        multiple
                        className="hidden"
                        onChange={(e) => handleFileInputChange(e, 'scene')}
                    />
                    <input
                        type="file"
                        ref={fileInputRefs.style}
                        accept="image/*"
                        multiple
                        className="hidden"
                        onChange={(e) => handleFileInputChange(e, 'style')}
                    />

                    {/* Right Controls: Settings & Generate */}
                    <div className="flex flex-wrap items-center gap-3 w-full md:w-auto justify-end">

                        {/* Settings Group */}
                        <div className="flex items-center gap-1 bg-[#0E0E10] p-1.5 rounded-full border border-white/10">
                            {/* Image Count */}
                            <button
                                onClick={cycleImageCount}
                                className="flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium text-white/60 hover:text-white hover:bg-white/5 transition-colors"
                                title="Number of images"
                            >
                                <Hash className="h-3.5 w-3.5 opacity-70" />
                                <span>{settings.imageCount}</span>
                            </button>

                            <div className="w-px h-3 bg-white/10" />

                            {/* Aspect Ratio */}
                            <button
                                onClick={nextAspectRatio}
                                className="px-3 py-1.5 rounded-full text-xs font-medium text-white/60 hover:text-white hover:bg-white/5 transition-colors"
                                title="Aspect Ratio"
                            >
                                <span className="opacity-70">Ratio:</span>
                                <span className="ml-1 text-white/80">{settings.aspectRatio}</span>
                            </button>

                            <div className="w-px h-3 bg-white/10" />

                            {/* Precise Mode */}
                            <button
                                onClick={() => setSettings({ preciseMode: !settings.preciseMode })}
                                className={cn(
                                    "px-3 py-1.5 rounded-full text-xs font-medium transition-all flex items-center gap-1.5",
                                    settings.preciseMode
                                        ? "text-amber-300 bg-amber-500/10 ring-1 ring-amber-500/30"
                                        : "text-white/40 hover:text-white hover:bg-white/5"
                                )}
                                title="Precise Mode: Uses images directly as visual reference"
                            >
                                <span>🍌</span>
                                <span>Precise</span>
                            </button>
                        </div>

                        {/* Generate Button */}
                        <GradientButton
                            onClick={handleGenerate}
                            disabled={isGenerating || !prompt.trim()}
                        >
                            {isGenerating ? (
                                <>
                                    <div className="h-4 w-4 animate-spin rounded-full border-2 border-white border-t-transparent" />
                                    <span>Creating...</span>
                                </>
                            ) : (
                                <>
                                    <Sparkles className="h-4 w-4" />
                                    <span>Create</span>
                                </>
                            )}
                        </GradientButton>
                    </div>
                </div>

                {/* Reference Preview Panel - shows when any references exist */}
                {(references.subject?.length || references.scene?.length || references.style?.length) ? (
                    <div className="mt-4 p-3 rounded-xl bg-white/5 border border-white/10">
                        <div className="flex flex-wrap gap-4">
                            {(['subject', 'scene', 'style'] as ReferenceCategory[]).map((cat) => {
                                const refs = references[cat] || [];
                                if (refs.length === 0) return null;
                                return (
                                    <div key={cat} className="flex-1 min-w-[120px]">
                                        <div className="text-[10px] uppercase tracking-wider text-white/40 mb-2 flex items-center justify-between">
                                            <span>{cat}</span>
                                            <span className="text-purple-300">{refs.length}</span>
                                        </div>
                                        <div className="flex flex-wrap gap-1.5">
                                            {refs.map((ref) => (
                                                <div key={ref.id} className="relative group/thumb">
                                                    <img
                                                        src={ref.thumbnail}
                                                        alt=""
                                                        className="h-10 w-10 rounded object-cover ring-1 ring-white/10 group-hover/thumb:ring-purple-500/50 transition-all"
                                                    />
                                                    <button
                                                        onClick={() => removeReference(cat, ref.id)}
                                                        className="absolute -top-1 -right-1 p-0.5 rounded-full bg-red-500 text-white opacity-0 group-hover/thumb:opacity-100 transition-opacity hover:bg-red-600"
                                                        title="Remove this reference"
                                                    >
                                                        <X className="h-2.5 w-2.5" />
                                                    </button>
                                                </div>
                                            ))}
                                            {/* Add more button */}
                                            <button
                                                onClick={() => openFilePicker(cat)}
                                                className="h-10 w-10 rounded border border-dashed border-white/20 flex items-center justify-center text-white/30 hover:text-white/60 hover:border-white/40 transition-colors"
                                                title={`Add more ${cat} references`}
                                            >
                                                <span className="text-lg">+</span>
                                            </button>
                                        </div>
                                    </div>
                                );
                            })}
                        </div>
                    </div>
                ) : null}
            </div>
        </div>
    );
}
