import { useState } from 'react'
import { Link } from 'react-router-dom'
import { ChevronRight, Shield, History, RefreshCw, Database, AlertTriangle, Radar, Bug, Play, Clock, RotateCcw, Trash2, CheckCircle2, MoreVertical, FolderOpen, Plus } from 'lucide-react'

// Mock data - will be replaced with API calls
const MOCK_QUARANTINE = [
  {
    id: '1',
    filename: 'invoice_2024.pdf.exe',
    path: '/mnt/pool0/users/finance/',
    threat: 'Win.Trojan.Agent-1',
    threatLevel: 'high',
    date: '2024-10-24T10:42:00Z',
  },
  {
    id: '2',
    filename: 'keygen_v2.zip',
    path: '/mnt/pool0/public/software/',
    threat: 'PUA.Win.Tool.Keygen',
    threatLevel: 'medium',
    date: '2024-10-22T20:15:00Z',
  },
  {
    id: '3',
    filename: 'script_final.sh',
    path: '/root/downloads/',
    threat: 'Unix.Malware.Agent',
    threatLevel: 'high',
    date: '2024-10-20T02:30:00Z',
  },
]

const MOCK_SCHEDULED_SCANS = [
  {
    id: '1',
    name: 'Daily Full System',
    target: '/',
    frequency: 'Every day at 02:00',
    lastRun: 'Yesterday 02:00',
    status: 'success',
  },
  {
    id: '2',
    name: 'Weekly Pool Scan',
    target: '/mnt/pool0',
    frequency: 'Sundays at 04:00',
    lastRun: '3 days ago',
    status: 'success',
  },
]

export default function ShareShield() {
  const [serviceEnabled, setServiceEnabled] = useState(true)
  const [scanPath, setScanPath] = useState('/mnt/pool0/data')
  const [recursiveScan, setRecursiveScan] = useState(true)
  const [scanArchives, setScanArchives] = useState(false)
  const [autoRemove, setAutoRemove] = useState(false)
  const [selectedQuarantine, setSelectedQuarantine] = useState<string[]>([])

  const formatDate = (dateString: string) => {
    const date = new Date(dateString)
    return date.toLocaleDateString('en-US', {
      month: 'short',
      day: 'numeric',
      hour: 'numeric',
      minute: '2-digit',
    })
  }

  const getThreatBadgeColor = (level: string) => {
    if (level === 'high') {
      return 'bg-red-500/10 text-red-400 border-red-500/20'
    }
    return 'bg-amber-500/10 text-amber-400 border-amber-500/20'
  }

  const handleSelectAll = () => {
    if (selectedQuarantine.length === MOCK_QUARANTINE.length) {
      setSelectedQuarantine([])
    } else {
      setSelectedQuarantine(MOCK_QUARANTINE.map((q) => q.id))
    }
  }

  const handleQuarantineSelect = (id: string) => {
    if (selectedQuarantine.includes(id)) {
      setSelectedQuarantine(selectedQuarantine.filter((q) => q !== id))
    } else {
      setSelectedQuarantine([...selectedQuarantine, id])
    }
  }

  return (
    <div className="flex-1 overflow-hidden flex flex-col bg-background-dark">
      {/* Header */}
      <header className="sticky top-0 z-20 bg-background-dark/95 backdrop-blur border-b border-border-dark px-6 py-4 lg:px-10">
        <div className="max-w-7xl mx-auto w-full">
          <div className="flex flex-col gap-4">
            <div className="flex items-center gap-2 text-sm">
              <Link to="/system" className="text-text-secondary hover:text-primary transition-colors">
                System
              </Link>
              <ChevronRight className="text-text-secondary" size={16} />
              <Link to="/system" className="text-text-secondary hover:text-primary transition-colors">
                Security
              </Link>
              <ChevronRight className="text-text-secondary" size={16} />
              <span className="text-white font-medium">Share Shield System</span>
            </div>
            <div className="flex flex-wrap justify-between items-end gap-4">
              <div>
                <h1 className="text-white text-3xl font-black tracking-tight mb-1">Share Shield System</h1>
                <p className="text-text-secondary text-sm lg:text-base max-w-2xl">
                  Manage virus definitions, on-demand scans, and quarantine settings for your storage pools.
                </p>
              </div>
              <div className="flex gap-3">
                <button className="flex items-center gap-2 px-4 py-2 bg-[#233648] hover:bg-[#2c445a] text-white text-sm font-medium rounded-lg border border-border-dark transition-all">
                  <History size={16} />
                  Scan History
                </button>
                <button className="flex items-center gap-2 px-4 py-2 bg-primary hover:bg-blue-600 text-white text-sm font-bold rounded-lg shadow-lg shadow-blue-500/20 transition-all">
                  <RefreshCw size={16} />
                  Update Definitions
                </button>
              </div>
            </div>
          </div>
        </div>
      </header>

      {/* Scrollable Content */}
      <div className="flex-1 p-6 lg:px-10 py-8 overflow-y-auto">
        <div className="max-w-7xl mx-auto w-full flex flex-col gap-6">
          {/* Top Row: Service Status & Key Stats */}
          <div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4">
            {/* Service Status Card */}
            <div className="xl:col-span-2 rounded-xl border border-border-dark bg-card-dark p-5 flex items-center justify-between shadow-sm">
              <div className="flex items-center gap-4">
                <div className="p-3 rounded-full bg-emerald-500/10 border border-emerald-500/20">
                  <Shield className="text-emerald-500" size={32} />
                </div>
                <div>
                  <h3 className="text-white text-base font-bold">Share Shield Service</h3>
                  <p className="text-emerald-400 text-sm font-medium flex items-center gap-1.5">
                    <span className="size-1.5 rounded-full bg-emerald-400 animate-pulse"></span>
                    Active & Protecting
                  </p>
                </div>
              </div>
              <label className="relative flex h-[28px] w-[48px] cursor-pointer items-center rounded-full border-none bg-[#233648] p-0.5 transition-colors duration-300 has-[:checked]:justify-end has-[:checked]:bg-primary">
                <div className="h-[24px] w-[24px] rounded-full bg-white shadow-sm"></div>
                <input
                  checked={serviceEnabled}
                  onChange={(e) => setServiceEnabled(e.target.checked)}
                  className="invisible absolute"
                  type="checkbox"
                />
              </label>
            </div>

            {/* Stats: Definitions */}
            <div className="rounded-xl border border-border-dark bg-card-dark p-5 flex flex-col justify-between shadow-sm">
              <div className="flex justify-between items-start mb-2">
                <span className="text-text-secondary text-sm font-medium">Virus Definitions</span>
                <Database className="text-text-secondary" size={20} />
              </div>
              <div>
                <p className="text-white text-2xl font-bold tracking-tight">v26500</p>
                <p className="text-text-secondary text-xs mt-1">Updated: 2 hours ago</p>
              </div>
            </div>

            {/* Stats: Quarantine */}
            <div className="rounded-xl border border-border-dark bg-card-dark p-5 flex flex-col justify-between shadow-sm">
              <div className="flex justify-between items-start mb-2">
                <span className="text-text-secondary text-sm font-medium">Quarantined Files</span>
                <AlertTriangle className="text-amber-500" size={20} />
              </div>
              <div>
                <div className="flex items-baseline gap-2">
                  <p className="text-white text-2xl font-bold tracking-tight">12</p>
                  <span className="text-emerald-500 text-xs font-bold bg-emerald-500/10 px-1.5 py-0.5 rounded">+2 new</span>
                </div>
                <p className="text-text-secondary text-xs mt-1">Since last reboot</p>
              </div>
            </div>
          </div>

          {/* Middle Row: Scanner Configuration & Quarantine Table */}
          <div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
            {/* Left Column: Scanner (1/3 width) */}
            <div className="flex flex-col gap-6 xl:col-span-1">
              {/* Quick Scan Card */}
              <div className="rounded-xl border border-border-dark bg-card-dark overflow-hidden flex flex-col">
                <div className="p-5 border-b border-border-dark bg-[#16202a]">
                  <h3 className="text-white font-bold text-lg flex items-center gap-2">
                    <Radar className="text-primary" size={20} />
                    On-Demand Scanner
                  </h3>
                </div>
                <div className="p-5 flex flex-col gap-5">
                  <div className="flex flex-col gap-2">
                    <label className="text-sm font-medium text-text-secondary">Target Directory</label>
                    <div className="flex">
                      <input
                        className="flex-1 bg-[#111a22] border border-border-dark text-white text-sm rounded-l-lg px-3 py-2.5 focus:ring-1 focus:ring-primary focus:border-primary outline-none placeholder-gray-600"
                        placeholder="/path/to/scan"
                        type="text"
                        value={scanPath}
                        onChange={(e) => setScanPath(e.target.value)}
                      />
                      <button className="bg-[#233648] hover:bg-[#2c445a] text-white px-3 border-y border-r border-border-dark rounded-r-lg transition-colors">
                        <FolderOpen size={18} />
                      </button>
                    </div>
                  </div>
                  <div className="space-y-3">
                    <label className="flex items-center gap-3 cursor-pointer group">
                      <input
                        checked={recursiveScan}
                        onChange={(e) => setRecursiveScan(e.target.checked)}
                        className="rounded border-border-dark bg-[#233648] text-primary focus:ring-offset-[#111a22] focus:ring-primary size-4"
                        type="checkbox"
                      />
                      <span className="text-sm text-gray-300 group-hover:text-white transition-colors">Recursive Scan</span>
                    </label>
                    <label className="flex items-center gap-3 cursor-pointer group">
                      <input
                        checked={scanArchives}
                        onChange={(e) => setScanArchives(e.target.checked)}
                        className="rounded border-border-dark bg-[#233648] text-primary focus:ring-offset-[#111a22] focus:ring-primary size-4"
                        type="checkbox"
                      />
                      <span className="text-sm text-gray-300 group-hover:text-white transition-colors">Scan Archives (zip, rar)</span>
                    </label>
                    <label className="flex items-center gap-3 cursor-pointer group">
                      <input
                        checked={autoRemove}
                        onChange={(e) => setAutoRemove(e.target.checked)}
                        className="rounded border-border-dark bg-[#233648] text-primary focus:ring-offset-[#111a22] focus:ring-primary size-4"
                        type="checkbox"
                      />
                      <span className="text-sm text-gray-300 group-hover:text-white transition-colors">Remove Threats Automatically</span>
                    </label>
                  </div>
                  <div className="pt-2 border-t border-border-dark/50 flex flex-col gap-3">
                    <button className="w-full py-2.5 bg-primary hover:bg-blue-600 text-white font-bold rounded-lg shadow-lg shadow-blue-500/20 transition-all flex justify-center items-center gap-2">
                      <Play size={18} />
                      Start Scan
                    </button>
                    <button className="w-full py-2.5 bg-[#233648] hover:bg-[#2c445a] text-text-secondary hover:text-white font-medium rounded-lg border border-border-dark transition-all flex justify-center items-center gap-2">
                      <Clock size={18} />
                      Schedule Scan
                    </button>
                  </div>
                </div>
              </div>
            </div>

            {/* Right Column: Quarantine & Active Threats (2/3 width) */}
            <div className="xl:col-span-2 flex flex-col gap-6">
              <div className="rounded-xl border border-border-dark bg-card-dark overflow-hidden flex flex-col h-full">
                <div className="p-5 border-b border-border-dark bg-[#16202a] flex justify-between items-center">
                  <h3 className="text-white font-bold text-lg flex items-center gap-2">
                    <Bug className="text-amber-500" size={20} />
                    Quarantine Manager
                  </h3>
                  <div className="flex gap-2">
                    <button
                      onClick={handleSelectAll}
                      className="text-xs font-medium text-text-secondary hover:text-white px-3 py-1.5 rounded-md hover:bg-[#233648] transition-colors border border-transparent hover:border-border-dark"
                    >
                      Select All
                    </button>
                    <button className="text-xs font-medium text-red-400 hover:text-red-300 px-3 py-1.5 rounded-md hover:bg-red-400/10 transition-colors border border-transparent hover:border-red-400/20">
                      Delete Selected
                    </button>
                  </div>
                </div>
                <div className="overflow-x-auto">
                  <table className="w-full text-left text-sm">
                    <thead className="bg-[#16202a] text-text-secondary font-medium">
                      <tr>
                        <th className="px-5 py-3 w-10">
                          <input
                            checked={selectedQuarantine.length === MOCK_QUARANTINE.length}
                            onChange={handleSelectAll}
                            className="rounded border-border-dark bg-[#233648] text-primary focus:ring-offset-[#111a22] focus:ring-primary size-4"
                            type="checkbox"
                          />
                        </th>
                        <th className="px-5 py-3">Filename</th>
                        <th className="px-5 py-3">Threat Detected</th>
                        <th className="px-5 py-3">Date</th>
                        <th className="px-5 py-3 text-right">Actions</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-border-dark">
                      {MOCK_QUARANTINE.map((item) => (
                        <tr key={item.id} className="group hover:bg-[#233648]/30 transition-colors">
                          <td className="px-5 py-4">
                            <input
                              checked={selectedQuarantine.includes(item.id)}
                              onChange={() => handleQuarantineSelect(item.id)}
                              className="rounded border-border-dark bg-[#233648] text-primary focus:ring-offset-[#111a22] focus:ring-primary size-4"
                              type="checkbox"
                            />
                          </td>
                          <td className="px-5 py-4">
                            <div className="flex flex-col">
                              <span className="text-white font-medium">{item.filename}</span>
                              <span className="text-xs text-text-secondary font-mono">{item.path}</span>
                            </div>
                          </td>
                          <td className="px-5 py-4">
                            <span className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border ${getThreatBadgeColor(item.threatLevel)}`}>
                              {item.threat}
                            </span>
                          </td>
                          <td className="px-5 py-4 text-text-secondary">{formatDate(item.date)}</td>
                          <td className="px-5 py-4 text-right">
                            <div className="flex justify-end gap-2 opacity-60 group-hover:opacity-100 transition-opacity">
                              <button
                                className="p-1.5 text-text-secondary hover:text-emerald-400 hover:bg-emerald-400/10 rounded-md transition-colors"
                                title="Restore"
                              >
                                <RotateCcw size={20} />
                              </button>
                              <button
                                className="p-1.5 text-text-secondary hover:text-red-400 hover:bg-red-400/10 rounded-md transition-colors"
                                title="Delete Permanently"
                              >
                                <Trash2 size={20} />
                              </button>
                            </div>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
                <div className="p-4 border-t border-border-dark bg-[#16202a] text-xs text-text-secondary flex justify-center">
                  Showing {MOCK_QUARANTINE.length} of 12 quarantined items
                </div>
              </div>
            </div>
          </div>

          {/* Bottom: Scheduled Tasks & Recent Logs */}
          <div className="rounded-xl border border-border-dark bg-card-dark overflow-hidden mt-2">
            <div className="p-5 border-b border-border-dark bg-[#16202a] flex justify-between items-center">
              <h3 className="text-white font-bold text-lg">Scheduled Scans</h3>
              <button className="text-primary hover:text-blue-400 text-sm font-bold flex items-center gap-1">
                <Plus size={18} />
                Add Schedule
              </button>
            </div>
            <div className="p-0 overflow-x-auto">
              <table className="w-full text-left text-sm">
                <thead className="text-text-secondary font-medium border-b border-border-dark">
                  <tr>
                    <th className="px-6 py-3 font-medium">Name</th>
                    <th className="px-6 py-3 font-medium">Target</th>
                    <th className="px-6 py-3 font-medium">Frequency</th>
                    <th className="px-6 py-3 font-medium">Last Run</th>
                    <th className="px-6 py-3 font-medium">Status</th>
                    <th className="px-6 py-3 font-medium text-right">Actions</th>
                  </tr>
                </thead>
                <tbody className="divide-y divide-border-dark">
                  {MOCK_SCHEDULED_SCANS.map((scan) => (
                    <tr key={scan.id}>
                      <td className="px-6 py-4 text-white font-medium">{scan.name}</td>
                      <td className="px-6 py-4 text-text-secondary font-mono text-xs">{scan.target}</td>
                      <td className="px-6 py-4 text-text-secondary">{scan.frequency}</td>
                      <td className="px-6 py-4 text-text-secondary">{scan.lastRun}</td>
                      <td className="px-6 py-4">
                        <span className="text-emerald-400 flex items-center gap-1">
                          <CheckCircle2 size={16} />
                          Success
                        </span>
                      </td>
                      <td className="px-6 py-4 text-right">
                        <button className="text-text-secondary hover:text-white">
                          <MoreVertical size={18} />
                        </button>
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}

