import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { Cpu, Sparkles, Zap, MessageSquare, ArrowLeft, Check, Layers, Search, ShieldCheck } from 'lucide-react';
import modelsData from '../data/models.json';

interface ModelsProps {
  onOpenAuth?: () => void;
}

export const Models: React.FC<ModelsProps> = ({ onOpenAuth }) => {
  const [selectedProvider, setSelectedProvider] = useState<string>('all');
  const [searchQuery, setSearchQuery] = useState<string>('');

  const providers = [
    { id: 'all', name: 'همه شرکت‌ها' },
    { id: 'OpenAI', name: 'OpenAI (GPT & O1)' },
    { id: 'Anthropic', name: 'Anthropic (Claude)' },
    { id: 'Google', name: 'Google (Gemini)' },
    { id: 'DeepSeek', name: 'DeepSeek (V3 & R1)' },
  ];

  const filteredModels = modelsData.filter(m => {
    const matchProv = selectedProvider === 'all' || m.provider.toLowerCase().includes(selectedProvider.toLowerCase());
    const q = searchQuery.toLowerCase();
    const matchSearch = !q || m.name.toLowerCase().includes(q) || m.description.toLowerCase().includes(q);
    return matchProv && matchSearch;
  });

  return (
    <div className="pt-28 pb-20 bg-slate-950 min-h-screen">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 relative z-10">
        
        {/* Header matching Donya Theme */}
        <div className="donya-section-header">
          <div className="inline-flex items-center gap-2 px-3.5 py-1.5 rounded-full bg-indigo-500/10 text-indigo-300 border border-indigo-500/20 text-xs font-bold mb-4">
            <Cpu className="w-3.5 h-3.5" />
            <span>پیشرفته‌ترین مدل‌های هوش مصنوعی جهان</span>
          </div>
          <h1 className="donya-section-title">
            <Sparkles className="w-7 h-7 text-indigo-400" />
            <span>کاتالوگ مدل‌های هوش مصنوعی هوشان</span>
          </h1>
          <div className="donya-divider">
            <span className="donya-divider-line"></span>
            <span className="donya-divider-icon"></span>
            <span className="donya-divider-line"></span>
          </div>
          <p className="text-slate-400 text-sm sm:text-base max-w-2xl mx-auto mt-3">
            دسترسی بدون محدودیت به قوی‌ترین مدل‌های زبانی، استدلالی و بینایی با پرداخت ریالی
          </p>
        </div>

        {/* Filters */}
        <div className="glass-panel p-4 sm:p-6 rounded-2xl border border-white/5 mb-8 flex flex-col sm:flex-row items-center justify-between gap-4">
          <div className="flex flex-wrap gap-2 w-full sm:w-auto">
            {providers.map(p => (
              <button
                key={p.id}
                onClick={() => setSelectedProvider(p.id)}
                className={`px-4 py-2 rounded-xl text-xs sm:text-sm font-bold transition-all ${
                  selectedProvider === p.id
                    ? 'btn-donya-primary text-white shadow-md'
                    : 'bg-slate-900/60 text-slate-400 hover:text-white border border-white/5'
                }`}
              >
                {p.name}
              </button>
            ))}
          </div>

          <div className="relative w-full sm:w-72">
            <input
              type="text"
              placeholder="جستجو در مدل‌ها..."
              value={searchQuery}
              onChange={(e) => setSearchQuery(e.target.value)}
              className="w-full bg-slate-900/90 border border-white/10 rounded-xl pr-10 pl-4 py-2.5 text-xs sm:text-sm text-white focus:outline-none focus:border-indigo-500"
            />
            <Search className="absolute right-3.5 top-3 w-4 h-4 text-slate-500" />
          </div>
        </div>

        {/* Models Grid (Elementor Page #4354) */}
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-16">
          {filteredModels.map((model) => (
            <div
              key={model.id}
              className="glass-panel rounded-2xl p-6 flex flex-col justify-between hover:border-indigo-500/40 transition-all duration-300 hover:-translate-y-1 shadow-xl group border border-white/5"
            >
              <div>
                <div className="flex items-center justify-between mb-4">
                  <div className="flex items-center gap-2">
                    {model.provider === 'OpenAI' && <img src="/storage/bot/llm/gpt.png" alt="OpenAI" className="w-6 h-6 object-contain" />}
                    {model.provider === 'Anthropic' && <img src="/storage/bot/llm/claude.png" alt="Anthropic" className="w-6 h-6 object-contain" />}
                    {model.provider === 'Google' && <img src="/storage/bot/llm/gemini.png" alt="Google" className="w-6 h-6 object-contain" />}
                    {model.provider === 'DeepSeek' && <img src="/storage/bot/llm/deepseek.png" alt="DeepSeek" className="w-6 h-6 object-contain" />}
                    <span className="px-2.5 py-0.5 rounded-lg text-xs font-bold bg-indigo-500/20 text-indigo-300 border border-indigo-500/30">
                      {model.provider}
                    </span>
                  </div>
                  <span className="px-2.5 py-0.5 rounded text-[11px] font-bold bg-cyan-500/10 text-cyan-300 border border-cyan-500/20">
                    {model.badge || model.provider}
                  </span>
                </div>

                <h3 className="text-xl font-extrabold text-white mb-2 group-hover:text-indigo-300 transition-colors">
                  {model.name}
                </h3>
                <p className="text-xs sm:text-sm text-slate-400 leading-relaxed mb-6">
                  {model.description}
                </p>

                <div className="space-y-2 mb-6 border-t border-white/5 pt-4 text-xs text-slate-300">
                  {model.strengths.slice(0, 3).map((st, sidx) => (
                    <div key={sidx} className="flex items-center gap-2">
                      <Check className="w-3.5 h-3.5 text-emerald-400 shrink-0" />
                      <span>{st}</span>
                    </div>
                  ))}
                </div>
              </div>

              <div className="pt-4 border-t border-white/5 flex items-center justify-between">
                <span className="text-xs text-slate-400">
                  کانتکست: <strong className="text-white">{model.contextWindow}</strong>
                </span>
                <Link
                  to={`/chat?model=${encodeURIComponent(model.name)}`}
                  className="btn-donya-primary px-4 py-2 rounded-xl text-xs font-bold flex items-center gap-1.5"
                >
                  <MessageSquare className="w-3.5 h-3.5" />
                  <span>استفاده از مدل</span>
                </Link>
              </div>
            </div>
          ))}
        </div>

      </div>
    </div>
  );
};
