import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { api, ApiError, type ApiResponse } from '../lib/api';
import type {
  HolidayOverride, NationalHoliday, ResourceAvailability,
  ResourceCalendarDays, ResourceException, WorkingHoursPayload,
} from '../types';

function fail(e: unknown, fallback: string) {
  toast.error(e instanceof ApiError ? e.message : fallback);
}

export function useResourceCalendar(resourceUuid: string | undefined) {
  const qc = useQueryClient();
  const key = ['resource-calendar', resourceUuid];

  const query = useQuery({
    queryKey: key,
    queryFn: () => api.get<ApiResponse<ResourceCalendarDays>>(`/api/v1/resource/${resourceUuid}/calendar`),
    enabled: !!resourceUuid,
  });

  /** PUT جایگزینی کامل است: روزی که نفرستید خالی می‌شود. */
  const save = useMutation({
    mutationFn: (days: WorkingHoursPayload) =>
      api.put<ApiResponse<ResourceCalendarDays>>(`/api/v1/resource/${resourceUuid}/calendar`, { days }),
    onSuccess: () => {
      toast.success('شیفت‌ها ذخیره شد');
      qc.invalidateQueries({ queryKey: key });
      qc.invalidateQueries({ queryKey: ['resource-availability', resourceUuid] });
    },
    onError: (e) => fail(e, 'ذخیرهٔ شیفت‌ها ناموفق بود'),
  });

  return { calendar: query.data?.data, loading: query.isLoading, save };
}

export function useResourceExceptions(resourceUuid: string | undefined) {
  const qc = useQueryClient();
  const key = ['resource-exceptions', resourceUuid];

  const invalidate = () => {
    qc.invalidateQueries({ queryKey: key });
    qc.invalidateQueries({ queryKey: ['resource-availability', resourceUuid] });
  };

  const query = useQuery({
    queryKey: key,
    queryFn: () => api.get<ApiResponse<ResourceException[]>>(`/api/v1/resource/${resourceUuid}/exceptions`),
    enabled: !!resourceUuid,
  });

  const create = useMutation({
    mutationFn: (d: { type: string; starts_at: number; ends_at: number; reason?: string | null }) =>
      api.post<ApiResponse<ResourceException>>(`/api/v1/resource/${resourceUuid}/exception`, d),
    onSuccess: () => { toast.success('استثنا ثبت شد'); invalidate(); },
    onError: (e) => fail(e, 'ثبت استثنا ناموفق بود'),
  });

  const remove = useMutation({
    mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/resource-exception/${uuid}`),
    onSuccess: () => { toast.success('استثنا حذف شد'); invalidate(); },
    onError: (e) => fail(e, 'حذف استثنا ناموفق بود'),
  });

  return { exceptions: query.data?.data ?? [], loading: query.isLoading, create, remove };
}

export function useResourceAvailability(resourceUuid: string | undefined, from: number, to: number) {
  const query = useQuery({
    queryKey: ['resource-availability', resourceUuid, from, to],
    queryFn: () =>
      api.get<ApiResponse<ResourceAvailability>>(`/api/v1/resource/${resourceUuid}/availability?from=${from}&to=${to}`),
    enabled: !!resourceUuid,
  });

  return { availability: query.data?.data, loading: query.isLoading };
}

export function useHolidays(year: number) {
  const qc = useQueryClient();
  const key = ['national-holidays', year];

  const query = useQuery({
    queryKey: key,
    queryFn: () =>
      api.get<ApiResponse<{ year: number; holidays: NationalHoliday[]; overrides: HolidayOverride[] }>>(
        `/api/v1/national-holidays?year=${year}`,
      ),
  });

  const setOverride = useMutation({
    mutationFn: (d: { date: number; is_working: boolean; note?: string | null }) =>
      api.post<ApiResponse<HolidayOverride>>('/api/v1/holiday-overrides', d),
    onSuccess: () => { toast.success('استثنای تعطیلی ذخیره شد'); qc.invalidateQueries({ queryKey: key }); },
    onError: (e) => fail(e, 'ذخیرهٔ استثنا ناموفق بود'),
  });

  const removeOverride = useMutation({
    mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/holiday-override/${uuid}`),
    onSuccess: () => { toast.success('استثنا حذف شد'); qc.invalidateQueries({ queryKey: key }); },
    onError: (e) => fail(e, 'حذف استثنا ناموفق بود'),
  });

  return {
    holidays: query.data?.data?.holidays ?? [],
    overrides: query.data?.data?.overrides ?? [],
    loading: query.isLoading,
    setOverride,
    removeOverride,
  };
}
