import { objectId } from '@jig-software/zod-bson';
import { z } from 'zod';
import { LeaveType } from './LeaveType';
import { WorkDay } from './WorkDay';

export const LeaveStatus = z.enum(['pending', 'approved']);

export type LeaveStatus = z.infer<typeof LeaveStatus>;

export const Leave = z.object({
  _id: objectId(),
  status: LeaveStatus,
  staff: objectId(),
  type: LeaveType,
  workDay: WorkDay,
  applyRemarks: z.string().nullish(),
  approveRemarks: z.string().nullish(),
  application: objectId(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export type Leave = z.infer<typeof Leave>;

// Don't change LeaveInit.
export const LeaveInit = Leave.pick({ type: true, workDay: true });
export type LeaveInit = z.infer<typeof LeaveInit>;

export const DateRangeLeaveInit = z.object({
  dateType: z.literal('range'),
  startWorkDay: WorkDay,
  endWorkDay: WorkDay,
  type: LeaveType,
});
export const SingleDateLeaveInit = z.object({
  dateType: z.literal('single'),
  workDay: WorkDay,
  type: LeaveType,
});

export const DateLeaveInit = z.discriminatedUnion('dateType', [
  SingleDateLeaveInit,
  DateRangeLeaveInit,
]);

export type DateLeaveInit = z.infer<typeof DateLeaveInit>;
