import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  OneToMany,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Unique,
} from 'typeorm';
import { AuditScheduleRow } from '../../audit-schedules/entities/audit-schedule-row.entity';
import { Company } from '../../companies/entities/company.entity';
import { Standard } from '../../standards/entities/standard.entity';
import { User } from '../../user/entities/user.entity';
import { AuditChecklistItem } from './audit-checklist-item.entity';

export enum AuditChecklistStatus {
  DRAFT = 'draft',
  SUBMITTED = 'submitted',
}

@Entity({ name: 'audit_checklists' })
@Unique('uq_audit_checklist_standard', ['audit_schedule_row_id', 'standard_id'])
export class AuditChecklist {
  @PrimaryGeneratedColumn()
  id: number;

  @Index()
  @Column({ type: 'int' })
  audit_schedule_row_id: number;

  @ManyToOne(() => AuditScheduleRow, { nullable: false })
  @JoinColumn({ name: 'audit_schedule_row_id' })
  audit_schedule_row: AuditScheduleRow;

  // Denormalized on purpose - same choice already made by nc__ncs, kept
  // consistent here. Lets client-portal scope queries with a flat
  // WHERE company_id = X instead of joining through the audit every time.
  @Index()
  @Column({ type: 'int' })
  company_id: number;

  @ManyToOne(() => Company, { nullable: false })
  @JoinColumn({ name: 'company_id' })
  company: Company;

  @Column({ type: 'int' })
  standard_id: number;

  @ManyToOne(() => Standard, { nullable: false })
  @JoinColumn({ name: 'standard_id' })
  standard: Standard;

  @Column({ type: 'enum', enum: AuditChecklistStatus, default: AuditChecklistStatus.DRAFT })
  status: AuditChecklistStatus;

  @Column({ type: 'int', nullable: true })
  submitted_by: number | null;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'submitted_by' })
  submitted_by_user: User | null;

  // Auditor generated + reviewed the PDF - client doesn't know yet.
  @Column({ type: 'timestamp', nullable: true })
  submitted_at: Date | null;

  // Separate moment: auditor clicked "Notify Client."
  @Column({ type: 'timestamp', nullable: true })
  client_notified_at: Date | null;

  // Client clicked their own Submit after uploading.
  @Column({ type: 'timestamp', nullable: true })
  client_submitted_at: Date | null;

  // Every item approved by the auditor.
  @Column({ type: 'timestamp', nullable: true })
  completed_at: Date | null;

  // JSON array of the emails the auditor confirmed at notify time -
  // review-result emails go to the same addresses.
  @Column({ type: 'text', nullable: true })
  notify_emails: string | null;

  // Optional message the auditor wrote for the client (shown on the
  // portal and in the email).
  @Column({ type: 'text', nullable: true })
  client_message: string | null;

  // Optional due date for the client's uploads (YYYY-MM-DD).
  @Column({ type: 'date', nullable: true })
  due_date: string | null;

  @OneToMany(() => AuditChecklistItem, (item) => item.audit_checklist)
  items: AuditChecklistItem[];

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}
