import apiClient from './authApi';

export interface AdminConfiguration {
  configId: string;
  configKey: string;
  configCategory: string;
  configValue: string;
  valueType: 'STRING' | 'NUMBER' | 'BOOLEAN' | 'JSON' | 'ARRAY';
  displayName: string;
  description?: string;
  defaultValue?: string;
  isEditable: boolean;
  isSensitive: boolean;
  validationRules?: {
    min?: number;
    max?: number;
    regex?: string;
    required?: boolean;
  };
  uiComponent?: string;
  options?: any;
  sortOrder: number;
  requiresRestart: boolean;
  lastModifiedBy?: string;
  lastModifiedAt?: string;
}

export interface Holiday {
  holidayId: string;
  holidayDate: string;
  holidayName: string;
  description?: string;
  holidayType: 'NATIONAL' | 'REGIONAL' | 'ORGANIZATIONAL' | 'OPTIONAL';
  isRecurring: boolean;
  recurrenceRule?: string;
  isActive: boolean;
  appliesToDepartments?: string[];
  appliesToLocations?: string[];
}

/**
 * Get public configurations (accessible to all authenticated users)
 * Returns non-sensitive configurations like DOCUMENT_POLICY, WORKFLOW_SHARING, TAT_SETTINGS
 */
export const getPublicConfigurations = async (category?: string): Promise<AdminConfiguration[]> => {
  const params = category ? { category } : {};
  const response = await apiClient.get('/users/configurations', { params });
  return response.data.data;
};

/**
 * Get all admin configurations (admin only)
 */
export const getAllConfigurations = async (category?: string): Promise<AdminConfiguration[]> => {
  const params = category ? { category } : {};
  const response = await apiClient.get('/admin/configurations', { params });
  return response.data.data;
};

/**
 * Update a configuration value
 */
export const updateConfiguration = async (
  configKey: string,
  configValue: string
): Promise<void> => {
  await apiClient.put(`/admin/configurations/${configKey}`, { configValue });
};

/**
 * Reset configuration to default
 */
export const resetConfiguration = async (configKey: string): Promise<void> => {
  await apiClient.post(`/admin/configurations/${configKey}/reset`);
};

/**
 * Get all holidays
 */
export const getAllHolidays = async (year?: number): Promise<Holiday[]> => {
  const params = year ? { year } : {};
  const response = await apiClient.get('/admin/holidays', { params });
  return response.data.data;
};

/**
 * Get holiday calendar for a year
 */
export const getHolidayCalendar = async (year: number): Promise<any> => {
  const response = await apiClient.get(`/admin/holidays/calendar/${year}`);
  return response.data;
};

/**
 * Create a new holiday
 */
export const createHoliday = async (holiday: Partial<Holiday>): Promise<Holiday> => {
  const response = await apiClient.post('/admin/holidays', holiday);
  return response.data.data;
};

/**
 * Update a holiday
 */
export const updateHoliday = async (
  holidayId: string,
  updates: Partial<Holiday>
): Promise<Holiday> => {
  const response = await apiClient.put(`/admin/holidays/${holidayId}`, updates);
  return response.data.data;
};

/**
 * Delete a holiday
 */
export const deleteHoliday = async (holidayId: string): Promise<void> => {
  await apiClient.delete(`/admin/holidays/${holidayId}`);
};

/**
 * Bulk import holidays
 */
export const bulkImportHolidays = async (holidays: Partial<Holiday>[]): Promise<any> => {
  const response = await apiClient.post('/admin/holidays/bulk-import', { holidays });
  return response.data;
};

