import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Application } from './application.entity';

/**
 * Existing loans / liabilities at other financial institutions
 * Maps to old table: application_payable_other_financial_institutions
 */
@Entity({ name: 'applicant_payable_institutions' })
export class ApplicantPayableInstitution {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ type: 'varchar', name: 'application_id' })
  applicationId!: string;

  @ManyToOne(() => Application, (a) => a.payableInstitutions, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'application_id' })
  application!: Application;

  @Column({ type: 'varchar', name: 'office_name', nullable: true })
  officeName?: string;

  @Column({ type: 'varchar', nullable: true, default: '0' })
  daily?: string;

  @Column({ type: 'varchar', nullable: true, default: '0' })
  monthly?: string;

  @Column({ type: 'varchar', nullable: true, default: '0' })
  yearly?: string;

  // total amount owed to the institution
  @Column({ type: 'varchar', name: 'amount_owed', nullable: true, default: '0' })
  amountOwed?: string;

  // whether it exceeds the allowed limit
  @Column({ default: false })
  exceeded!: boolean;

  @Column({ type: 'varchar', nullable: true, default: '0' })
  amount?: string;

  // 'daily' | 'monthly' | 'yearly'
  @Column({ type: 'varchar', name: 'amount_type', nullable: true, default: 'monthly' })
  amountType?: string;

  @Column({ type: 'varchar', nullable: true })
  remarks?: string;

  @Column({ type: 'varchar', name: 'loan_purpose', nullable: true })
  loanPurpose?: string;

  @Column({ type: 'varchar', name: 'loan_amount', nullable: true })
  loanAmount?: string;

  @Column({ type: 'varchar', name: 'loan_approved_date', length: 20, nullable: true })
  loanApprovedDate?: string;

  @Column({ type: 'varchar', name: 'loan_approved_date_bs', length: 20, nullable: true })
  loanApprovedDateBs?: string;

  @Column({ default: 0 })
  order!: number;

  @Column({ type: 'varchar', name: 'created_by', nullable: true })
  createdBy?: string;

  @Column({ type: 'varchar', name: 'updated_by', nullable: true })
  updatedBy?: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt!: Date;

  @DeleteDateColumn({ name: 'deleted_at' })
  deletedAt?: Date;
}
