/**
 * AsignacionVivienda Entity
 * Asignaciones de vivienda a derechohabientes
 *
 * @module Infonavit
 * @table infonavit.asignaciones_vivienda
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { OfertaVivienda } from './oferta-vivienda.entity';
import { Derechohabiente } from './derechohabiente.entity';

export type AsignacionStatus = 'pending' | 'approved' | 'formalized' | 'delivered' | 'cancelled';

@Entity({ schema: 'infonavit', name: 'asignaciones_vivienda' })
@Index(['tenantId', 'assignmentNumber'], { unique: true })
export class AsignacionVivienda {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'oferta_id', type: 'uuid' })
  ofertaId: string;

  @Column({ name: 'derechohabiente_id', type: 'uuid' })
  derechohabienteId: string;

  @Column({ name: 'assignment_number', type: 'varchar', length: 50 })
  assignmentNumber: string;

  @Column({ name: 'assignment_date', type: 'date' })
  assignmentDate: Date;

  @Column({ name: 'approval_date', type: 'date', nullable: true })
  approvalDate: Date;

  @Column({ name: 'formalization_date', type: 'date', nullable: true })
  formalizationDate: Date;

  @Column({ name: 'delivery_date', type: 'date', nullable: true })
  deliveryDate: Date;

  @Column({ name: 'credit_amount', type: 'decimal', precision: 14, scale: 2 })
  creditAmount: number;

  @Column({ name: 'subsidy_amount', type: 'decimal', precision: 14, scale: 2, default: 0 })
  subsidyAmount: number;

  @Column({ name: 'down_payment', type: 'decimal', precision: 14, scale: 2, default: 0 })
  downPayment: number;

  @Column({ type: 'varchar', length: 20, default: 'pending' })
  status: AsignacionStatus;

  @Column({ name: 'notary_name', type: 'varchar', length: 200, nullable: true })
  notaryName: string;

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

  @Column({ name: 'deed_number', type: 'varchar', length: 50, nullable: true })
  deedNumber: string;

  @Column({ name: 'deed_date', type: 'date', nullable: true })
  deedDate: Date;

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

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdById: string;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedById: string;

  // Computed property
  get totalAmount(): number {
    return Number(this.creditAmount) + Number(this.subsidyAmount) + Number(this.downPayment);
  }

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => OfertaVivienda, (o) => o.asignaciones, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'oferta_id' })
  oferta: OfertaVivienda;

  @ManyToOne(() => Derechohabiente, (d) => d.asignaciones)
  @JoinColumn({ name: 'derechohabiente_id' })
  derechohabiente: Derechohabiente;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User;
}
