/**
 * ENGINE PRIMITIVES — the finite, correctness-critical vocabulary the billing/
 * ledger engine is built on. These live in code (and are mirrored 1:1 as Prisma
 * enums) precisely because they are NOT business configuration.
 *
 * By contrast, BUSINESS TAXONOMY — charge types, tax/interest rates, settlement
 * methods, regions, property categories, approval steps, roles/permissions —
 * lives in the DATABASE as editable rows, so users configure it without a
 * redeploy. If you are tempted to add a value here that a user might one day
 * want to add themselves, it belongs in a table, not in this file.
 */

const asEnum = <T extends Record<string, string>>(o: T) => Object.freeze(o);

// ── Double-entry ledger ─────────────────────────────────────
export const LedgerDirection = asEnum({ DEBIT: 'DEBIT', CREDIT: 'CREDIT' });
export type LedgerDirection = (typeof LedgerDirection)[keyof typeof LedgerDirection];

/** Normal balance side is derived from this. Chart-of-accounts rows carry a type. */
export const GlAccountType = asEnum({
  ASSET: 'ASSET',
  LIABILITY: 'LIABILITY',
  INCOME: 'INCOME',
  EXPENSE: 'EXPENSE',
  EQUITY: 'EQUITY',
});
export type GlAccountType = (typeof GlAccountType)[keyof typeof GlAccountType];

export const JournalSourceType = asEnum({
  INVOICE: 'INVOICE',
  RECEIPT: 'RECEIPT',
  RECEIPT_ALLOCATION: 'RECEIPT_ALLOCATION',
  DEPOSIT: 'DEPOSIT',
  DEPOSIT_ALLOCATION: 'DEPOSIT_ALLOCATION',
  ADJUSTMENT: 'ADJUSTMENT',
  OPENING_BALANCE: 'OPENING_BALANCE',
  WRITE_OFF: 'WRITE_OFF',
  REVERSAL: 'REVERSAL',
});
export type JournalSourceType = (typeof JournalSourceType)[keyof typeof JournalSourceType];

// ── Billing / charges ───────────────────────────────────────
/** Category is a fixed engine concept; individual charge_types are DB rows. */
export const ChargeCategory = asEnum({
  RECURRING: 'RECURRING', // rent, management fee, rates… billed each period
  ONE_OFF: 'ONE_OFF', // stamp duty, one-time fees
  DEPOSIT: 'DEPOSIT', // security deposits collected on the invoice
  UTILITY: 'UTILITY', // water / electricity / gas (metered)
  ADJUSTMENT: 'ADJUSTMENT', // concessions, rent-free, manual corrections
});
export type ChargeCategory = (typeof ChargeCategory)[keyof typeof ChargeCategory];

/** How a recurring charge relates to the period it is billed in. */
export const BillingTiming = asEnum({ ADVANCE: 'ADVANCE', ARREARS: 'ARREARS' });
export type BillingTiming = (typeof BillingTiming)[keyof typeof BillingTiming];

export const ProrationPolicy = asEnum({
  DAILY: 'DAILY', // pro-rata by actual days in period
  FULL_MONTH: 'FULL_MONTH', // always charge the full amount
  NONE: 'NONE', // charge once, no proration
});
export type ProrationPolicy = (typeof ProrationPolicy)[keyof typeof ProrationPolicy];

// ── Invoice (Debit Note) lifecycle ──────────────────────────
// OVERDUE / partially-derived states are computed from ledger + due date at read
// time, never stored, so status can't drift from the money. PENDING_APPROVAL /
// APPROVED are the explicit approval stage: reached only when an approval
// workflow matches the amount, otherwise DRAFT goes straight to ISSUED.
export const InvoiceStatus = asEnum({
  DRAFT: 'DRAFT',
  PENDING_APPROVAL: 'PENDING_APPROVAL',
  APPROVED: 'APPROVED',
  ISSUED: 'ISSUED',
  PARTIALLY_PAID: 'PARTIALLY_PAID',
  PAID: 'PAID',
  VOID: 'VOID',
});
export type InvoiceStatus = (typeof InvoiceStatus)[keyof typeof InvoiceStatus];

export const InvoiceLineKind = asEnum({
  CHARGE: 'CHARGE', // a real charge, references a charge_type
  INTEREST: 'INTEREST', // late interest generated by the engine
  OVERDUE_CARRYFORWARD: 'OVERDUE_CARRYFORWARD', // prior unpaid balance rolled in
  DEPOSIT_APPLICATION: 'DEPOSIT_APPLICATION', // a held deposit applied (negative)
  CREDIT_APPLICATION: 'CREDIT_APPLICATION', // tenant credit applied (negative)
  WAIVER: 'WAIVER', // interest/charge waived (negative)
  ROUNDING: 'ROUNDING',
});
export type InvoiceLineKind = (typeof InvoiceLineKind)[keyof typeof InvoiceLineKind];

// ── Receipts / allocations / deposits ───────────────────────
export const ReceiptStatus = asEnum({
  PENDING: 'PENDING', // tenant-uploaded, awaiting review
  APPROVED: 'APPROVED',
  REJECTED: 'REJECTED',
});
export type ReceiptStatus = (typeof ReceiptStatus)[keyof typeof ReceiptStatus];

export const AllocationStatus = asEnum({
  DRAFT: 'DRAFT',
  CONFIRMED: 'CONFIRMED',
  VOID: 'VOID',
});
export type AllocationStatus = (typeof AllocationStatus)[keyof typeof AllocationStatus];

export const DepositStatus = asEnum({
  HELD: 'HELD',
  PARTIALLY_APPLIED: 'PARTIALLY_APPLIED',
  APPLIED: 'APPLIED',
  REFUNDED: 'REFUNDED',
  VOID: 'VOID',
});
export type DepositStatus = (typeof DepositStatus)[keyof typeof DepositStatus];

// ── Leasing / property ──────────────────────────────────────
export const UnitStatus = asEnum({
  AVAILABLE: 'AVAILABLE',
  OCCUPIED: 'OCCUPIED',
  RESERVED: 'RESERVED',
  SOLD: 'SOLD',
  INACTIVE: 'INACTIVE',
});
export type UnitStatus = (typeof UnitStatus)[keyof typeof UnitStatus];

export const LeaseStatus = asEnum({
  DRAFT: 'DRAFT',
  ACTIVE: 'ACTIVE',
  ENDED: 'ENDED',
  TERMINATED: 'TERMINATED',
  RENEWED: 'RENEWED',
});
export type LeaseStatus = (typeof LeaseStatus)[keyof typeof LeaseStatus];

// ── Approval workflows (config-driven, replaces hardcoded Grace/Long) ──
export const ApprovalActorType = asEnum({ ROLE: 'ROLE', USER: 'USER' });
export type ApprovalActorType = (typeof ApprovalActorType)[keyof typeof ApprovalActorType];

export const ApprovalActionType = asEnum({
  APPROVE: 'APPROVE',
  REJECT: 'REJECT',
  ACKNOWLEDGE: 'ACKNOWLEDGE',
});
export type ApprovalActionType = (typeof ApprovalActionType)[keyof typeof ApprovalActionType];

// ── Reference-data domains (rows live in reference_values table) ──
// A curated list of the lookup "buckets"; the values inside each are DB rows.
export const ReferenceDomain = asEnum({
  REGION: 'REGION',
  AMENITY: 'AMENITY',
  MANAGEMENT_TYPE: 'MANAGEMENT_TYPE',
  LEASE_TYPE: 'LEASE_TYPE',
  PROPERTY_CATEGORY: 'PROPERTY_CATEGORY',
});
export type ReferenceDomain = (typeof ReferenceDomain)[keyof typeof ReferenceDomain];
