import React, { useState, useMemo } from 'react';
import { SALES_ASSETS } from './constants';
import { SalesAsset } from './types';
import SalesCard from './components/SalesCard';
import Assistant from './components/Assistant';
import AssetDetailModal from './components/AssetDetailModal';
import { LayoutGrid, Menu, Bell, Search, ShieldCheck, CheckCircle2 } from 'lucide-react';

const App: React.FC = () => {
  const [activeFilter, setActiveFilter] = useState('All');
  const [selectedAsset, setSelectedAsset] = useState<SalesAsset | null>(null);
  const [showToast, setShowToast] = useState(false);

  // Filter Logic
  const filteredAssets = useMemo(() => {
    if (activeFilter === 'All') return SALES_ASSETS;
    
    return SALES_ASSETS.filter(asset => {
      if (activeFilter === 'CEO Logic') return asset.tags.some(tag => ['Concept', 'Pitch', 'Pain Points', 'Solution', 'Closing'].includes(tag));
      if (activeFilter === 'Legal/Tax') return asset.tags.some(tag => ['Legal', 'Benefit', 'Risk', 'Finance'].includes(tag));
      if (activeFilter === 'Demo') return asset.tags.some(tag => ['Demo', 'Dashboard', 'Video', 'Mobile', 'UX', 'Infra'].includes(tag));
      return true;
    });
  }, [activeFilter]);

  const handleDownload = () => {
    setShowToast(true);
    setTimeout(() => setShowToast(false), 3000);
  };

  return (
    <div className="min-h-screen bg-slate-50 text-slate-900 font-sans selection:bg-brand-200 selection:text-brand-900">
      
      {/* Toast Notification */}
      <div 
        className={`fixed top-24 right-4 z-[60] bg-slate-800 text-white px-6 py-4 rounded-xl shadow-2xl flex items-center gap-3 transition-all duration-500 transform ${showToast ? 'translate-x-0 opacity-100' : 'translate-x-full opacity-0'}`}
      >
        <CheckCircle2 className="text-green-400" size={24} />
        <div>
          <h4 className="font-bold text-sm">Download Started</h4>
          <p className="text-slate-400 text-xs">CodeBridgeX_Proposal_v2.4.pdf</p>
        </div>
      </div>

      {/* Navbar */}
      <nav className="sticky top-0 z-30 bg-white/80 backdrop-blur-md border-b border-slate-200">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="flex justify-between items-center h-16">
            <div className="flex items-center gap-3 cursor-pointer" onClick={() => setActiveFilter('All')}>
              <div className="w-8 h-8 bg-brand-600 rounded-lg flex items-center justify-center text-white font-bold text-lg shadow-lg shadow-brand-200">
                S
              </div>
              <span className="text-xl font-bold tracking-tight text-slate-900">CodeBridgeX <span className="text-brand-600">SAM</span></span>
            </div>
            <div className="flex items-center gap-4">
              <button className="p-2 text-slate-500 hover:text-brand-600 transition-colors hidden sm:block">
                <Search size={20} />
              </button>
              <button className="p-2 text-slate-500 hover:text-brand-600 transition-colors relative">
                <Bell size={20} />
                <span className="absolute top-2 right-2 w-2 h-2 bg-red-500 rounded-full border border-white"></span>
              </button>
              <button className="p-2 text-slate-500 hover:text-slate-900 sm:hidden">
                <Menu size={24} />
              </button>
              <div className="hidden sm:flex items-center gap-2 ml-2">
                <div className="w-8 h-8 rounded-full bg-slate-200 flex items-center justify-center text-slate-500 font-bold border border-slate-300">
                    S
                </div>
                <span className="text-sm font-medium text-slate-700">영업팀 관리자</span>
              </div>
            </div>
          </div>
        </div>
      </nav>

      {/* Hero Section */}
      <header className="relative bg-white pt-16 pb-20 lg:pt-24 lg:pb-28 overflow-hidden">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 relative z-10">
          <div className="text-center max-w-4xl mx-auto">
            <div className="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-brand-50 text-brand-700 text-xs font-semibold uppercase tracking-wider mb-6 animate-fade-in-up">
              <ShieldCheck size={14} />
              CEO Management Solution
            </div>
            <h1 className="text-4xl sm:text-5xl lg:text-6xl font-extrabold text-slate-900 tracking-tight mb-6 animate-fade-in-up delay-100">
              직원의 관리 도구가 아닙니다.<br />
              <span className="text-transparent bg-clip-text bg-gradient-to-r from-brand-600 to-indigo-600">
                대표님의 경영 무기입니다.
              </span>
            </h1>
            <p className="text-lg sm:text-xl text-slate-500 mb-10 leading-relaxed max-w-2xl mx-auto animate-fade-in-up delay-200">
              "SAM"은 단순한 ERP가 아닙니다. <br className="hidden sm:block"/>
              가지급금 이자 계산부터 채권 추심, 실시간 경영 알림까지.<br />
              <strong>오직 CEO를 위한 시크릿 대시보드</strong>를 제안하십시오.
            </p>
            <div className="flex justify-center gap-4 animate-fade-in-up delay-300">
              <button 
                onClick={handleDownload}
                className="px-8 py-3 rounded-xl bg-slate-900 text-white font-semibold hover:bg-slate-800 hover:shadow-lg transition-all transform hover:-translate-y-1 active:scale-95"
              >
                제안서 다운로드
              </button>
              <button 
                onClick={() => setActiveFilter('Demo')}
                className="px-8 py-3 rounded-xl bg-white text-slate-700 border border-slate-200 font-semibold hover:bg-slate-50 hover:border-slate-300 transition-all flex items-center gap-2 active:scale-95"
              >
                <LayoutGrid size={18} />
                세일즈 덱 보기
              </button>
            </div>
          </div>
        </div>
        
        {/* Background Decorations */}
        <div className="absolute top-0 left-1/2 -translate-x-1/2 w-full h-full z-0 pointer-events-none">
          <div className="absolute top-20 left-10 w-72 h-72 bg-brand-200 rounded-full mix-blend-multiply filter blur-3xl opacity-30 animate-blob"></div>
          <div className="absolute top-20 right-10 w-72 h-72 bg-indigo-200 rounded-full mix-blend-multiply filter blur-3xl opacity-30 animate-blob animation-delay-2000"></div>
          <div className="absolute -bottom-8 left-1/2 w-72 h-72 bg-pink-200 rounded-full mix-blend-multiply filter blur-3xl opacity-30 animate-blob animation-delay-4000"></div>
        </div>
      </header>

      {/* Main Grid Content */}
      <main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <div className="flex items-center justify-between mb-8">
          <h2 className="text-2xl font-bold text-slate-900">Sales Materials</h2>
          <div className="flex gap-2">
            {['All', 'CEO Logic', 'Legal/Tax', 'Demo'].map((filter) => (
              <button 
                key={filter} 
                onClick={() => setActiveFilter(filter)}
                className={`px-4 py-2 rounded-lg text-sm font-medium transition-colors duration-200 ${
                  activeFilter === filter 
                  ? 'bg-slate-900 text-white shadow-md' 
                  : 'bg-white text-slate-600 hover:bg-slate-100 border border-transparent hover:border-slate-200'
                }`}
              >
                {filter}
              </button>
            ))}
          </div>
        </div>

        {/* Masonry-like Grid */}
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6 auto-rows-[minmax(200px,auto)]">
          {filteredAssets.map((asset) => (
            <SalesCard 
              key={asset.id} 
              asset={asset} 
              onClick={(a) => setSelectedAsset(a)}
            />
          ))}
          {filteredAssets.length === 0 && (
             <div className="col-span-3 text-center py-20 text-slate-400">
               해당 카테고리에 자료가 없습니다.
             </div>
          )}
        </div>
      </main>

      {/* Footer */}
      <footer className="bg-white border-t border-slate-200 py-12 mt-12">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 flex flex-col md:flex-row justify-between items-center gap-6">
          <p className="text-slate-500 text-sm">© 2024 CodeBridgeX Corp. Sales Enablement Portal.</p>
          <div className="flex gap-6">
            <a href="#" className="text-slate-400 hover:text-slate-600">Internal Use Only</a>
            <a href="#" className="text-slate-400 hover:text-slate-600">Sales Script</a>
            <a href="#" className="text-slate-400 hover:text-slate-600">Support</a>
          </div>
        </div>
      </footer>

      {/* Detail Modal */}
      {selectedAsset && (
        <AssetDetailModal 
          asset={selectedAsset} 
          onClose={() => setSelectedAsset(null)} 
        />
      )}

      {/* AI Assistant */}
      <Assistant />
    </div>
  );
};

export default App;
