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

export type TimePunch = components['schemas']['TimePunch'];
export type ClockStatus = components['schemas']['ClockState'];
export type ClockIntent = components['schemas']['ClockIntent'];
export type Timesheet = components['schemas']['PayrollTimesheet'];
export type PunchInput = components['schemas']['CreateTimePunch'];
export type PunchCorrection = components['schemas']['UpdateTimePunch'];
export type PayrollExport = components['schemas']['PayrollExport'];
export type PayrollExportInput = components['schemas']['PayrollExportRequest'];
const base = (store: string) => `/v1/stores/${encodeURIComponent(store)}`;
export function createTimekeepingEndpoints(client: AlqoveClient) {
  return {
    payroll: {
      list: (store: string) => client.get<ApiResponse<PayrollExport[]>>(`${base(store)}/payroll/exports`),
      create: (store: string, input: PayrollExportInput, key: string) => client.post<ApiResponse<PayrollExport>>(`${base(store)}/payroll/exports`, input, { headers: { 'Idempotency-Key': key } }),
      download: (store: string, id: string) => client.getBlob(`${base(store)}/payroll/exports/${encodeURIComponent(id)}/download`),
    },
    timesheets: {
      own: (store: string) => client.get<ApiResponse<Timesheet[]>>(`${base(store)}/my/timesheets`),
      list: (store: string, week: string) => client.get<ApiResponse<Timesheet[]>>(`${base(store)}/timesheets`, { week_start: week }),
      detail: (store: string, id: string) => client.get<ApiResponse<Timesheet>>(`${base(store)}/timesheets/${encodeURIComponent(id)}`),
      recalculate: (store: string, week: string) => client.post<ApiResponse<Timesheet[]>>(`${base(store)}/timesheets/recalculate`, { week_start: week }),
      approve: (store: string, id: string, acknowledge = false) => client.post<ApiResponse<Timesheet>>(`${base(store)}/timesheets/${encodeURIComponent(id)}/approve`, { acknowledge_oversized_sessions: acknowledge }),
      unlock: (store: string, id: string) => client.post<ApiResponse<Timesheet>>(`${base(store)}/timesheets/${encodeURIComponent(id)}/unlock`, {}),
    },
    punches: {
      list: (store: string, member: string, from: string, to: string) => client.get<ApiResponse<TimePunch[]>>(`${base(store)}/punches`, { store_membership_id: member, from, to }),
      create: (store: string, input: PunchInput) => client.post<ApiResponse<TimePunch>>(`${base(store)}/punches`, input),
      update: (store: string, id: string, input: PunchCorrection) => client.patch<ApiResponse<TimePunch>>(`${base(store)}/punches/${encodeURIComponent(id)}`, input),
      remove: (store: string, id: string, input: components['schemas']['DeleteTimePunch']) => client.request<ApiResponse<{ deleted: true }>>('DELETE', `${base(store)}/punches/${encodeURIComponent(id)}`, { body: input }),
    },
    clock: {
      status: (store: string) => client.get<ApiResponse<ClockStatus>>(`${base(store)}/my/clock`),
      act: (store: string, input: ClockIntent, key: string) => client.post<ApiResponse<ClockStatus>>(`${base(store)}/my/clock`, input, { headers: { 'Idempotency-Key': key } }),
    },
  };
}
