/**
 * Inspection Entity
 * Inspecciones de calidad realizadas
 *
 * @module Quality
 * @table quality.inspections
 */

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 { Checklist } from './checklist.entity';
import { InspectionResult } from './inspection-result.entity';
import { NonConformity } from './non-conformity.entity';

export type InspectionStatus = 'pending' | 'in_progress' | 'completed' | 'approved' | 'rejected';

@Entity({ schema: 'construction', name: 'inspecciones' })
@Index(['tenantId', 'inspectionNumber'], { unique: true })
export class Inspection {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'checklist_id', type: 'uuid' })
  checklistId: string;

  @Column({ name: 'lote_id', type: 'uuid' })
  loteId: string;

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

  @Column({ name: 'inspection_date', type: 'date' })
  inspectionDate: Date;

  @Column({ name: 'inspector_id', type: 'uuid' })
  inspectorId: string;

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

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

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

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

  @Column({ name: 'pass_rate', type: 'decimal', precision: 5, scale: 2, nullable: true })
  passRate: number;

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

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

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

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

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

  @ManyToOne(() => Checklist, (c) => c.inspections)
  @JoinColumn({ name: 'checklist_id' })
  checklist: Checklist;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'inspector_id' })
  inspector: User;

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'approved_by' })
  approvedBy: User;

  @OneToMany(() => InspectionResult, (r) => r.inspection)
  results: InspectionResult[];

  @OneToMany(() => NonConformity, (nc) => nc.inspection)
  nonConformities: NonConformity[];
}
