import { apiClient } from './client';
import { getClientTimeZone } from '@/lib/i18n/clientTimeZone';

export type WorkingHoursPayload = {
  autoRepeatWeekly: boolean;
  blocks: { dayOfWeek: number; startMinute: number; endMinute: number; sortOrder?: number }[];
};

export type ParticipationData = {
  membershipId: string;
  orgType: 'CLINIC' | 'LAB';
  participatesInTreatments: boolean;
  participatesInTasks: boolean;
  hasWorkingHours: boolean;
};

export type ParticipationUpdateData = ParticipationData & {
  permissions?: string[];
};

export const accountApi = {
  getParticipation: async (): Promise<{ success: boolean; data: ParticipationData }> => {
    const response = await apiClient.get('/auth/profile/participation');
    return response.data;
  },

  updateParticipation: async (participate: boolean): Promise<{
    success: boolean;
    data: ParticipationUpdateData;
  }> => {
    const response = await apiClient.patch('/auth/profile/participation', { participate });
    return response.data;
  },

  getMyWorkingHours: async (): Promise<{
    success: boolean;
    data: WorkingHoursPayload & { hasWorkingHours: boolean };
  }> => {
    const response = await apiClient.get('/auth/profile/working-hours');
    return response.data;
  },

  upsertMyWorkingHours: async (
    payload: WorkingHoursPayload,
  ): Promise<{ success: boolean; message: string }> => {
    const response = await apiClient.put('/auth/profile/working-hours', {
      ...payload,
      timeZone: getClientTimeZone(),
    });
    return response.data;
  },
};
