/**
 * InspectionResult Entity
 * Resultados por item de inspección
 *
 * @module Quality
 * @table quality.inspection_results
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { Inspection } from './inspection.entity';
import { ChecklistItem } from './checklist-item.entity';

export type InspectionResultStatus = 'pending' | 'passed' | 'failed' | 'not_applicable';

@Entity({ schema: 'construction', name: 'inspeccion_resultados' })
@Index(['tenantId', 'inspectionId', 'checklistItemId'], { unique: true })
export class InspectionResult {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ name: 'checklist_item_id', type: 'uuid' })
  checklistItemId: string;

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

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

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

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

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

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

  @ManyToOne(() => Inspection, (i) => i.results, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'inspection_id' })
  inspection: Inspection;

  @ManyToOne(() => ChecklistItem)
  @JoinColumn({ name: 'checklist_item_id' })
  checklistItem: ChecklistItem;
}
