import {
    Column,
    CreateDateColumn,
    DeleteDateColumn,
    Entity,
    Index,
    JoinColumn,
    ManyToOne,
    OneToMany,
    PrimaryGeneratedColumn,
    UpdateDateColumn,
  } from 'typeorm';
  import { Office } from './office.entity';
  import { Member } from './member.entity';
  import { ApplicantSaving } from './applicant-saving.entity';
  import { ApplicantIncome } from './applicant-income.entity';
  import { ApplicantCharge } from './applicant-charge.entity';
  import { Guarantor } from './guarantor.entity';
  import { ApplicantBusinessIncome } from './applicant-business-income.entity';
  import { ApplicantPayableInstitution } from './applicant-payable-institution.entity';
  import { ApplicantFinancialTransaction } from './applicant-financial-transaction.entity';
  import { ApplicationRelation } from './application-relation.entity';

  @Entity({ name: 'applications' })
  export class Application {
    @PrimaryGeneratedColumn('uuid')
    id!: string;

    @Index()
    @Column({ type: 'varchar', name: 'application_number', nullable: true })
    applicationNumber?: string;

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

    @ManyToOne(() => Office, { nullable: true, onDelete: 'SET NULL' })
    @JoinColumn({ name: 'office_id' })
    office?: Office;

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

    @ManyToOne(() => Member, { nullable: true, onDelete: 'SET NULL' })
    @JoinColumn({ name: 'member_id' })
    member?: Member;

    // ── Multi-branch / office codes ───────────────────────
    @Column({ type: 'varchar', name: 'head_office', nullable: true })
    headOffice?: string;

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

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

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

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

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

    // collateral-based loan type: 'home' | 'vehicle' | 'anshiyar' | 'savings' | 'microfinance' | 'general'
    @Column({ type: 'varchar', name: 'loan_type', nullable: true })
    loanType?: string;

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

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

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

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

    // ── Dates ─────────────────────────────────────────────
    @Column({ type: 'varchar', name: 'application_date', length: 20, nullable: true })
    applicationDate?: string;

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

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

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

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

    // ── Meeting info ──────────────────────────────────────
    @Column({ type: 'varchar', name: 'meeting_date', length: 20, nullable: true })
    meetingDate?: string;

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

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

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

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

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

    // ── Approval team & loan account ──────────────────────
    @Column({ type: 'varchar', name: 'approval_team', nullable: true })
    approvalTeam?: string;

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

    // ── Status ────────────────────────────────────────────
    // draft | submitted | under_review | approved | rejected | disbursed
    @Column({ type: 'varchar', length: 30, default: 'draft' })
    status!: string;

    // ── Income summary ────────────────────────────────────
    @Column({ type: 'varchar', name: 'total_monthly_income', nullable: true })
    totalMonthlyIncome?: string;

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

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

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

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

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

    // ── Collateral info ───────────────────────────────────
    @Column({ type: 'varchar', name: 'collateral_type', nullable: true })
    collateralType?: string;

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

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

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

    // ── Relations ─────────────────────────────────────────
    @OneToMany(() => ApplicantSaving, (s) => s.application, { cascade: true, eager: false })
    savings?: ApplicantSaving[];

    @OneToMany(() => ApplicantIncome, (i) => i.application, { cascade: true, eager: false })
    incomes?: ApplicantIncome[];

    @OneToMany(() => ApplicantCharge, (c) => c.application, { cascade: true, eager: false })
    charges?: ApplicantCharge[];

    @OneToMany(() => ApplicantBusinessIncome, (b) => b.application, { cascade: true, eager: false })
    businessIncomes?: ApplicantBusinessIncome[];

    @OneToMany(() => ApplicantPayableInstitution, (p) => p.application, { cascade: true, eager: false })
    payableInstitutions?: ApplicantPayableInstitution[];

    @OneToMany(() => ApplicantFinancialTransaction, (f) => f.application, { cascade: true, eager: false })
    financialTransactions?: ApplicantFinancialTransaction[];

    @OneToMany(() => ApplicationRelation, (r) => r.application, { cascade: true, eager: false })
    relations?: ApplicationRelation[];

    @OneToMany(() => Guarantor, (g) => g.application, { cascade: true, eager: false })
    guarantors?: Guarantor[];

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

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

    @CreateDateColumn()
    createdAt!: Date;

    @UpdateDateColumn()
    updatedAt!: Date;

    @DeleteDateColumn()
    deletedAt?: Date;
  }
