/**
 * Derechohabiente Entity
 * Derechohabientes INFONAVIT (trabajadores con crédito)
 *
 * @module Infonavit
 * @table infonavit.derechohabientes
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { AsignacionVivienda } from './asignacion-vivienda.entity';
import { HistoricoPuntos } from './historico-puntos.entity';

export type DerechohabienteStatus = 'active' | 'pre_qualified' | 'qualified' | 'assigned' | 'inactive';

@Entity({ schema: 'infonavit', name: 'derechohabientes' })
@Index(['tenantId', 'nss'], { unique: true })
@Index(['tenantId', 'curp'], { unique: true })
export class Derechohabiente {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'varchar', length: 11 })
  nss: string;

  @Column({ type: 'varchar', length: 18 })
  curp: string;

  @Column({ type: 'varchar', length: 13, nullable: true })
  rfc: string;

  @Column({ name: 'first_name', type: 'varchar', length: 100 })
  firstName: string;

  @Column({ name: 'last_name', type: 'varchar', length: 100 })
  lastName: string;

  @Column({ name: 'second_last_name', type: 'varchar', length: 100, nullable: true })
  secondLastName: string;

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

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

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

  @Column({ name: 'credit_points', type: 'integer', default: 0 })
  creditPoints: number;

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

  @Column({ name: 'precalification_amount', type: 'decimal', precision: 14, scale: 2, nullable: true })
  precalificationAmount: number;

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

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

  @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;

  @Column({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;

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

  // Computed property
  get fullName(): string {
    return `${this.firstName} ${this.lastName}${this.secondLastName ? ' ' + this.secondLastName : ''}`;
  }

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

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

  @OneToMany(() => AsignacionVivienda, (a) => a.derechohabiente)
  asignaciones: AsignacionVivienda[];

  @OneToMany(() => HistoricoPuntos, (h) => h.derechohabiente)
  historicoPuntos: HistoricoPuntos[];
}
