/**
 * ActaVivienda Entity
 * Viviendas incluidas en actas oficiales
 *
 * @module Infonavit
 * @table infonavit.acta_viviendas
 */

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 { Acta } from './acta.entity';
import { OfertaVivienda } from './oferta-vivienda.entity';

export type ActaViviendaResult = 'pending' | 'approved' | 'rejected' | 'observations';

@Entity({ schema: 'infonavit', name: 'acta_viviendas' })
@Index(['tenantId', 'actaId', 'ofertaId'], { unique: true })
export class ActaVivienda {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'acta_id', type: 'uuid' })
  actaId: string;

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

  @Column({ name: 'sequence_number', type: 'integer' })
  sequenceNumber: number;

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

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

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

  @Column({ name: 'inspected_by', type: 'uuid', nullable: true })
  inspectedById: 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;

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

  @ManyToOne(() => Acta, (a) => a.viviendas, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'acta_id' })
  acta: Acta;

  @ManyToOne(() => OfertaVivienda)
  @JoinColumn({ name: 'oferta_id' })
  oferta: OfertaVivienda;

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'inspected_by' })
  inspectedBy: User;
}
