import { useEffect, useState, useCallback } from 'react'
import type { NightModeConfig, NightModeStatus, ServicesInfo } from './types'

export function useNightMode() {
  const [status, setStatus] = useState<NightModeStatus | null>(null)
  const [services, setServices] = useState<ServicesInfo | null>(null)
  const [loading, setLoading] = useState(true)
  const [actionLoading, setActionLoading] = useState<string | null>(null)
  const [error, setError] = useState<string | null>(null)
  const [successMessage, setSuccessMessage] = useState<string | null>(null)

  const [editMode, setEditMode] = useState(false)
  const [editConfig, setEditConfig] = useState<NightModeConfig | null>(null)

  const fetchData = useCallback(async () => {
    setError(null)
    try {
      const [statusRes, servicesRes] = await Promise.all([
        fetch('/api/admin/night-mode'),
        fetch('/api/admin/night-mode/services'),
      ])

      if (statusRes.ok) {
        const data = await statusRes.json()
        setStatus(data)
        if (!editMode) {
          setEditConfig(data.config)
        }
      } else {
        const errData = await statusRes.json()
        setError(errData.error || 'Fehler beim Laden des Status')
      }

      if (servicesRes.ok) {
        setServices(await servicesRes.json())
      }
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Verbindung zum Night-Scheduler fehlgeschlagen')
    } finally {
      setLoading(false)
    }
  }, [editMode])

  useEffect(() => {
    fetchData()
  }, [fetchData])

  // Auto-Refresh alle 30 Sekunden
  useEffect(() => {
    const interval = setInterval(fetchData, 30000)
    return () => clearInterval(interval)
  }, [fetchData])

  const saveConfig = async () => {
    if (!editConfig) return

    setActionLoading('save')
    setError(null)
    setSuccessMessage(null)

    try {
      const response = await fetch('/api/admin/night-mode', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(editConfig),
      })

      if (!response.ok) {
        const errData = await response.json()
        throw new Error(errData.error || 'Fehler beim Speichern')
      }

      setEditMode(false)
      setSuccessMessage('Konfiguration gespeichert')
      setTimeout(() => setSuccessMessage(null), 3000)
      fetchData()
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Speichern fehlgeschlagen')
    } finally {
      setActionLoading(null)
    }
  }

  const executeAction = async (action: 'start' | 'stop') => {
    setActionLoading(action)
    setError(null)
    setSuccessMessage(null)

    try {
      const response = await fetch('/api/admin/night-mode/execute', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ action }),
      })

      if (!response.ok) {
        const errData = await response.json()
        throw new Error(errData.error || `Fehler bei ${action}`)
      }

      const data = await response.json()
      setSuccessMessage(data.message || `${action === 'start' ? 'Gestartet' : 'Gestoppt'}`)
      setTimeout(() => setSuccessMessage(null), 5000)
      fetchData()
    } catch (err) {
      setError(err instanceof Error ? err.message : `${action} fehlgeschlagen`)
    } finally {
      setActionLoading(null)
    }
  }

  const toggleEnabled = async () => {
    if (!editConfig) return

    const newConfig = { ...editConfig, enabled: !editConfig.enabled }
    setEditConfig(newConfig)

    setActionLoading('toggle')
    setError(null)

    try {
      const response = await fetch('/api/admin/night-mode', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(newConfig),
      })

      if (!response.ok) {
        throw new Error('Fehler beim Umschalten')
      }

      setSuccessMessage(newConfig.enabled ? 'Nachtmodus aktiviert' : 'Nachtmodus deaktiviert')
      setTimeout(() => setSuccessMessage(null), 3000)
      fetchData()
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Umschalten fehlgeschlagen')
      // Zuruecksetzen bei Fehler
      setEditConfig({ ...editConfig })
    } finally {
      setActionLoading(null)
    }
  }

  const cancelEdit = () => {
    setEditMode(false)
    setEditConfig(status?.config || null)
  }

  const runningCount = Object.values(status?.services_status || {}).filter(
    s => s.toLowerCase() === 'running' || s.toLowerCase().includes('up')
  ).length
  const stoppedCount = Object.values(status?.services_status || {}).filter(
    s => s.toLowerCase() === 'exited' || s.toLowerCase().includes('exit')
  ).length

  return {
    status,
    services,
    loading,
    actionLoading,
    error,
    successMessage,
    editMode,
    setEditMode,
    editConfig,
    setEditConfig,
    saveConfig,
    executeAction,
    toggleEnabled,
    cancelEdit,
    runningCount,
    stoppedCount,
  }
}
