import { getQuery } from 'h3';
import {
  fetchTransactions,
  type TransactionStatus,
} from '~/utils/finance-repository';
import { useResponseSuccess } from '~/utils/response';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const type = query.type as string | undefined;
  const includeDeleted = query.includeDeleted === 'true';
  const rawStatuses = (query.statuses ?? query.status) as
    | string
    | undefined;
  const statuses = rawStatuses
    ? (rawStatuses
        .split(',')
        .map((item) => item.trim())
        .filter((item) => item.length > 0) as TransactionStatus[])
    : (['approved', 'paid'] satisfies TransactionStatus[]);
  const transactions = fetchTransactions({
    type,
    includeDeleted,
    statuses,
  });

  return useResponseSuccess(transactions);
});
