import axios from 'axios';
import {
  Holiday,
  HolidaysForYearResponse,
  HolidaysForDepartmentResponse,
  UpcomingHolidaysResponse,
  CheckHolidayResponse,
  Bundesland,
  HolidayStatisticsResponse,
  HolidayFormData,
} from '../types/holiday';

const BASE_URL = '/zeiterfassung/holidays';

/**
 * Get all holidays with optional filtering
 */
export const getHolidays = async (params?: {
  bundesland?: string | 'national';
  active?: boolean;
  calculation_type?: 'fixed' | 'easter_relative';
  year?: number;
}): Promise<Holiday[]> => {
  const response = await axios.get<Holiday[]>(BASE_URL, { params });
  return response.data;
};

/**
 * Get a single holiday by ID
 */
export const getHoliday = async (id: number): Promise<Holiday> => {
  const response = await axios.get<Holiday>(`${BASE_URL}/${id}`);
  return response.data;
};

/**
 * Create a new holiday
 */
export const createHoliday = async (data: HolidayFormData): Promise<Holiday> => {
  const response = await axios.post<Holiday>(BASE_URL, data);
  return response.data;
};

/**
 * Update an existing holiday
 */
export const updateHoliday = async (
  id: number,
  data: Partial<HolidayFormData>
): Promise<Holiday> => {
  const response = await axios.put<Holiday>(`${BASE_URL}/${id}`, data);
  return response.data;
};

/**
 * Delete a holiday
 */
export const deleteHoliday = async (id: number): Promise<void> => {
  await axios.delete(`${BASE_URL}/${id}`);
};

/**
 * Get all German federal states (Bundesländer)
 */
export const getBundeslaender = async (): Promise<Bundesland[]> => {
  const response = await axios.get<Bundesland[]>(`${BASE_URL}/bundeslaender`);
  return response.data;
};

/**
 * Get holidays for a specific year and optional Bundesland
 */
export const getHolidaysForYear = async (
  year: number,
  bundesland?: string
): Promise<HolidaysForYearResponse> => {
  const response = await axios.get<HolidaysForYearResponse>(
    `${BASE_URL}/for-year`,
    {
      params: { year, bundesland },
    }
  );
  return response.data;
};

/**
 * Get holidays for a specific department
 */
export const getHolidaysForDepartment = async (
  berId: number,
  year: number
): Promise<HolidaysForDepartmentResponse> => {
  const response = await axios.get<HolidaysForDepartmentResponse>(
    `${BASE_URL}/for-department`,
    {
      params: { ber_id: berId, year },
    }
  );
  return response.data;
};

/**
 * Get upcoming holidays for a department
 */
export const getUpcomingHolidays = async (
  berId: number,
  limit: number = 5
): Promise<UpcomingHolidaysResponse> => {
  const response = await axios.get<UpcomingHolidaysResponse>(
    `${BASE_URL}/upcoming`,
    {
      params: { ber_id: berId, limit },
    }
  );
  return response.data;
};

/**
 * Check if a specific date is a holiday for a department
 */
export const checkHoliday = async (
  date: string,
  berId: number
): Promise<CheckHolidayResponse> => {
  const response = await axios.get<CheckHolidayResponse>(`${BASE_URL}/check`, {
    params: { date, ber_id: berId },
  });
  return response.data;
};

/**
 * Get holiday statistics
 */
export const getHolidayStatistics = async (
  bundesland?: string
): Promise<HolidayStatisticsResponse> => {
  const response = await axios.get<HolidayStatisticsResponse>(
    `${BASE_URL}/statistics`,
    {
      params: { bundesland },
    }
  );
  return response.data;
};

/**
 * Clear holiday cache
 */
export const clearHolidayCache = async (): Promise<void> => {
  await axios.post(`${BASE_URL}/clear-cache`);
};
