import {
  Entity, PrimaryGeneratedColumn, Column, OneToMany, CreateDateColumn, UpdateDateColumn, Index, Unique,
} from 'typeorm';
import { AuditReportClause } from './audit-report-clause.entity';
import { AuditReportSection } from './audit-report-section.entity';
import { AuditReportAttendance } from './audit-report-attendance.entity';

export type StageType = 'stage1' | 'stage2' | 'surveillance' | 'recert';
export type ReportStatus = 'draft' | 'submitted' | 'returned' | 'approved' | 'locked';
export type Conclusion = 'complies' | 'minor_nc' | 'major_nc' | 'not_recommended';
export type Stage1Recommendation = 'proceed' | 'hold' | 'repeat_stage1';

@Entity({ name: 'audit_reports' })
@Unique('uq_audit_report_row_std', ['audit_schedule_row_id', 'standard_id'])
export class AuditReport {
  @PrimaryGeneratedColumn() id: number;

  @Index() @Column({ type: 'int' }) audit_schedule_row_id: number;
  @Index() @Column({ type: 'int' }) company_id: number;
  @Column({ type: 'int' }) standard_id: number;
  @Column({ type: 'int', nullable: true }) template_id: number | null;

  @Column({ type: 'varchar', length: 50, nullable: true, unique: true }) report_no: string | null;
  @Column({ type: 'enum', enum: ['stage1', 'stage2', 'surveillance', 'recert'] }) stage_type: StageType;
  @Index()
  @Column({ type: 'enum', enum: ['draft', 'submitted', 'returned', 'approved', 'locked'], default: 'draft' })
  status: ReportStatus;

  @Column({ type: 'enum', enum: ['complies', 'minor_nc', 'major_nc', 'not_recommended'], nullable: true })
  conclusion: Conclusion | null;
  @Column({ type: 'enum', enum: ['proceed', 'hold', 'repeat_stage1'], nullable: true })
  recommendation: Stage1Recommendation | null;
  @Column({ type: 'date', nullable: true }) next_audit_date: string | null;

  @Column({ type: 'json', nullable: true }) crm_json: Record<string, any> | null;
  @Column({ type: 'json', nullable: true }) edits_json: Record<string, any> | null;
  @Column({ type: 'json', nullable: true }) extras_json: Record<string, any> | null;
  @Column({ type: 'longtext', nullable: true }) snapshot_json: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true }) pdf_path: string | null;
  @Column({ type: 'int', default: 1 }) version: number;
  @Column({ type: 'text', nullable: true }) return_reason: string | null;

  @Column({ type: 'int', nullable: true }) created_by: number | null;
  @Column({ type: 'int', nullable: true }) submitted_by: number | null;
  @Column({ type: 'datetime', nullable: true }) submitted_at: Date | null;
  @Column({ type: 'int', nullable: true }) approved_by: number | null;
  @Column({ type: 'datetime', nullable: true }) approved_at: Date | null;
  @Column({ type: 'datetime', nullable: true }) locked_at: Date | null;

  @OneToMany(() => AuditReportClause, (c) => c.report) clauses: AuditReportClause[];
  @OneToMany(() => AuditReportSection, (s) => s.report) sections: AuditReportSection[];
  @OneToMany(() => AuditReportAttendance, (a) => a.report) attendance: AuditReportAttendance[];

  @CreateDateColumn() created_at: Date;
  @UpdateDateColumn() updated_at: Date;
}
