import { useCallback, useEffect, useState } from 'react'
import { backendClient } from '../api/backend'
import type {
  ChartDayPoint,
  DailyEconomics,
  DailyEconomicsResponse,
  IntervalEconomics,
  LockResponse,
} from '../types/economics'

export function useEconomicsDaily(siteId: number | null, month: string) {
  const [days, setDays] = useState<DailyEconomics[]>([])
  const [hasGreenBonus, setHasGreenBonus] = useState(false)
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  const load = useCallback(async () => {
    if (siteId == null || !month) return
    setLoading(true)
    setError(null)
    try {
      const { data } = await backendClient.get<DailyEconomicsResponse>(
        `/sites/${siteId}/economics/daily`,
        { params: { month }, timeout: 30_000 },
      )
      setDays(data.days ?? [])
      setHasGreenBonus(data.has_green_bonus ?? false)
    } catch {
      setDays([])
      setError('Nepodařilo se načíst ekonomiku')
    } finally {
      setLoading(false)
    }
  }, [siteId, month])

  useEffect(() => {
    void load()
  }, [load])

  return { days, hasGreenBonus, loading, error, reload: load }
}

export function useEconomicsIntervals(siteId: number | null, day: string | null) {
  const [intervals, setIntervals] = useState<IntervalEconomics[]>([])
  const [loading, setLoading] = useState(false)

  const load = useCallback(async () => {
    if (siteId == null || !day) {
      setIntervals([])
      return
    }
    setLoading(true)
    try {
      const { data } = await backendClient.get<IntervalEconomics[]>(
        `/sites/${siteId}/economics/daily/${day}/intervals`,
        { timeout: 30_000 },
      )
      setIntervals(Array.isArray(data) ? data : [])
    } catch {
      setIntervals([])
    } finally {
      setLoading(false)
    }
  }, [siteId, day])

  useEffect(() => {
    void load()
  }, [load])

  return { intervals, loading }
}

export function useEconomicsChart(siteId: number | null, month: string) {
  const [points, setPoints] = useState<ChartDayPoint[]>([])
  const [loading, setLoading] = useState(false)

  const load = useCallback(async () => {
    if (siteId == null || !month) return
    setLoading(true)
    try {
      const { data } = await backendClient.get<ChartDayPoint[]>(
        `/sites/${siteId}/economics/monthly-chart`,
        { params: { month }, timeout: 30_000 },
      )
      setPoints(Array.isArray(data) ? data : [])
    } catch {
      setPoints([])
    } finally {
      setLoading(false)
    }
  }, [siteId, month])

  useEffect(() => {
    void load()
  }, [load])

  return { points, loading }
}

export async function lockDay(siteId: number, day: string): Promise<LockResponse> {
  const { data } = await backendClient.post<LockResponse>(
    `/sites/${siteId}/economics/daily/${day}/lock`,
  )
  return data
}

export async function unlockDay(siteId: number, day: string): Promise<LockResponse> {
  const { data } = await backendClient.delete<LockResponse>(
    `/sites/${siteId}/economics/daily/${day}/lock`,
  )
  return data
}
