'use client';

import { useState, useEffect } from 'react';
import { Loader2, CheckCircle2, Lock, Sparkles } from 'lucide-react';
import { cn } from '@/lib/utils';
import { useI18n } from '@/lib/i18n';
import type { Provider, AvailableProvider } from './types';

interface ProviderSelectorProps {
  provider: Provider | null;
  onProviderChange: (provider: Provider) => void;
  availableProviders: AvailableProvider[];
  isLoadingProviders: boolean;
  isPro: boolean;
}

interface CardTheme {
  badge: string;
  subBadge: string;
  accentClass: string;
  glowClass: string;
  descriptionOverride: string;
}

const LLM_THEMES_FACTORY = (t: (k: string) => string): Record<string, CardTheme> => ({
  deepseek: {
    badge: t('providerTheme.deepseek.badge'),
    subBadge: t('providerTheme.deepseek.subBadge'),
    accentClass: 'border-cyan-500/30 text-cyan-600 dark:text-cyan-400 bg-cyan-500/5',
    glowClass: 'from-cyan-500/10 dark:from-cyan-500/5 to-transparent',
    descriptionOverride: t('providerTheme.deepseek.desc')
  },
  openai: {
    badge: t('providerTheme.openai.badge'),
    subBadge: t('providerTheme.openai.subBadge'),
    accentClass: 'border-emerald-500/30 text-emerald-600 dark:text-emerald-400 bg-emerald-500/5',
    glowClass: 'from-emerald-500/10 dark:from-emerald-500/5 to-transparent',
    descriptionOverride: t('providerTheme.openai.desc')
  },
  minimax: {
    badge: t('providerTheme.minimax.badge'),
    subBadge: t('providerTheme.minimax.subBadge'),
    accentClass: 'border-indigo-500/30 text-indigo-600 dark:text-indigo-400 bg-indigo-500/5',
    glowClass: 'from-indigo-500/10 dark:from-indigo-500/5 to-transparent',
    descriptionOverride: t('providerTheme.minimax.desc')
  },
  openrouter: {
    badge: t('providerTheme.openrouter.badge'),
    subBadge: t('providerTheme.openrouter.subBadge'),
    accentClass: 'border-purple-500/30 text-purple-600 dark:text-purple-400 bg-purple-500/5',
    glowClass: 'from-purple-500/10 dark:from-purple-500/5 to-transparent',
    descriptionOverride: t('providerTheme.openrouter.desc')
  },
  openrouter_premium: {
    badge: t('providerTheme.openrouter_premium.badge'),
    subBadge: t('providerTheme.openrouter_premium.subBadge'),
    accentClass: 'border-rose-500/30 text-rose-600 dark:text-rose-400 bg-rose-500/5',
    glowClass: 'from-rose-500/10 dark:from-rose-500/5 to-transparent',
    descriptionOverride: t('providerTheme.openrouter_premium.desc')
  },
  zai: {
    badge: t('providerTheme.zai.badge'),
    subBadge: t('providerTheme.zai.subBadge'),
    accentClass: 'border-amber-500/30 text-amber-600 dark:text-amber-400 bg-amber-500/5',
    glowClass: 'from-amber-500/10 dark:from-amber-500/5 to-transparent',
    descriptionOverride: t('providerTheme.zai.desc')
  }
});

const makeDefaultLlmTheme = (t: (k: string) => string): CardTheme => ({
  badge: t('providerTheme.default.badge'),
  subBadge: t('providerTheme.default.subBadge'),
  accentClass: 'border-brand-accent/30 text-brand-goldink dark:text-brand-accent bg-brand-accent/5',
  glowClass: 'from-brand-accent/10 to-transparent',
  descriptionOverride: t('providerTheme.default.desc')
});

const CLASSIC_THEMES_FACTORY = (t: (k: string) => string): Record<string, { labelOverride?: string; descriptionOverride?: string }> => ({
  google: {
    labelOverride: t('providerTheme.classic.google.label'),
    descriptionOverride: t('providerTheme.classic.google.desc')
  },
  google_cloud: {
    labelOverride: t('providerTheme.classic.google_cloud.label'),
    descriptionOverride: t('providerTheme.classic.google_cloud.desc')
  }
});

export function ProviderSelector({
  provider,
  onProviderChange,
  availableProviders,
  isLoadingProviders,
  isPro,
}: ProviderSelectorProps) {
  const { t } = useI18n();
  const [activeTab, setActiveTab] = useState<'classic' | 'llm'>('classic');

  // Theme maps built from i18n (so they follow the active locale)
  const LLM_THEMES = LLM_THEMES_FACTORY(t);
  const DEFAULT_LLM_THEME = makeDefaultLlmTheme(t);
  const CLASSIC_THEMES = CLASSIC_THEMES_FACTORY(t);

  // Filter providers
  const classicProviders = availableProviders.filter((p) => p.mode === 'classic');
  const llmProviders = availableProviders.filter((p) => p.mode === 'llm');

  // Initialize and synchronize activeTab based on current provider
  useEffect(() => {
    if (provider) {
      const selected = availableProviders.find((p) => p.id === provider);
      if (selected) {
        setActiveTab(selected.mode);
      }
    }
  }, [provider, availableProviders]);

  if (isLoadingProviders) {
    return (
      <div className="flex items-center gap-2 text-sm text-brand-dark/50 dark:text-white/40 py-4">
        <Loader2 className="size-4 animate-spin text-brand-goldink dark:text-brand-accent" />
        <span>{t('dashboard.translate.provider.loading')}</span>
      </div>
    );
  }

  if (availableProviders.length === 0) {
    return (
      <p className="rounded-xl border border-amber-200 bg-amber-50/50 px-4 py-3 text-xs text-amber-700 dark:border-amber-900/30 dark:bg-amber-950/20 dark:text-amber-400 font-light">
        {t('dashboard.translate.provider.noneConfigured')}
      </p>
    );
  }

  const renderClassicCard = (p: AvailableProvider) => {
    const isSelected = provider === p.id;
    const meta = CLASSIC_THEMES[p.id];
    const label = meta?.labelOverride || p.label;

    return (
      <button
        key={p.id}
        type="button"
        onClick={() => onProviderChange(p.id)}
        className={cn(
          'p-2.5 py-2 rounded-lg border text-left transition-all relative overflow-hidden flex flex-col justify-center min-h-[48px]',
          isSelected
            ? 'border-brand-accent bg-brand-accent/5 dark:bg-brand-accent/10'
            : 'border-black/[0.05] dark:border-white/[0.05] bg-brand-muted/20 dark:bg-zinc-800/10 hover:bg-brand-muted/50 dark:hover:bg-zinc-800/20'
        )}
      >
        <div className="flex items-center justify-between">
          <span className={cn(
            "text-[10px] font-black uppercase tracking-tight truncate flex-1",
            isSelected ? 'text-brand-goldink dark:text-brand-accent' : 'text-brand-dark/60 dark:text-white/60'
          )}>
            {t('providerSelector.standard')}
          </span>
          {isSelected && <div className="w-1.5 h-1.5 rounded-full bg-brand-accent animate-pulse shrink-0 ml-1" />}
        </div>
        <span className="text-xs font-bold text-brand-dark dark:text-white block leading-tight truncate">
          {label}
        </span>
      </button>
    );
  };

  const renderLlmCard = (p: AvailableProvider, locked: boolean) => {
    const isSelected = provider === p.id;
    const theme = LLM_THEMES[p.id] || DEFAULT_LLM_THEME;
    const displayLabel = p.model || p.label.replace(/^Traduction\s+IA\s+/i, '');

    return (
      <button
        key={p.id}
        type="button"
        disabled={locked}
        onClick={() => !locked && onProviderChange(p.id)}
        className={cn(
          'p-2 py-2 rounded-lg border text-left transition-all relative overflow-hidden flex flex-col justify-between min-h-[64px]',
          isSelected
            ? 'border-brand-accent bg-brand-accent/5 dark:bg-brand-accent/10'
            : locked
            ? 'cursor-not-allowed border-brand-dark/5 dark:border-white/5 bg-brand-dark/[0.01] dark:bg-white/[0.01] opacity-50'
            : 'border-black/[0.05] dark:border-white/[0.05] bg-brand-muted/20 dark:bg-zinc-800/10 hover:bg-brand-muted/50 dark:hover:bg-zinc-800/20'
        )}
      >
        <div className="flex items-center justify-between mb-0.5">
          <span className={cn(
            "text-[9px] font-black uppercase tracking-tight truncate flex-1",
            isSelected ? 'text-brand-goldink dark:text-brand-accent' : 'text-brand-dark/40 dark:text-white/40'
          )}>
            {theme.badge}
          </span>
          {isSelected && <div className="w-1.5 h-1.5 rounded-full bg-brand-accent animate-pulse shrink-0 ml-1" />}
          {locked && <Lock className="size-2 text-brand-dark/40 dark:text-white/40 shrink-0 ml-1" />}
        </div>
        <span className="text-xs font-bold text-brand-dark dark:text-white block leading-none mb-0.5 truncate">
          {displayLabel}
        </span>
        <span className="text-[11px] text-brand-dark/40 dark:text-white/45 uppercase font-bold block leading-none truncate">
          {theme.subBadge}
        </span>
      </button>
    );
  };

  return (
    <div className="flex flex-col gap-3">
      {/* Title */}
      <div className="flex items-center justify-between">
        <label className="text-[10px] font-bold uppercase tracking-[0.2em] text-brand-dark/40 dark:text-white/45">
          {t('dashboard.translate.provider.sectionTitle')}
        </label>
      </div>

      {/* Tabs Container */}
      <div className="grid grid-cols-2 p-1 bg-brand-muted/70 dark:bg-zinc-800/40 rounded-xl border border-brand-dark/5 dark:border-white/5">
        <button
          type="button"
          onClick={() => setActiveTab('classic')}
          className={cn(
            'py-1.5 text-xs font-semibold rounded-lg transition-all duration-200',
            activeTab === 'classic'
              ? 'bg-white dark:bg-zinc-900 text-brand-dark dark:text-white shadow-sm'
              : 'text-brand-dark/50 dark:text-white/40 hover:text-brand-dark dark:hover:text-white'
          )}
        >
          {t('dashboard.translate.provider.tabStandard')}
        </button>
        <button
          type="button"
          onClick={() => setActiveTab('llm')}
          className={cn(
            'py-1.5 text-xs font-semibold rounded-lg transition-all duration-200 flex items-center justify-center gap-1.5',
            activeTab === 'llm'
              ? 'bg-white dark:bg-zinc-900 text-brand-dark dark:text-white shadow-sm'
              : 'text-brand-dark/50 dark:text-white/40 hover:text-brand-dark dark:hover:text-white'
          )}
        >
          <Sparkles className={cn("size-3", activeTab === 'llm' ? 'text-brand-goldink dark:text-brand-accent' : 'text-brand-dark/35 dark:text-white/30')} />
          {t('dashboard.translate.provider.tabLLM')}
          {!isPro && <Lock className="size-2.5 opacity-60 ml-0.5" />}
        </button>
      </div>

      {/* Active Tab List */}
      <div className="space-y-2">
        {activeTab === 'classic' ? (
          classicProviders.length > 0 ? (
            <div className="grid grid-cols-2 gap-2">
              {classicProviders.map((p) => renderClassicCard(p))}
            </div>
          ) : (
            <p className="text-xs text-brand-dark/40 dark:text-white/30 text-center py-4 border border-dashed border-brand-dark/10 dark:border-white/10 rounded-xl font-light">
              {t('providerSelector.noClassic')}
            </p>
          )
        ) : (
          llmProviders.length > 0 ? (
            <div className="grid grid-cols-3 gap-2">
              {llmProviders.map((p) => renderLlmCard(p, !isPro))}
            </div>
          ) : (
            <p className="text-xs text-brand-dark/40 dark:text-white/30 text-center py-4 border border-dashed border-brand-dark/10 dark:border-white/10 rounded-xl font-light">
              {t('providerSelector.noLlm')}
            </p>
          )
        )}

        {/* Dynamic Contextual Help Area */}
        {provider && (
          <div className="mt-1 p-2.5 rounded-lg bg-brand-muted/20 dark:bg-white/[0.01] border border-black/[0.03] dark:border-white/[0.03]">
            {activeTab === 'classic' ? (
              (() => {
                const activeP = classicProviders.find(p => p.id === provider);
                const meta = activeP ? CLASSIC_THEMES[activeP.id] : null;
                return activeP ? (
                  <p className="text-[10px] text-brand-dark/60 dark:text-white/60 leading-normal font-light">
                    <span className="font-bold text-brand-dark dark:text-white">{meta?.labelOverride || activeP.label} : </span>
                    {meta?.descriptionOverride || activeP.description}
                    <span className="block mt-1 font-bold text-brand-goldink dark:text-brand-accent">{t('providerSelector.costOne')}</span>
                  </p>
                ) : null;
              })()
            ) : (
              (() => {
                const activeP = llmProviders.find(p => p.id === provider);
                const theme = activeP ? (LLM_THEMES[activeP.id] || DEFAULT_LLM_THEME) : null;
                return activeP && theme ? (
                  <p className="text-[10px] text-brand-dark/60 dark:text-white/60 leading-normal font-light">
                    <span className="font-bold text-brand-dark dark:text-white">{activeP.label} : </span>
                    {theme.descriptionOverride} <span className="opacity-50 text-[9px] font-mono">({activeP.model || 'default'})</span>
                    <span className="block mt-1 font-bold text-brand-goldink dark:text-brand-accent">
                      {activeP.id === 'openrouter_premium' ? t('providerSelector.costFive') : t('providerSelector.costOne')}
                    </span>
                  </p>
                ) : null;
              })()
            )}
          </div>
        )}
      </div>

      {/* Pro upgrade banner when llm is active and user is not pro */}
      {!isPro && activeTab === 'llm' && (
        <div className="p-3.5 rounded-xl border border-brand-accent/20 bg-brand-accent/[0.02] dark:bg-brand-accent/[0.01] text-center flex flex-col items-center gap-1.5">
          <Sparkles className="size-3.5 text-brand-goldink dark:text-brand-accent animate-pulse" />
          <span className="text-[11px] font-bold text-brand-dark dark:text-white leading-none">
            {t('dashboard.translate.provider.llmDivider')}
          </span>
          <p className="text-[9.5px] text-brand-dark/50 dark:text-white/50 leading-relaxed font-light">
            {t('providerSelector.unlockContextual')}
          </p>
          <a
            href="/pricing"
            className="w-full inline-flex items-center justify-center py-1.5 rounded-lg bg-brand-dark dark:bg-white text-white dark:text-brand-dark text-[10px] font-semibold hover:opacity-95 active:scale-[0.98] transition-all shadow-sm"
          >
            {t('dashboard.translate.provider.upgrade')}
          </a>
        </div>
      )}
    </div>
  );
}
