'use client';

import { useState, useEffect, useMemo } from 'react';
import { ServiceStatus } from '@/src/types/service';
import { AppConfig, Service } from '@/src/lib/config';
import { ServiceCard } from './ServiceCard';
import { DashboardFilters } from './dashboard-filters';

interface ServiceCheckResult {
  id: string;
  status: 'online' | 'warning' | 'offline';
  responseTimeMs: number;
  httpStatus: number | null;
  checkedAt: string;
}

interface DashboardClientProps {
  initialServices: Service[];
  config: AppConfig;
}

export function DashboardClient({ initialServices, config }: DashboardClientProps) {
  const [results, setResults] = useState<ServiceCheckResult[]>([]);
  const [loading, setLoading] = useState(true);
  const [refreshing, setRefreshing] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [lastUpdated, setLastUpdated] = useState<string | null>(null);

  // Filter states
  const [searchQuery, setSearchQuery] = useState('');
  const [selectedCategory, setSelectedCategory] = useState('all');
  const [selectedStatus, setSelectedStatus] = useState<ServiceStatus | 'all'>('all');

  const fetchStatus = async (isManualRefresh = false) => {
    try {
      if (isManualRefresh) {
        setRefreshing(true);
      } else {
        setError(null);
      }

      const response = await fetch('/api/status');
      if (!response.ok) {
        throw new Error('Failed to fetch status');
      }
      const data: ServiceCheckResult[] = await response.json();
      setResults(data);
      setLastUpdated(new Date().toISOString());
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Unknown error');
    } finally {
      setLoading(false);
      setRefreshing(false);
    }
  };

  useEffect(() => {
    fetchStatus();

    const interval = setInterval(() => fetchStatus(), config.refreshInterval);

    return () => clearInterval(interval);
  }, [config.refreshInterval]);

  // Get unique categories from config
  const categories = useMemo(() => {
    return config.categories;
  }, [config.categories]);

  // Filter services based on search, category, and status
  const filteredServices = useMemo(() => {
    return initialServices.filter((service) => {
      const result = results.find(r => r.id === service.id);

      // Search filter (case-insensitive)
      const matchesSearch =
        searchQuery === '' ||
        service.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
        service.description.toLowerCase().includes(searchQuery.toLowerCase());

      // Category filter
      const matchesCategory =
        selectedCategory === 'all' || service.category === selectedCategory;

      // Status filter
      const matchesStatus =
        selectedStatus === 'all' || (result && result.status === selectedStatus);

      return matchesSearch && matchesCategory && matchesStatus;
    });
  }, [initialServices, results, searchQuery, selectedCategory, selectedStatus]);

  const getResultForService = (serviceId: string): ServiceCheckResult | undefined => {
    return results.find(result => result.id === serviceId);
  };

  const handleManualRefresh = () => {
    fetchStatus(true);
  };

  return (
    <div className="min-h-screen bg-slate-50 dark:bg-slate-900">
      {/* Header */}
      <header className="bg-gradient-to-r from-slate-900 to-slate-800 border-b border-slate-700 shadow-lg">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
          <div className="flex flex-col gap-2">
            <div className="flex items-center justify-between">
              <h1 className="text-3xl sm:text-4xl font-bold text-white tracking-tight">
                {config.title}
              </h1>
              <div className="text-sm text-slate-400">
                {loading ? (
                  'Lade Status...'
                ) : refreshing ? (
                  'Aktualisiere...'
                ) : (
                  `Automatische Aktualisierung: ${Math.round(config.refreshInterval / 1000)}s`
                )}
              </div>
            </div>
            <p className="text-slate-400 text-sm">
              {config.subtitle}
            </p>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        {error && (
          <div className="mb-8 p-4 bg-red-50 dark:bg-red-950 border border-red-200 dark:border-red-800 rounded-lg">
            <div className="flex items-center gap-2">
              <svg
                className="w-5 h-5 text-red-600 dark:text-red-400"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"
                />
              </svg>
              <span className="text-red-800 dark:text-red-200">
                Fehler beim Laden der Status-Daten: {error}
              </span>
            </div>
          </div>
        )}

        {/* Filters */}
        <DashboardFilters
          searchQuery={searchQuery}
          onSearchChange={setSearchQuery}
          selectedCategory={selectedCategory}
          onCategoryChange={setSelectedCategory}
          selectedStatus={selectedStatus}
          onStatusChange={setSelectedStatus}
          categories={categories}
          onRefresh={handleManualRefresh}
          isRefreshing={refreshing}
          visibleCount={filteredServices.length}
          totalCount={initialServices.length}
          lastUpdated={lastUpdated}
        />

        {/* Services Grid */}
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
          {filteredServices.map((service) => (
            <ServiceCard
              key={service.id}
              service={service}
              result={getResultForService(service.id)}
            />
          ))}
        </div>

        {/* No results message */}
        {filteredServices.length === 0 && !loading && (
          <div className="flex flex-col items-center justify-center py-12">
            <svg
              className="w-16 h-16 text-slate-400 mb-4"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth={1.5}
                d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
              />
            </svg>
            <h3 className="text-xl font-semibold text-slate-900 dark:text-white">
              Keine Dienste gefunden
            </h3>
            <p className="text-slate-600 dark:text-slate-400 mt-2 text-center">
              Versuche andere Suchbegriffe oder Filtereinstellungen.
            </p>
          </div>
        )}
      </main>
    </div>
  );
}
