/**
 * NonConformity Entity
 * No conformidades detectadas en inspecciones
 *
 * @module Quality
 * @table quality.non_conformities
 */

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 { Inspection } from './inspection.entity';
import { CorrectiveAction } from './corrective-action.entity';

export type NCSeverity = 'minor' | 'major' | 'critical';
export type NCStatus = 'open' | 'in_progress' | 'closed' | 'verified';

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

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

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

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

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

  @Column({ name: 'detection_date', type: 'date' })
  detectionDate: Date;

  @Column({ type: 'varchar', length: 100 })
  category: string;

  @Column({ type: 'varchar', length: 20 })
  severity: NCSeverity;

  @Column({ type: 'text' })
  description: string;

  @Column({ name: 'root_cause', type: 'text', nullable: true })
  rootCause: string;

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

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

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

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

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

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

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

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

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

  @Column({ name: 'closure_notes', type: 'text', nullable: true })
  closureNotes: 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(() => Inspection, (i) => i.nonConformities)
  @JoinColumn({ name: 'inspection_id' })
  inspection: Inspection;

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'closed_by' })
  closedBy: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'verified_by' })
  verifiedBy: User;

  @OneToMany(() => CorrectiveAction, (ca) => ca.nonConformity)
  correctiveActions: CorrectiveAction[];
}
