/**
 * DocumentoStps Entity
 * Documentos STPS emitidos (DC-1, DC-2, DC-3, etc.)
 *
 * @module HSE
 * @table hse.documentos_stps
 * @ddl schemas/03-hse-schema-ddl.sql
 * @rf RF-MAA017-005
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { Fraccionamiento } from '../../construction/entities/fraccionamiento.entity';
import { Employee } from '../../hr/entities/employee.entity';
import { User } from '../../core/entities/user.entity';

export type TipoDocumentoStps = 'dc1' | 'dc2' | 'dc3' | 'dc4' | 'st7' | 'st9';

@Entity({ schema: 'hse', name: 'documentos_stps' })
@Index(['tenantId', 'tipo', 'folio'], { unique: true })
@Index(['fechaVencimiento'])
export class DocumentoStps {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({
    type: 'enum',
    enum: ['dc1', 'dc2', 'dc3', 'dc4', 'st7', 'st9'],
  })
  tipo: TipoDocumentoStps;

  @Column({ type: 'varchar', length: 30 })
  folio: string;

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

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

  @Column({ name: 'fecha_emision', type: 'date' })
  fechaEmision: Date;

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

  @Column({ name: 'datos_documento', type: 'jsonb', nullable: true })
  datosDocumento: Record<string, unknown>;

  @Column({ name: 'documento_url', type: 'varchar', length: 500, nullable: true })
  documentoUrl: string;

  @Column({ type: 'boolean', default: false })
  firmado: boolean;

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

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

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

  @ManyToOne(() => Fraccionamiento)
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @ManyToOne(() => Employee)
  @JoinColumn({ name: 'employee_id' })
  employee: Employee;

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