import { getRouterParam, readBody } from 'h3';
import {
  restoreTransaction,
  updateTransaction,
  type TransactionStatus,
} from '~/utils/finance-repository';
import { useResponseError, useResponseSuccess } from '~/utils/response';

const ALLOWED_STATUSES: TransactionStatus[] = [
  'draft',
  'pending',
  'approved',
  'rejected',
  'paid',
];

export default defineEventHandler(async (event) => {
  const id = Number(getRouterParam(event, 'id'));
  if (Number.isNaN(id)) {
    return useResponseError('参数错误', -1);
  }

  const body = await readBody(event);

  if (body?.isDeleted === false) {
    const restored = restoreTransaction(id);
    if (!restored) {
      return useResponseError('报销单不存在', -1);
    }
    return useResponseSuccess(restored);
  }

  const payload: Record<string, unknown> = {};

  if (body?.type) payload.type = body.type;
  if (body?.amount !== undefined) {
    const amount = Number(body.amount);
    if (Number.isNaN(amount)) {
      return useResponseError('金额格式不正确', -1);
    }
    payload.amount = amount;
  }
  if (body?.currency) payload.currency = body.currency;
  if (body?.categoryId !== undefined)
    payload.categoryId = body.categoryId ?? null;
  if (body?.accountId !== undefined) payload.accountId = body.accountId ?? null;
  if (body?.transactionDate) payload.transactionDate = body.transactionDate;
  if (body?.description !== undefined)
    payload.description = body.description ?? '';
  if (body?.project !== undefined) payload.project = body.project ?? null;
  if (body?.memo !== undefined) payload.memo = body.memo ?? null;
  if (body?.isDeleted !== undefined) payload.isDeleted = body.isDeleted;
  if (body?.status !== undefined) {
    const status = body.status as TransactionStatus;
    if (!ALLOWED_STATUSES.includes(status)) {
      return useResponseError('状态值不合法', -1);
    }
    payload.status = status;
  }
  if (body?.statusUpdatedAt !== undefined) {
    payload.statusUpdatedAt = body.statusUpdatedAt;
  }
  if (body?.reimbursementBatch !== undefined) {
    payload.reimbursementBatch = body.reimbursementBatch ?? null;
  }
  if (body?.reviewNotes !== undefined) {
    payload.reviewNotes = body.reviewNotes ?? null;
  }
  if (body?.submittedBy !== undefined) {
    payload.submittedBy = body.submittedBy ?? null;
  }
  if (body?.approvedBy !== undefined) {
    payload.approvedBy = body.approvedBy ?? null;
  }
  if (body?.approvedAt !== undefined) {
    payload.approvedAt = body.approvedAt ?? null;
  }

  const updated = updateTransaction(id, payload);
  if (!updated) {
    return useResponseError('报销单不存在', -1);
  }

  return useResponseSuccess(updated);
});
