/**
 * ReporteInfonavit Entity
 * Reportes generados para INFONAVIT
 *
 * @module Infonavit
 * @table infonavit.reportes_infonavit
 */

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

export type ReporteTipo = 'monthly' | 'quarterly' | 'annual' | 'progress' | 'delivery' | 'custom';
export type ReporteStatus = 'draft' | 'generated' | 'submitted' | 'accepted' | 'rejected';

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

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

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

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

  @Column({ name: 'report_type', type: 'varchar', length: 20 })
  reportType: ReporteTipo;

  @Column({ name: 'period_start', type: 'date' })
  periodStart: Date;

  @Column({ name: 'period_end', type: 'date' })
  periodEnd: Date;

  @Column({ name: 'generation_date', type: 'date' })
  generationDate: Date;

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

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

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

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

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

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

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

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

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

  @Column({ name: 'rejection_reason', type: 'text', nullable: true })
  rejectionReason: 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: 'submitted_by', type: 'uuid', nullable: true })
  submittedById: string;

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

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'submitted_by' })
  submittedBy: User;
}
