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

export const LeaveTransactionInit = LeaveTransaction.pick({
  leaveType: true,
  amount: true,
  remarks: true,
  grantedTo: true,
  grantedBy: true,
});

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

export const leaveTransactionContract = buildContract((c) =>
  c
    .prefix('/leave-transactions')
    .parse((p) => p.error.text(400))
    .endpoints({
      createLeaveTransaction: (e) =>
        e
          .method('POST')
          .prepare((p) =>
            p.body.json(LeaveTransactionInit).annotate<[init: A]>(),
          )
          .parse((p) => p.success.object(201, LeaveTransaction)),
      getLeaveTransactions: (e) =>
        e
          .method('GET')
          .parse((p) => p.success.object(200, z.array(LeaveTransaction))),
    }),
);
