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

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

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

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

    // guarantor_type: member | non_member | dhan_jamani | sachi
    @Column({ type: 'varchar', name: 'guarantor_type', length: 30, default: 'member' })
    guarantorType!: string;

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

    // external guarantor pool reference
    @Column({ type: 'varchar', name: 'external_guarantor_id', nullable: true })
    externalGuarantorId?: string;

    // ── Personal ──────────────────────────────────────────────
    @Column({ type: 'varchar', length: 200, nullable: true })
    name?: string;

    @Column({ type: 'varchar', name: 'name_en', length: 200, nullable: true })
    nameEn?: string;

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

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

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

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

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

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

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

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

    // ── Permanent address ─────────────────────────────────────
    @Column({ type: 'varchar', name: 'p_province', length: 5, nullable: true })
    pProvince?: string;

    @Column({ type: 'varchar', name: 'p_district', length: 5, nullable: true })
    pDistrict?: string;

    @Column({ type: 'varchar', name: 'p_localgovt', length: 5, nullable: true })
    pLocalgovt?: string;

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

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

    // ── Temporary address ─────────────────────────────────────
    @Column({ type: 'varchar', name: 't_province', length: 5, nullable: true })
    tProvince?: string;

    @Column({ type: 'varchar', name: 't_district', length: 5, nullable: true })
    tDistrict?: string;

    @Column({ type: 'varchar', name: 't_localgovt', length: 5, nullable: true })
    tLocalgovt?: string;

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

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

    // ── Citizenship ───────────────────────────────────────────
    @Column({ type: 'varchar', name: 'citizenship_no', nullable: true })
    citizenshipNo?: string;

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

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

    @Column({ type: 'varchar', name: 'citizenship_issue_district', length: 5, nullable: true })
    citizenshipIssueDistrict?: string;

    // ── Generation fields ─────────────────────────────────────
    @Column({ type: 'varchar', name: 'second_generation', length: 50, nullable: true })
    secondGeneration?: string;

    @Column({ type: 'varchar', name: 'second_generation_name', length: 200, nullable: true })
    secondGenerationName?: string;

    @Column({ type: 'varchar', name: 'third_generation', length: 50, nullable: true })
    thirdGeneration?: string;

    @Column({ type: 'varchar', name: 'third_generation_name', length: 200, nullable: true })
    thirdGenerationName?: string;

    // ── Financial ─────────────────────────────────────────────
    @Column({ type: 'varchar', name: 'subscription_id', nullable: true })
    subscriptionId?: string;

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

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

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

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

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

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

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

    // ── Approval flags ────────────────────────────────────────
    @Column({ default: false, name: 'family_approval' })
    familyApproval!: boolean;

    @Column({ default: false, name: 'saving_approval' })
    savingApproval!: boolean;

    // ── Photos ────────────────────────────────────────────────
    @Column({ type: 'varchar', name: 'photo_url', nullable: true })
    photoUrl?: string;

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

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

    @Column({ type: 'varchar', nullable: true })
    address?: 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()
    createdAt!: Date;

    @UpdateDateColumn()
    updatedAt!: Date;

    @DeleteDateColumn()
    deletedAt?: Date;
  }
