import React from 'react';
import { Brain, Cpu, Zap, Lock, RotateCcw, Wifi, FileText, Shield } from 'lucide-react';
import { cn } from '@/lib/utils';

interface AiManagerProps {
  aiConfig: any;
  handleUpdateAiProvider: (provider: string) => void;
  aiKeys: { gemini: string; claude: string };
  setAiKeys: (keys: any) => void;
  isSavingKeys: boolean;
  onSaveAiKeys: () => void;
  isTestingKeys: { gemini: boolean; claude: boolean };
  onTestAiKey: (provider: 'gemini' | 'claude') => void;
  aiPrompt: string;
  setAiPrompt: (prompt: string) => void;
  isSavingPrompt: boolean;
  onUpdatePrompt: () => void;
}

export default function AiManager({
  aiConfig,
  handleUpdateAiProvider,
  aiKeys,
  setAiKeys,
  isSavingKeys,
  onSaveAiKeys,
  isTestingKeys,
  onTestAiKey,
  aiPrompt,
  setAiPrompt,
  isSavingPrompt,
  onUpdatePrompt
}: AiManagerProps) {
  return (
    <section data-testid="ai-config" className="level-1 p-4 md:p-6 space-y-4 transition-all group/ai">
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-3">
          <div className="w-10 h-10 bg-primary/10 flex items-center justify-center text-primary border border-primary/20">
            <Brain size={20} />
          </div>
          <h2 className="text-xl font-normal text-white tracking-tight">AI Intelligence</h2>
        </div>
      </div>
      
      <div className="grid sm:grid-cols-2 gap-3">
        {aiConfig?.providers?.map((p: any) => (
          <button
            key={p.id}
            data-testid="provider-option"
            onClick={() => handleUpdateAiProvider(p.id)}
            className={cn(
              "p-4 lg:p-5 xl:p-6 lg:p-8 xl:p-10 border transition-all text-left flex items-center justify-between group",
              p.active 
                ? "bg-primary border-primary" 
                : "level-0 border-border hover:border-primary/40"
            )}
          >
            <div className="flex items-center gap-3">
              <div className={cn(
                "w-8 h-8 flex items-center justify-center transition-colors",
                p.active ? "bg-surface-container-lowest/20 text-black" : "bg-primary/10 text-primary group-hover:bg-primary/20"
              )}>
                {p.id === 'gemini' ? <Cpu size={16} /> : <Zap size={16} />}
              </div>
              <div>
                <p className={cn("text-xs font-normal tracking-tight", p.active ? "text-black" : "text-secondary")}>{p.name}</p>
                <p className={cn(
                  "text-xs font-normal mt-1 px-0.5",
                  p.active 
                    ? (p.configured ? "text-black/70" : "text-black/50") 
                    : (p.configured ? "text-emerald-500" : "text-error")
                )}>
                  {p.configured ? "● Key Configured" : "○ Missing Api Key"}
                </p>
              </div>
            </div>
            {p.active && (
              <div className="bg-surface-container-lowest/20 px-2.5 py-1 text-xs font-normal text-black tracking-tight">
                Active
              </div>
            )}
          </button>
        ))}
      </div>

      <div className="bg-primary/5 border border-primary/10 p-5 space-y-3">
        <div className="flex items-center justify-between">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-primary/10 text-primary"><Lock size={14} /></div>
            <h3 className="text-sm font-normal text-secondary tracking-tight">Provider Access Keys</h3>
          </div>
          <button
            onClick={onSaveAiKeys}
            disabled={isSavingKeys}
            data-testid="save-settings-button"
            className="btn-primary"
          >
            {isSavingKeys ? <RotateCcw size={14} className="animate-spin" /> : <Lock size={14} />}
            {isSavingKeys ? "Storing..." : "Store API Keys"}
          </button>
        </div>

        <div className="grid md:grid-cols-2 gap-4">
          <div className="space-y-1">
            <label className="text-sm font-normal text-secondary tracking-tight ml-1">Gemini Api Key</label>
            <div className="flex gap-2">
              <input
                data-testid="ai-api-key-input"
                type="password"
                value={aiKeys.gemini}
                onChange={(e) => setAiKeys({...aiKeys, gemini: e.target.value})}
                placeholder={aiConfig?.providers?.find((p: any) => p.id === 'gemini')?.masked_key || "Enter Gemini Key..."}
                className="input-field flex-1 text-xs"
              />
              <button
                onClick={() => onTestAiKey('gemini')}
                disabled={isTestingKeys.gemini}
                className={cn(
                  "px-3 py-1.5 text-xs font-normal tracking-tight transition-all border flex items-center gap-1.5",
                  isTestingKeys.gemini
                    ? "bg-surface-bright border-border text-muted cursor-wait"
                    : "btn-primary"
                )}
              >
                {isTestingKeys.gemini ? <RotateCcw size={12} className="animate-spin" /> : <Wifi size={12} />}
                {isTestingKeys.gemini ? "..." : "Test"}
              </button>
            </div>
          </div>
          <div className="space-y-1">
            <label className="text-sm font-normal text-secondary tracking-tight ml-1">Claude Api Key</label>
            <div className="flex gap-2">
              <input
                data-testid="ai-api-key-input"
                type="password"
                value={aiKeys.claude}
                onChange={(e) => setAiKeys({...aiKeys, claude: e.target.value})}
                placeholder={aiConfig?.providers?.find((p: any) => p.id === 'claude')?.masked_key || "Enter Claude Key..."}
                className="input-field flex-1 text-xs"
              />
              <button
                onClick={() => onTestAiKey('claude')}
                disabled={isTestingKeys.claude}
                className={cn(
                  "px-3 py-1.5 text-xs font-normal tracking-tight transition-all border flex items-center gap-1.5",
                  isTestingKeys.claude
                    ? "bg-surface-bright border-border text-muted cursor-wait"
                    : "btn-primary"
                )}
              >
                {isTestingKeys.claude ? <RotateCcw size={12} className="animate-spin" /> : <Wifi size={12} />}
                {isTestingKeys.claude ? "..." : "Test"}
              </button>
            </div>
          </div>
        </div>
      </div>

      <div className="space-y-2 pt-0">
        <div className="space-y-1">
          <div className="flex items-center justify-between px-1">
            <label className="text-sm font-normal text-secondary tracking-tight">System Prompt (Vision Extraction)</label>
            <button
              onClick={onUpdatePrompt}
              disabled={isSavingPrompt}
              className="btn-primary py-1.5"
            >
              {isSavingPrompt ? <RotateCcw size={14} className="animate-spin" /> : <FileText size={14} />}
              {isSavingPrompt ? "Saving..." : "Save Prompt"}
            </button>
          </div>
          <textarea
            value={aiPrompt}
            onChange={(e) => setAiPrompt(e.target.value)}
            className="log-viewer w-full min-h-[200px] text-xs text-secondary leading-relaxed outline-none focus:border-primary/50 transition-all"
          />
        </div>
        <div className="bg-primary/5 border border-primary/10 p-4 flex gap-3 items-start">
          <div className="p-2 bg-primary/10 text-primary shrink-0"><Shield size={14} /></div>
          <p className="text-xs font-normal text-secondary leading-relaxed">
            This prompt instructs the Vision AI core on label interpretation. Ensure it defines explicit mapping for technical attributes like <span className="text-primary">Item</span>, <span className="text-primary">Type</span>, and <span className="text-primary">Part Number</span> to avoid extraction null-pointers and ensure inventory data integrity.
          </p>
        </div>
      </div>
    </section>
  );
}
