import type { ApiResponse, components } from '@alqove/types';
import type { AlqoveClient } from '../client';

export type Shift = components['schemas']['Shift'];
export type ScheduleWeek = components['schemas']['ScheduleWeek'];
export type ShiftInput = components['schemas']['CreateShift'];
export type ShiftUpdateInput = components['schemas']['UpdateShift'];
export type ScheduleSettings = components['schemas']['ScheduleSettings'];
// OpenAPI defaults make generator properties required even for PATCH; server
// validates each setting with `sometimes`, so omission is explicitly supported.
export type ScheduleSettingsInput = Partial<components['schemas']['UpdateScheduleSettings']>;
export type CopyWeekInput = components['schemas']['CopyScheduleWeek'];
export type CopyShift = components['schemas']['CopyShiftCandidate'];
export type CopyWeekResult = components['schemas']['CopyScheduleWeekResult'];
export type ScheduleDeleted = components['schemas']['ScheduleDeleted'];

export function createSchedulingEndpoints(client: AlqoveClient) {
  return {
    settings: (storeId: string) => client.get<ApiResponse<ScheduleSettings>>(`/v1/stores/${storeId}/schedule/settings`),
    updateSettings: (storeId: string, body: ScheduleSettingsInput) => client.patch<ApiResponse<ScheduleSettings>>(`/v1/stores/${storeId}/schedule/settings`, body),
    createShift: (storeId: string, body: ShiftInput) => client.post<ApiResponse<Shift>>(`/v1/stores/${storeId}/schedule/shifts`, body),
    updateShift: (storeId: string, id: string, body: ShiftUpdateInput) => client.patch<ApiResponse<Shift>>(`/v1/stores/${storeId}/schedule/shifts/${id}`, body),
    deleteShift: (storeId: string, id: string, revision: string) => client.request<ApiResponse<ScheduleDeleted>>('DELETE', `/v1/stores/${storeId}/schedule/shifts/${id}`, { body: { revision } }),
    copyWeek: (storeId: string, body: CopyWeekInput) => client.post<ApiResponse<CopyWeekResult>>(`/v1/stores/${storeId}/schedule/copy-week`, body),
    publish: (storeId: string, weekStart: string) => client.post<ApiResponse<ScheduleWeek>>(`/v1/stores/${storeId}/schedule/publish`, { week_start: weekStart }),
    week: (storeId: string, weekStart: string) => client.get<ApiResponse<ScheduleWeek>>(`/v1/stores/${storeId}/schedule/week`, { week_start: weekStart }),
    myWeek: (storeId: string, weekStart: string) => client.get<ApiResponse<ScheduleWeek>>(`/v1/stores/${storeId}/my/schedule`, { week_start: weekStart }),
  };
}
