import { Prisma } from '@prisma/client';
import prisma from '../lib/prisma';

type TxClient = Prisma.TransactionClient;

export async function nextOrderNo(tx?: TxClient): Promise<string> {
  const client = tx || prisma;
  const seq = await client.sequence.update({
    where: { key: 'order' },
    data: { value: { increment: 1 } },
  });
  return `WO-${String(seq.value).padStart(5, '0')}`;
}

export async function nextReceiptNo(tx?: TxClient): Promise<string> {
  const client = tx || prisma;
  const seq = await client.sequence.update({
    where: { key: 'receipt' },
    data: { value: { increment: 1 } },
  });
  return `RCP-${String(seq.value).padStart(5, '0')}`;
}

export async function nextAppointmentNo(tx?: TxClient): Promise<string> {
  const client = tx || prisma;
  const seq = await client.sequence.update({
    where: { key: 'appointment' },
    data: { value: { increment: 1 } },
  });
  return `APT-${String(seq.value).padStart(5, '0')}`;
}
