import { A, buildContract } from '@jig-software/trest-core';
import { z } from 'zod';
import { Attendance } from '../schemas';

// Schema to be completed
export const AttendanceInit = Attendance.omit({
  _id: true,
  createdAt: true,
  updatedAt: true,
});
export type AttendanceInit = z.infer<typeof AttendanceInit>;

export const GetAttendanceQuery = Attendance.pick({
  staff: true,
}).partial();
export type GetAttendanceQuery = z.infer<typeof Attendance>;

export const attendanceContract = buildContract((c) =>
  c
    .prefix('/attendances')
    .parse((p) => p.error.text(400))
    .endpoints({
      upsertAttendance: (e) =>
        e
          .method('PUT')
          .prepare((p) => p.body.json(AttendanceInit).annotate<[init: A]>())
          .parse((p) => p.success.object(201, Attendance)),
      getAttendance: (e) =>
        e
          .method('GET')
          .prepare((p) => p.query(GetAttendanceQuery.optional()))
          .parse((p) => p.success.object(200, z.array(Attendance))),
    }),
);
