import { Check, Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { CurrencyEntity } from './currency.entity';

export type CashHolderType = 'OWN' | 'SUPPLIER';
export type CashHeldMovementType =
  'TRANSFER_IN' | 'TRANSFER_OUT' | 'PAID_SUPPLIER' | 'CUSTOMER_USE' | 'RETURNED';

/**
 * Cash Held at destination — every movement of company cash held by a
 * holder (own staff, or a supplier's driver) after it was received.
 * Receipts themselves are the confirmed `md_cash_collections` rows
 * flagged `counts_as_held`. Balance per holder + currency =
 * received (COLLECTED) + TRANSFER_IN − all OUT movements.
 * Append-only: no edit / delete (client 2026-10-19 — corrections are
 * opposite movements). See docs/cash-held-at-destination-plan.md.
 */
@Entity('cash_held_movements')
@Index('ix_cash_held_tenant_holder', ['tenant_id', 'holder_key', 'currency_id'], {
  where: '"is_deleted" = false',
})
@Check('chk_cash_held_amount_positive', `"amount" > 0`)
@Check(
  'chk_cash_held_type',
  `"movement_type" IN ('TRANSFER_IN','TRANSFER_OUT','PAID_SUPPLIER','CUSTOMER_USE','RETURNED')`,
)
@Check(
  'chk_cash_held_holder',
  `(
    ("holder_type" = 'OWN' AND "collector_name" IS NOT NULL AND "supplier_id" IS NULL AND "driver_id" IS NULL)
    OR ("holder_type" = 'SUPPLIER' AND "collector_name" IS NULL AND "supplier_id" IS NOT NULL AND "driver_id" IS NOT NULL)
  )`,
)
export class CashHeldMovementEntity extends BaseEntity {
  /** OWN:<normalised name> or SUPPLIER:<supplier_id>:<driver_id>. */
  @Column({ type: 'varchar', length: 300 })
  holder_key: string;

  @Column({ type: 'varchar', length: 20 })
  holder_type: CashHolderType;

  @Column({ type: 'varchar', length: 255, nullable: true })
  collector_name: string | null;

  @Column({ type: 'uuid', nullable: true })
  supplier_id: string | null;

  @Column({ type: 'uuid', nullable: true })
  driver_id: string | null;

  @Column({ type: 'varchar', length: 20 })
  movement_type: CashHeldMovementType;

  /** IN adds to the holder's balance, OUT takes from it. */
  @Column({ type: 'varchar', length: 3 })
  direction: 'IN' | 'OUT';

  @ManyToOne(() => CurrencyEntity)
  @JoinColumn({ name: 'currency_id' })
  currency: CurrencyEntity;

  @Column({ type: 'uuid' })
  currency_id: string;

  @Column({ type: 'decimal', precision: 12, scale: 2 })
  amount: number;

  @Column({ type: 'date' })
  movement_date: string;

  /** CUSTOMER_USE — booking the cash was used for. */
  @Column({ type: 'uuid', nullable: true })
  booking_id: string | null;

  /** TRANSFER_OUT ↔ TRANSFER_IN pair. */
  @Column({ type: 'uuid', nullable: true })
  related_movement_id: string | null;

  /** PAID_SUPPLIER — the Settlements supplier payout it funded. */
  @Column({ type: 'uuid', nullable: true })
  supplier_payout_id: string | null;

  /** RETURNED — office bank account it went back to. */
  @Column({ type: 'uuid', nullable: true })
  bank_account_id: string | null;

  @Column({ type: 'varchar', length: 100, nullable: true })
  ref_no: string | null;

  @Column({ type: 'text', nullable: true })
  notes: string | null;
}
