import React, { useState, useEffect, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { motion, AnimatePresence } from 'framer-motion';
import { BookOpen, FileText, Layers, Loader2, Plus, Search, Trash2, Edit2, AlertCircle, Check, Clock, XCircle } from 'lucide-react';
import { apiClient } from '../../services/apiClient';
import { templateService } from '../../services/templateService';
import { AssessmentTemplate } from '../../types';
import { useToast } from '../../contexts/ToastContext';
import { useConfirm } from '../../contexts/ConfirmContext';
import { useLanguage } from '../../contexts/LanguageContext';

interface QuestionBankViewProps {
  isAdmin?: boolean;
}

interface QuestionBankItem {
  id: string;
  name: string;
  description?: string;
  status: string;
  templateId?: string;
  createdAt: string;
}

type StatusFilter = 'ALL' | 'DRAFT' | 'PENDING_REVIEW' | 'PUBLISHED';

export default function QuestionBankView({ isAdmin: _isAdmin }: QuestionBankViewProps) {
  const navigate = useNavigate();
  const { t } = useLanguage();
  const { showSuccess, showError } = useToast();
  const { confirm } = useConfirm();

  const [banks, setBanks] = useState<QuestionBankItem[]>([]);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState('');
  const [showDrawer, setShowDrawer] = useState(false);
  const [formData, setFormData] = useState({ name: '', description: '', templateId: '' });
  const [saving, setSaving] = useState(false);
  const [templates, setTemplates] = useState<AssessmentTemplate[]>([]);
  const [loadingTemplates, setLoadingTemplates] = useState(false);
  const [deletingId, setDeletingId] = useState<string | null>(null);
  const [statusFilter, setStatusFilter] = useState<StatusFilter>('ALL');
  const [searchQuery, setSearchQuery] = useState('');

  useEffect(() => { fetchData(); }, []);

  const fetchData = async () => {
    try {
      setLoading(true);
      const res = await apiClient.request('/question-banks', {});
      if (!res.ok) throw new Error(res.status.toString());
      const data = await res.json();
      setBanks(Array.isArray(data) ? data : (data.data || []));
    } catch (err: any) {
      setError(err.message || t('actionFailed'));
      showError(err.message || t('actionFailed'));
    } finally {
      setLoading(false);
    }
  };

  const openDrawer = () => {
    setFormData({ name: '', description: '', templateId: '' });
    setLoadingTemplates(true);
    templateService.getAll()
      .then(data => setTemplates(data))
      .catch(() => showError(t('actionFailed')))
      .finally(() => setLoadingTemplates(false));
    setShowDrawer(true);
  };

  const handleCreate = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!formData.name.trim()) return;
    setSaving(true);
    try {
      const payload: any = { name: formData.name, description: formData.description };
      if (formData.templateId) payload.templateId = formData.templateId;
      const res = await apiClient.request('/question-banks', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload),
      });
      if (!res.ok) {
        const errBody = await res.text().catch(() => '');
        let msg = res.status.toString();
        try { const parsed = JSON.parse(errBody); if (parsed.message) msg = parsed.message; } catch {}
        throw new Error(msg);
      }
      setShowDrawer(false);
      showSuccess(t('questionBankCreated'));
      fetchData();
    } catch (err: any) {
      showError(err.message || t('actionFailed'));
    } finally {
      setSaving(false);
    }
  };

  const handleDelete = async (e: React.MouseEvent, bankId: string, bankName: string) => {
    e.stopPropagation();
    const ok = await confirm({ message: t('confirmDeleteBank').replace('$1', bankName), confirmLabel: t('delete'), cancelLabel: t('cancel') });
    if (!ok) return;
    setDeletingId(bankId);
    try {
      const res = await apiClient.request(`/question-banks/${bankId}`, { method: 'DELETE' });
      if (!res.ok) {
        const errBody = await res.text().catch(() => '');
        let msg = res.status.toString();
        try { const parsed = JSON.parse(errBody); if (parsed.message) msg = parsed.message; } catch {}
        throw new Error(msg);
      }
      showSuccess(t('confirm'));
      fetchData();
    } catch (err: any) {
      showError(err.message || t('questionBankDeleteFailed'));
    } finally {
      setDeletingId(null);
    }
  };

  const handleCardClick = (bank: QuestionBankItem) => {
    navigate(`/question-banks/${bank.id}`);
  };

  const filteredBanks = useMemo(() => {
    let result = banks;
    if (statusFilter !== 'ALL') result = result.filter(b => b.status === statusFilter);
    if (searchQuery.trim()) {
      const q = searchQuery.toLowerCase();
      result = result.filter(b => b.name.toLowerCase().includes(q) || (b.description || '').toLowerCase().includes(q));
    }
    return result;
  }, [banks, statusFilter, searchQuery]);

  const STATUS_TABS: { key: StatusFilter; label: string; icon: React.ReactNode; count: (b: QuestionBankItem[]) => number }[] = [
    { key: 'ALL', label: t('all'), icon: <Layers size={14} />, count: (b) => b.length },
    { key: 'PUBLISHED', label: t('published'), icon: <Check size={14} />, count: (b) => b.filter(i => i.status === 'PUBLISHED').length },
    { key: 'DRAFT', label: t('draft'), icon: <FileText size={14} />, count: (b) => b.filter(i => i.status === 'DRAFT').length },
    { key: 'PENDING_REVIEW', label: t('pendingReview'), icon: <Clock size={14} />, count: (b) => b.filter(i => i.status === 'PENDING_REVIEW').length },
  ];

  const stats = useMemo(() => ({
    total: banks.length,
    published: banks.filter(b => b.status === 'PUBLISHED').length,
    draft: banks.filter(b => b.status === 'DRAFT').length,
    pending: banks.filter(b => b.status === 'PENDING_REVIEW').length,
  }), [banks]);

  const statusLabels: Record<string, string> = {
    PUBLISHED: t('published'),
    PENDING_REVIEW: t('pendingReview'),
    REJECTED: t('rejected'),
    DRAFT: t('draft'),
  };

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-black text-slate-900">{t('questionBankManagement')}</h1>
          <p className="text-sm text-slate-500 mt-1">{t('questionBankManagementDesc')}</p>
        </div>
        <button
          onClick={openDrawer}
          className="px-5 py-3 bg-blue-600 text-white rounded-2xl text-sm font-black uppercase tracking-widest flex items-center gap-2 shadow-lg shadow-blue-600/20 hover:bg-blue-700 transition-all active:scale-[0.98]"
        >
          <Plus size={18} />
          {t('createQuestionBank')}
        </button>
      </div>

      {!loading && !error && banks.length > 0 && (
        <div className="grid grid-cols-4 gap-4">
          {[
            { label: t('totalBanks'), value: stats.total, color: 'bg-slate-50 border-slate-200 text-slate-700', icon: <Layers size={16} className="text-slate-500" /> },
            { label: t('published'), value: stats.published, color: 'bg-emerald-50 border-emerald-200/50 text-emerald-700', icon: <Check size={16} className="text-emerald-500" /> },
            { label: t('draft'), value: stats.draft, color: 'bg-slate-50 border-slate-200 text-slate-700', icon: <FileText size={16} className="text-slate-500" /> },
            { label: t('pendingReview'), value: stats.pending, color: 'bg-amber-50 border-amber-200/50 text-amber-700', icon: <Clock size={16} className="text-amber-500" /> },
          ].map((stat, i) => (
            <motion.div key={stat.label} initial={{ opacity: 0, y: 10 }} animate={{ opacity: 1, y: 0 }} transition={{ delay: i * 0.05 }}
              className={`${stat.color} rounded-2xl border p-4`}>
              <div className="flex items-center justify-between mb-1">
                <span className="text-[10px] font-black uppercase tracking-widest opacity-70">{stat.label}</span>
                {stat.icon}
              </div>
              <div className="text-2xl font-black">{stat.value}</div>
            </motion.div>
          ))}
        </div>
      )}

      {!loading && !error && banks.length > 0 && (
        <div className="flex items-center gap-4">
          <div className="relative flex-1 max-w-sm">
            <Search size={16} className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400" />
            <input
              type="text"
              value={searchQuery}
              onChange={(e) => setSearchQuery(e.target.value)}
              placeholder={t('searchQuestionBanksPlaceholder')}
              className="w-full pl-10 pr-4 py-3 bg-slate-50 border border-slate-200 rounded-2xl text-sm font-bold focus:ring-4 focus:ring-blue-500/10 focus:border-blue-500/50 outline-none transition-all placeholder:text-slate-300"
            />
          </div>
          <div className="flex gap-1 bg-slate-50 rounded-2xl p-1 border border-slate-200">
            {STATUS_TABS.map((tab) => {
              const active = statusFilter === tab.key;
              return (
                <button
                  key={tab.key}
                  onClick={() => setStatusFilter(tab.key)}
                  className={`flex items-center gap-1.5 px-4 py-2 rounded-xl text-xs font-bold transition-all ${
                    active
                      ? 'bg-white text-slate-900 shadow-sm border border-slate-200/50'
                      : 'text-slate-500 hover:text-slate-700'
                  }`}
                >
                  {tab.icon}
                  {tab.label}
                  <span className={`${active ? 'bg-slate-100 text-slate-600' : 'bg-white/50 text-slate-400'} px-1.5 py-0.5 rounded-lg text-[10px] font-black`}>
                    {tab.count(banks)}
                  </span>
                </button>
              );
            })}
          </div>
        </div>
      )}

      {loading ? (
        <div className="flex items-center justify-center py-20">
          <Loader2 className="w-8 h-8 animate-spin text-blue-600 opacity-20" />
        </div>
      ) : error ? (
        <div className="flex items-center gap-3 text-red-500 bg-red-50 rounded-2xl p-6 border border-red-100">
          <AlertCircle size={20} />
          <span className="text-sm font-bold">{t('actionFailed')}</span>
          <button onClick={fetchData} className="ml-auto text-xs font-black text-red-600 hover:text-red-700 uppercase tracking-widest">{t('retry')}</button>
        </div>
      ) : banks.length === 0 ? (
        <div className="bg-slate-50 rounded-[2rem] border-2 border-dashed border-slate-200 p-20 text-center">
          <div className="w-16 h-16 bg-slate-100 rounded-3xl flex items-center justify-center mx-auto mb-6">
            <BookOpen size={32} className="text-slate-300" />
          </div>
          <p className="text-slate-400 font-black uppercase tracking-widest text-xs mb-2">{t('noQuestionBanks')}</p>
          <p className="text-slate-300 text-xs mb-6">{t('createFirstBank')}</p>
          <button onClick={openDrawer} className="inline-flex items-center gap-2 px-6 py-3 bg-blue-600 text-white rounded-2xl text-sm font-black uppercase tracking-widest hover:bg-blue-700 transition-all active:scale-[0.98] shadow-lg shadow-blue-600/20">
            <Plus size={18} /> {t('createQuestionBank')}
          </button>
        </div>
      ) : filteredBanks.length === 0 ? (
        <div className="bg-slate-50 rounded-[2rem] border-2 border-dashed border-slate-200 p-20 text-center">
          <Search size={32} className="text-slate-300 mx-auto mb-4" />
          <p className="text-slate-400 font-bold text-xs uppercase tracking-widest">{t('noMatchingQuestionBanks')}</p>
          <p className="text-slate-300 text-xs mt-2">{t('tryChangingFilter')}</p>
        </div>
      ) : (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          <AnimatePresence mode="popLayout">
            {filteredBanks.map((bank) => (
              <motion.div
                key={bank.id}
                layout
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{ opacity: 1, scale: 1 }}
                exit={{ opacity: 0, scale: 0.95 }}
                onClick={() => handleCardClick(bank)}
                className="bg-white border border-slate-200 rounded-3xl p-5 shadow-sm hover:shadow-md transition-all cursor-pointer group relative overflow-hidden"
              >
                <div className={`absolute top-0 right-0 w-32 h-32 rounded-full blur-3xl -mr-16 -mt-16 ${
                  bank.status === 'PUBLISHED' ? 'bg-emerald-500/5' :
                  bank.status === 'PENDING_REVIEW' ? 'bg-amber-500/5' :
                  bank.status === 'REJECTED' ? 'bg-red-500/5' : 'bg-blue-500/5'
                }`} />

                <div className="relative z-10">
                  <div className="flex items-start justify-between mb-3">
                    <h3 className="font-black text-base text-slate-900 pr-8 line-clamp-1">{bank.name}</h3>
                    <div className="flex gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity absolute top-0 right-0">
                      <button onClick={(e) => { e.stopPropagation(); handleCardClick(bank); }}
                        className="p-1.5 text-slate-400 hover:text-blue-600 hover:bg-blue-50 rounded-xl transition-all" title={t('edit')}>
                        <Edit2 size={13} />
                      </button>
                      <button onClick={(e) => handleDelete(e, bank.id, bank.name)} disabled={deletingId === bank.id}
                        className="p-1.5 text-slate-400 hover:text-red-600 hover:bg-red-50 rounded-xl transition-all disabled:opacity-50" title={t('delete')}>
                        {deletingId === bank.id ? <Loader2 size={13} className="animate-spin" /> : <Trash2 size={13} />}
                      </button>
                    </div>
                  </div>

                  <p className="text-xs text-slate-500 mb-4 line-clamp-2 h-8">{bank.description || t('noDescription')}</p>

                  <div className="flex items-center justify-between pt-3 border-t border-slate-50">
                    <span className={`px-2.5 py-1 text-[10px] font-black uppercase tracking-widest rounded-full border ${
                      bank.status === 'PUBLISHED' ? 'bg-emerald-50 text-emerald-600 border-emerald-200/50' :
                      bank.status === 'PENDING_REVIEW' ? 'bg-amber-50 text-amber-600 border-amber-200/50' :
                      bank.status === 'REJECTED' ? 'bg-red-50 text-red-500 border-red-200/50' :
                      'bg-slate-50 text-slate-500 border-slate-200/50'
                    }`}>
                      {statusLabels[bank.status] || bank.status}
                    </span>
                    <span className="text-[10px] text-slate-400 font-medium">
                      {new Date(bank.createdAt).toLocaleDateString('zh-CN')}
                    </span>
                  </div>
                </div>
              </motion.div>
            ))}
          </AnimatePresence>
        </div>
      )}

      <AnimatePresence>
        {showDrawer && (
          <>
            <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}
              onClick={() => setShowDrawer(false)} className="fixed inset-0 bg-slate-900/40 backdrop-blur-sm z-40" />
            <motion.div initial={{ x: '100%' }} animate={{ x: 0 }} exit={{ x: '100%' }}
              transition={{ type: 'spring', damping: 25, stiffness: 200 }}
              className="fixed right-0 top-0 h-full w-full max-w-md bg-white shadow-2xl z-50 flex flex-col">
              <div className="flex items-center justify-between px-6 py-4 border-b border-slate-100">
                <div className="flex items-center gap-3">
                  <div className="w-10 h-10 bg-blue-50 text-blue-600 rounded-2xl flex items-center justify-center"><Plus size={22} /></div>
                  <h2 className="text-lg font-black text-slate-900">{t('createQuestionBank')}</h2>
                </div>
                <button onClick={() => setShowDrawer(false)} className="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-50 rounded-xl transition-all"><XCircle size={22} /></button>
              </div>
              <div className="flex-1 overflow-y-auto p-6">
                <form id="create-form" onSubmit={handleCreate} className="space-y-5">
                  <div className="space-y-1.5">
                    <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest px-1 ml-1 flex items-center gap-2">
                      <BookOpen size={12} className="text-blue-500" /> {t('name')} <span className="text-red-500">*</span>
                    </label>
                    <input type="text" value={formData.name} onChange={(e) => setFormData({ ...formData, name: e.target.value })}
                      className="w-full px-5 py-4 bg-slate-50 border border-slate-200 rounded-[1.25rem] text-sm font-bold focus:ring-4 focus:ring-blue-500/10 focus:border-blue-500/50 outline-none transition-all placeholder:text-slate-300"
                      placeholder={t('name')} required autoFocus />
                  </div>
                  <div className="space-y-1.5">
                    <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest px-1 ml-1 flex items-center gap-2">
                      <FileText size={12} className="text-blue-500" /> {t('description')}
                    </label>
                    <input type="text" value={formData.description} onChange={(e) => setFormData({ ...formData, description: e.target.value })}
                      className="w-full px-5 py-4 bg-slate-50 border border-slate-200 rounded-[1.25rem] text-sm font-bold focus:ring-4 focus:ring-blue-500/10 focus:border-blue-500/50 outline-none transition-all placeholder:text-slate-300"
                      placeholder={t('description')} />
                  </div>
                  <div className="space-y-1.5">
                    <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest px-1 ml-1 flex items-center gap-2">
                      <Layers size={12} className="text-blue-500" /> {t('linkTemplate')}
                    </label>
                    <select value={formData.templateId} onChange={(e) => setFormData({ ...formData, templateId: e.target.value })}
                      className="w-full px-5 py-4 bg-slate-50 border border-slate-200 rounded-[1.25rem] text-sm font-bold focus:ring-4 focus:ring-blue-500/10 focus:border-blue-500/50 outline-none transition-all cursor-pointer"
                      disabled={loadingTemplates}>
                      <option value="">{t('noTemplate')}</option>
                      {templates.map((t) => <option key={t.id} value={t.id}>{t.name}</option>)}
                    </select>
                    {loadingTemplates && (
                      <div className="flex items-center gap-2 px-2 py-1">
                        <Loader2 size={12} className="animate-spin text-slate-400" />
                        <span className="text-[10px] text-slate-400 font-medium">{t('loading')}</span>
                      </div>
                    )}
                  </div>
                </form>
              </div>
              <div className="p-6 border-t border-slate-100">
                <button type="submit" form="create-form" disabled={saving || !formData.name.trim()}
                  className="w-full flex items-center justify-center gap-2 px-6 py-4 bg-blue-600 text-white font-black uppercase tracking-widest text-xs rounded-[1.25rem] hover:bg-blue-700 active:scale-[0.98] transition-all disabled:opacity-50 disabled:cursor-not-allowed shadow-xl shadow-blue-100">
                  {saving ? <Loader2 size={18} className="animate-spin" /> : <Plus size={18} />}
                  {saving ? t('creating') : t('createQuestionBank')}
                </button>
              </div>
            </motion.div>
          </>
        )}
      </AnimatePresence>
    </div>
  );
}
