import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Separator } from "@/components/ui/separator"
import { Button } from "@/components/ui/button"
import { Moon, Sun, Monitor, CheckCircle2, XCircle, AlertCircle, Clock } from "lucide-react"
import { useTheme } from "@/components/theme-provider"
import { useMetrics } from "./hooks/useMetrics"
import { ServiceStatus } from "./types/metrics"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { 
  Server, 
  Box, 
  Play, 
  Activity, 
  Shield, 
  Database, 
  Download, 
  Home, 
  Tv, 
  Globe, 
  Gauge, 
  HardDrive,
  Search,
  Settings,
  LayoutDashboard,
  Package,
  Smartphone
} from "lucide-react"

interface ServiceCard {
  name: string
  description: string
  url: string
  icon: React.ReactNode
  category: 'system' | 'media' | 'monitoring' | 'tools' | 'acot' | 'home'
  monitorName?: string // Optional monitor name that matches Uptime Kuma
}

const services: ServiceCard[] = [
  {
    name: "Portainer",
    description: "Container Management",
    url: "https://portainer.kent.pw",
    icon: <Box className="h-6 w-6" />,
    category: "system"
  },
  {
    name: "Gitea",
    description: "Git Server",
    url: "https://gitea.kent.pw",
    icon: <Server className="h-6 w-6" />,
    category: "system"
  },
  {
    name: "Cockpit",
    description: "Server Management",
    url: "https://cockpit.kent.pw",
    icon: <Settings className="h-6 w-6" />,
    category: "system"
  },
  {
    name: "DiskStation",
    description: "Synology NAS",
    url: "https://diskstation.kent.pw",
    icon: <HardDrive className="h-6 w-6" />,
    category: "system",
    monitorName: "Synology Diskstation"
  },
  {
    name: "Plex",
    description: "Media Server",
    url: "https://plex.kent.pw",
    icon: <Play className="h-6 w-6" />,
    category: "media"
  },
  {
    name: "Sonarr",
    description: "TV Show Management",
    url: "https://sonarr.kent.pw",
    icon: <Tv className="h-6 w-6" />,
    category: "media"
  },
  {
    name: "Jackett",
    description: "Torrent Indexer",
    url: "https://jackett.kent.pw",
    icon: <Search className="h-6 w-6" />,
    category: "media"
  },
  {
    name: "Deluge",
    description: "Torrent Client",
    url: "https://deluge.kent.pw",
    icon: <Download className="h-6 w-6" />,
    category: "media"
  },
  {
    name: "Uptime",
    description: "Service Monitoring",
    url: "https://uptime.kent.pw",
    icon: <Activity className="h-6 w-6" />,
    category: "monitoring"
  },
  {
    name: "AdGuard",
    description: "Network Ad Blocking",
    url: "https://adguard.kent.pw",
    icon: <Shield className="h-6 w-6" />,
    category: "system",
    monitorName: "Adguard Home"
  },
  {
    name: "NocoDB",
    description: "Database Platform",
    url: "https://noco.kent.pw",
    icon: <Database className="h-6 w-6" />,
    category: "tools",
    monitorName: "NocoDB"
  },
  {
    name: "IT Tools",
    description: "Developer Utilities",
    url: "https://ittools.kent.pw",
    icon: <Settings className="h-6 w-6" />,
    category: "tools",
    monitorName: "IT Tools"
  },
  {
    name: "Firefox",
    description: "Browser Instance",
    url: "https://firefox.kent.pw",
    icon: <Globe className="h-6 w-6" />,
    category: "tools"
  },
  {
    name: "Speedtest",
    description: "Network Speed Monitor",
    url: "https://speedtest.kent.pw",
    icon: <Gauge className="h-6 w-6" />,
    category: "monitoring",
    monitorName: "Speedtest Tracker"
  },
  {
    name: "Drive",
    description: "File Storage",
    url: "https://drive.kent.pw",
    icon: <HardDrive className="h-6 w-6" />,
    category: "tools",
    monitorName: "Synology Drive"
  },
  {
    name: "Dashboard",
    description: "ACOT Dashboard",
    url: "https://dashboard.kent.pw",
    icon: <LayoutDashboard className="h-6 w-6" />,
    category: "acot"
  },
  {
    name: "Inventory",
    description: "ACOT Inventory",
    url: "https://inventory.kent.pw",
    icon: <Package className="h-6 w-6" />,
    category: "acot"
  },
  {
    name: "Homebridge",
    description: "HomeKit Bridge",
    url: "https://homebridge.kent.pw",
    icon: <Home className="h-6 w-6" />,
    category: "home"
  },
  {
    name: "Scrypted",
    description: "Smart Home Integration",
    url: "https://scrypted.kent.pw",
    icon: <Smartphone className="h-6 w-6" />,
    category: "home"
  }
]

function ThemeToggle() {
  const { theme, setTheme } = useTheme()

  const cycleTheme = () => {
    if (theme === 'light') setTheme('dark')
    else if (theme === 'dark') setTheme('system')
    else setTheme('light')
  }

  return (
    <Button 
      variant="outline" 
      size="icon" 
      onClick={cycleTheme}
      className="relative w-10 h-10"
    >
      <Sun className={`h-[1.2rem] w-[1.2rem] transition-all ${
        theme === 'light' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
      }`} />
      <Moon className={`absolute h-[1.2rem] w-[1.2rem] transition-all ${
        theme === 'dark' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
      }`} />
      <Monitor className={`absolute h-[1.2rem] w-[1.2rem] transition-all ${
        theme === 'system' ? 'scale-100 rotate-0' : 'scale-0 rotate-90'
      }`} />
      <span className="sr-only">Toggle theme</span>
    </Button>
  )
}

function StatusIndicator({ status, responseTime, certDaysRemaining }: { 
  status: ServiceStatus, 
  responseTime: number,
  certDaysRemaining: number 
}) {
  const getStatusIcon = () => {
    switch (status) {
      case 'up':
        return <CheckCircle2 className="h-4 w-4 text-green-500" />
      case 'down':
        return <XCircle className="h-4 w-4 text-red-500" />
      case 'pending':
        return <Clock className="h-4 w-4 text-yellow-500" />
      case 'maintenance':
        return <AlertCircle className="h-4 w-4 text-blue-500" />
    }
  }

  const getStatusText = () => {
    const statusText = status.charAt(0).toUpperCase() + status.slice(1)
    const responseTimeText = `Response time: ${responseTime}ms`
    const certText = certDaysRemaining > 0 
      ? `SSL cert expires in ${certDaysRemaining} days`
      : 'SSL cert expired'

    return `${statusText} • ${responseTimeText} • ${certText}`
  }

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger>
          {getStatusIcon()}
        </TooltipTrigger>
        <TooltipContent>
          <p>{getStatusText()}</p>
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  )
}

function ServiceSection({ title, services, metrics }: { 
  title: string, 
  services: ServiceCard[],
  metrics: Record<string, any>
}) {
  return (
    <div className="space-y-4">
      <div className="flex items-center">
        <h2 className="text-2xl font-bold">{title}</h2>
        <Separator className="flex-1 ml-4" />
      </div>
      <div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-4">
        {services.map((service) => {
          const serviceMetrics = metrics[service.monitorName || service.name] || {
            status: 'pending',
            responseTime: 0,
            certDaysRemaining: 0
          }

          // Get background color based on service
          const bgColor = (() => {
            switch (service.name) {
              case "Portainer":
                return "bg-blue-50 dark:bg-blue-950/30" // Portainer blue
              case "Gitea":
                return "bg-green-50 dark:bg-green-950/30" // Gitea green
              case "Plex":
                return "bg-orange-50 dark:bg-orange-950/30" // Plex orange
              case "Sonarr":
                return "bg-blue-50 dark:bg-blue-950/30" // Sonarr blue
              case "AdGuard":
                return "bg-emerald-50 dark:bg-emerald-950/30" // AdGuard green
              case "Homebridge":
                return "bg-purple-50 dark:bg-purple-950/30" // Homebridge purple
              case "Scrypted":
                return "bg-indigo-50 dark:bg-indigo-950/30" // Scrypted blue/purple
              case "Dashboard":
              case "Inventory":
                return "bg-sky-50 dark:bg-sky-950/30" // ACOT blue
              case "DiskStation":
                return "bg-slate-50 dark:bg-slate-950/30" // Synology gray
              case "Deluge":
                return "bg-green-50 dark:bg-green-950/30" // Deluge green
              case "Firefox":
                return "bg-orange-50 dark:bg-orange-950/30" // Firefox orange
              case "Uptime":
                return "bg-emerald-50 dark:bg-emerald-950/30" // Uptime green (success color)
              case "Speedtest":
                return "bg-blue-50 dark:bg-blue-950/30" // Speedtest blue
              default:
                return "bg-gray-50 dark:bg-gray-950/30" // Default subtle background
            }
          })()

          // Get icon color based on service
          const iconColor = (() => {
            switch (service.name) {
              case "Portainer":
                return "text-blue-600 dark:text-blue-400"
              case "Gitea":
                return "text-green-600 dark:text-green-400"
              case "Plex":
                return "text-orange-600 dark:text-orange-400"
              case "Sonarr":
                return "text-blue-600 dark:text-blue-400"
              case "AdGuard":
                return "text-emerald-600 dark:text-emerald-400"
              case "Homebridge":
                return "text-purple-600 dark:text-purple-400"
              case "Scrypted":
                return "text-indigo-600 dark:text-indigo-400"
              case "Dashboard":
              case "Inventory":
                return "text-sky-600 dark:text-sky-400"
              case "DiskStation":
                return "text-slate-600 dark:text-slate-400"
              case "Deluge":
                return "text-green-600 dark:text-green-400"
              case "Firefox":
                return "text-orange-600 dark:text-orange-400"
              case "Uptime":
                return "text-emerald-600 dark:text-emerald-400"
              case "Speedtest":
                return "text-blue-600 dark:text-blue-400"
              default:
                return "text-gray-600 dark:text-gray-400"
            }
          })()

          return (
            <a 
              key={service.name} 
              href={service.url} 
              target="_blank" 
              rel="noopener noreferrer"
              className="transition-transform hover:scale-105"
            >
              <Card className={bgColor}>
                <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                  <div className="flex items-center">
                    <div className={iconColor}>{service.icon}</div>
                    <CardTitle className="ml-2">{service.name}</CardTitle>
                  </div>
                  <StatusIndicator 
                    status={serviceMetrics.status}
                    responseTime={serviceMetrics.responseTime}
                    certDaysRemaining={serviceMetrics.certDaysRemaining}
                  />
                </CardHeader>
                <CardContent>
                  <CardDescription>{service.description}</CardDescription>
                </CardContent>
              </Card>
            </a>
          )
        })}
      </div>
    </div>
  )
}

function App() {
  const { metrics, loading, error } = useMetrics(import.meta.env.VITE_UPTIME_API_KEY)
  const systemServices = services.filter(s => s.category === 'system')
  const mediaServices = services.filter(s => s.category === 'media')
  const monitoringServices = services.filter(s => s.category === 'monitoring')
  const toolServices = services.filter(s => s.category === 'tools')
  const acotServices = services.filter(s => s.category === 'acot')
  const homeServices = services.filter(s => s.category === 'home')

  return (
    <div className="min-h-screen bg-gray-100 dark:bg-background p-8 space-y-8">
      <div className="flex justify-between items-center">
        <h1 className="text-4xl font-bold">Kent.pw Homepage</h1>
        <ThemeToggle />
      </div>
      <ServiceSection title="System" services={systemServices} metrics={metrics} />
      <ServiceSection title="Media" services={mediaServices} metrics={metrics} />
      <ServiceSection title="Monitoring" services={monitoringServices} metrics={metrics} />
      <ServiceSection title="Tools" services={toolServices} metrics={metrics} />
      <ServiceSection title="ACOT" services={acotServices} metrics={metrics} />
      <ServiceSection title="Home" services={homeServices} metrics={metrics} />
    </div>
  )
}

export default App
