import { useState } from 'react';
import { useScraperStore, ScraperJob, Session } from '../stores/scraperStore';
import { 
  Activity, Key, Clock, CheckCircle, XCircle,
  StopCircle, ChevronDown, Terminal, RefreshCw
} from 'lucide-react';
import { formatDistanceToNow, format } from 'date-fns';

type Tab = 'jobs' | 'sessions';

export function ScraperPanel() {
  const [activeTab, setActiveTab] = useState<Tab>('jobs');
  const { jobs, sessions, activeJobs, fetchJobs, fetchSessions } = useScraperStore();
  
  return (
    <div className="h-full flex flex-col bg-slate-900/50">
      {/* Tabs */}
      <div className="flex border-b border-slate-800">
        <button
          onClick={() => setActiveTab('jobs')}
          className={`flex-1 px-4 py-3 text-xs font-medium flex items-center justify-center gap-2 transition-colors ${
            activeTab === 'jobs' 
              ? 'text-accent-cyan border-b-2 border-accent-cyan bg-slate-800/30' 
              : 'text-slate-400 hover:text-slate-200'
          }`}
        >
          <Activity className="w-3.5 h-3.5" />
          Jobs
          {activeJobs.length > 0 && (
            <span className="px-1.5 py-0.5 rounded-full bg-accent-cyan/20 text-accent-cyan text-[10px]">
              {activeJobs.length}
            </span>
          )}
        </button>
        <button
          onClick={() => setActiveTab('sessions')}
          className={`flex-1 px-4 py-3 text-xs font-medium flex items-center justify-center gap-2 transition-colors ${
            activeTab === 'sessions' 
              ? 'text-accent-cyan border-b-2 border-accent-cyan bg-slate-800/30' 
              : 'text-slate-400 hover:text-slate-200'
          }`}
        >
          <Key className="w-3.5 h-3.5" />
          Sessions
          <span className="px-1.5 py-0.5 rounded-full bg-slate-700 text-slate-300 text-[10px]">
            {sessions.length}
          </span>
        </button>
      </div>
      
      {/* Content */}
      <div className="flex-1 overflow-y-auto">
        {activeTab === 'jobs' ? (
          <JobsList jobs={jobs} onRefresh={fetchJobs} />
        ) : (
          <SessionsList sessions={sessions} onRefresh={fetchSessions} />
        )}
      </div>
    </div>
  );
}

function JobsList({ jobs, onRefresh }: { jobs: ScraperJob[]; onRefresh: () => void }) {
  const { cancelJob, logs } = useScraperStore();
  const [expandedJob, setExpandedJob] = useState<string | null>(null);
  
  const getStatusIcon = (status: string) => {
    switch (status) {
      case 'running':
        return <div className="w-3 h-3 border-2 border-accent-cyan border-t-transparent rounded-full animate-spin" />;
      case 'completed':
        return <CheckCircle className="w-4 h-4 text-accent-emerald" />;
      case 'failed':
        return <XCircle className="w-4 h-4 text-accent-rose" />;
      case 'cancelled':
        return <StopCircle className="w-4 h-4 text-slate-500" />;
      default:
        return <Clock className="w-4 h-4 text-accent-amber" />;
    }
  };
  
  const getStatusColor = (status: string) => {
    switch (status) {
      case 'running': return 'border-accent-cyan/30 bg-accent-cyan/5';
      case 'completed': return 'border-accent-emerald/30 bg-accent-emerald/5';
      case 'failed': return 'border-accent-rose/30 bg-accent-rose/5';
      default: return 'border-slate-700 bg-slate-800/50';
    }
  };
  
  return (
    <div className="p-3 space-y-2">
      <div className="flex items-center justify-between mb-2">
        <span className="text-xs text-slate-500">{jobs.length} total jobs</span>
        <button
          onClick={onRefresh}
          className="p-1.5 rounded text-slate-400 hover:text-slate-200 hover:bg-slate-800"
        >
          <RefreshCw className="w-3.5 h-3.5" />
        </button>
      </div>
      
      {jobs.length === 0 ? (
        <div className="text-center py-8">
          <Activity className="w-10 h-10 text-slate-700 mx-auto mb-2" />
          <p className="text-xs text-slate-500">No scraper jobs yet</p>
        </div>
      ) : (
        jobs.slice(0, 20).map(job => {
          const isExpanded = expandedJob === job.id;
          const jobLogs = logs.get(job.id) || [];
          
          return (
            <div 
              key={job.id} 
              className={`rounded-lg border ${getStatusColor(job.status)} transition-colors`}
            >
              <button
                onClick={() => setExpandedJob(isExpanded ? null : job.id)}
                className="w-full p-3 text-left"
              >
                <div className="flex items-center justify-between">
                  <div className="flex items-center gap-2">
                    {getStatusIcon(job.status)}
                    <span className="text-sm font-medium text-slate-200 capitalize">
                      {job.platform}
                    </span>
                  </div>
                  <ChevronDown className={`w-4 h-4 text-slate-500 transition-transform ${isExpanded ? 'rotate-180' : ''}`} />
                </div>
                
                <div className="flex items-center gap-3 mt-2">
                  {job.status === 'running' && (
                    <div className="flex-1">
                      <div className="h-1 bg-slate-700 rounded-full overflow-hidden">
                        <div 
                          className="h-full bg-accent-cyan transition-all" 
                          style={{ width: `${job.progress}%` }} 
                        />
                      </div>
                    </div>
                  )}
                  <span className="text-[10px] text-slate-500 font-mono">
                    {formatDistanceToNow(new Date(job.created_at), { addSuffix: true })}
                  </span>
                </div>
              </button>
              
              {isExpanded && (
                <div className="px-3 pb-3 border-t border-slate-700/50 mt-2 pt-2">
                  <div className="space-y-1 text-xs">
                    <div className="flex justify-between">
                      <span className="text-slate-500">Job ID:</span>
                      <span className="text-slate-300 font-mono">{job.id.slice(0, 8)}...</span>
                    </div>
                    {job.target_name && (
                      <div className="flex justify-between">
                        <span className="text-slate-500">Target:</span>
                        <span className="text-slate-300">{job.target_name}</span>
                      </div>
                    )}
                    {job.error && (
                      <div className="mt-2 p-2 bg-accent-rose/10 rounded text-accent-rose text-xs">
                        {job.error}
                      </div>
                    )}
                    
                    {job.status === 'running' && (
                      <button
                        onClick={() => cancelJob(job.id)}
                        className="w-full mt-2 py-1.5 rounded border border-accent-rose/30 text-accent-rose text-xs hover:bg-accent-rose/10"
                      >
                        Cancel Job
                      </button>
                    )}
                    
                    {/* Logs */}
                    {jobLogs.length > 0 && (
                      <div className="mt-3">
                        <div className="flex items-center gap-1 text-slate-500 mb-2">
                          <Terminal className="w-3 h-3" />
                          <span>Logs</span>
                        </div>
                        <div className="bg-slate-950 rounded p-2 max-h-32 overflow-y-auto font-mono text-[10px] space-y-0.5">
                          {jobLogs.slice(-10).map((log, i) => (
                            <div key={i} className={`${log.level === 'error' ? 'text-accent-rose' : 'text-slate-400'}`}>
                              <span className="text-slate-600">[{format(new Date(log.timestamp), 'HH:mm:ss')}]</span>{' '}
                              {log.message}
                            </div>
                          ))}
                        </div>
                      </div>
                    )}
                  </div>
                </div>
              )}
            </div>
          );
        })
      )}
    </div>
  );
}

function SessionsList({ sessions, onRefresh }: { sessions: Session[]; onRefresh: () => void }) {
  const { deleteSession } = useScraperStore();
  
  const getStatusIndicator = (status: string) => {
    switch (status) {
      case 'active':
        return <div className="status-indicator status-active" />;
      case 'expired':
        return <div className="status-indicator status-warning" />;
      default:
        return <div className="status-indicator status-error" />;
    }
  };
  
  return (
    <div className="p-3 space-y-2">
      <div className="flex items-center justify-between mb-2">
        <span className="text-xs text-slate-500">Session Vault</span>
        <button
          onClick={onRefresh}
          className="p-1.5 rounded text-slate-400 hover:text-slate-200 hover:bg-slate-800"
        >
          <RefreshCw className="w-3.5 h-3.5" />
        </button>
      </div>
      
      {sessions.length === 0 ? (
        <div className="text-center py-8">
          <Key className="w-10 h-10 text-slate-700 mx-auto mb-2" />
          <p className="text-xs text-slate-500">No sessions stored</p>
          <p className="text-[10px] text-slate-600 mt-1">Add session cookies to enable authenticated scraping</p>
        </div>
      ) : (
        sessions.map(session => (
          <div key={session.id} className="p-3 rounded-lg border border-slate-800 bg-slate-800/30">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                {getStatusIndicator(session.status)}
                <span className="text-sm font-medium text-slate-200 capitalize">
                  {session.platform}
                </span>
              </div>
              <span className={`text-[10px] px-2 py-0.5 rounded-full ${
                session.status === 'active' 
                  ? 'bg-accent-emerald/20 text-accent-emerald' 
                  : session.status === 'expired'
                    ? 'bg-accent-amber/20 text-accent-amber'
                    : 'bg-accent-rose/20 text-accent-rose'
              }`}>
                {session.status}
              </span>
            </div>
            
            <div className="mt-2 space-y-1 text-xs">
              <div className="flex justify-between">
                <span className="text-slate-500">Name:</span>
                <span className="text-slate-300">{session.session_name}</span>
              </div>
              {session.last_validated && (
                <div className="flex justify-between">
                  <span className="text-slate-500">Validated:</span>
                  <span className="text-slate-400 font-mono text-[10px]">
                    {formatDistanceToNow(new Date(session.last_validated), { addSuffix: true })}
                  </span>
                </div>
              )}
            </div>
            
            <button
              onClick={() => {
                if (confirm(`Delete ${session.platform} session?`)) {
                  deleteSession(session.id);
                }
              }}
              className="w-full mt-3 py-1.5 rounded border border-slate-700 text-slate-400 text-xs hover:border-accent-rose/30 hover:text-accent-rose hover:bg-accent-rose/5 transition-colors"
            >
              Remove Session
            </button>
          </div>
        ))
      )}
    </div>
  );
}
