/**
 * Acta Entity
 * Actas oficiales INFONAVIT (entregas, inspecciones, etc.)
 *
 * @module Infonavit
 * @table infonavit.actas
 */

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 { ActaVivienda } from './acta-vivienda.entity';

export type ActaTipo = 'delivery' | 'inspection' | 'verification' | 'closure' | 'other';
export type ActaStatus = 'draft' | 'scheduled' | 'in_progress' | 'completed' | 'cancelled';

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

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

  @Column({ name: 'registro_id', type: 'uuid' })
  registroId: string;

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

  @Column({ name: 'acta_type', type: 'varchar', length: 20 })
  actaType: ActaTipo;

  @Column({ name: 'scheduled_date', type: 'date' })
  scheduledDate: Date;

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

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

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

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

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

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

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

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

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

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

  @OneToMany(() => ActaVivienda, (av) => av.acta)
  viviendas: ActaVivienda[];
}
