// ═══════════════════════════════════════════════════════════════════════
// Previous NC — TypeScript types
// ═══════════════════════════════════════════════════════════════════════

export type NcSource = 'QRS' | 'TQS' |'NEW'; 
export type NcStatus = 'open' | 'closed';
export type NcEntryStatus = 'open' | 'closed' | 'pending';

// ─── Row returned by /paged ────────────────────────────────────────────
export interface PreviousNcRow {
  id: number;
  source: NcSource;
  company_name: string | null;
  contact_primary: string | null;
  audit_type: string;
  nc_type: string;
  status: NcStatus;
  created_by_name: string | null;
  assigned_to_name: string | null;
  uploaded_doc: string | null;
  old_nc_document: string | null;
  created_at: string;
  entries_count: number;
}

export interface PreviousNcPagedResponse {
  rows: PreviousNcRow[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

// ─── User dropdown option (for user filter) ────────────────────────────
export interface PreviousNcUserOption {
  name: string;
  count?: number;
}

// ─── Single-NC detail ─────────────────────────────────────────────────
export interface PreviousNcEntry {
  id: number;
  nc_id: number;
  nc_type: string | null;
  ncr_statement: string | null;
  criteria_clause: string | null;
  corrective_action: string | null;
  document_path: string | null;
  status: NcEntryStatus;
  created_at: string;
  updated_at: string;
}

export interface PreviousNcRemark {
  id: number;
  nc_id: number;
  user_id: number;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface PreviousNcFinalClosure {
  id: number;
  nc_id: number;
  ncrentry_id: number | null;
  final_closure_pdf: string | null;
  nc_type: string | null;
  critical_clause: string | null;
  submitted_documents_checkbox: number | null;
  submitted_documents: string | null;
  verification_by: string | null;
  result_accepted: number;
  result_not_accepted: number;
  remarks: string | null;
  auditor_name: string | null;
  auditor_signature: string | null;
  closure_date: string | null;
  created_at: string;
  updated_at: string;
}

export interface PreviousNcDetailNc {
  id: number;
  source: NcSource;
  client_id: number | null;
  serve_id: number | null;
  auditee_name: string | null;
  audit_type: string;
  nc_type: string;
  status: NcStatus;
  follow_up_date: string | null;
  followed_up_by: number | null;
  final_closure_pdf: string | null;
  follow_up_notes: string | null;
  closed_by: number | null;
  closed_at: string | null;
  due_date: string | null;
  remark: string | null;
  created_at: string;
  updated_at: string;
  company_name: string | null;
  audit_date: string | null;
  designation: string | null;
  standard_names: string[];
  created_by_name: string | null;
  assigned_to_name: string | null;
}

export interface PreviousNcDetailResponse {
  nc: PreviousNcDetailNc;
  entries: PreviousNcEntry[];
  remarks: PreviousNcRemark[];
  final_closures: PreviousNcFinalClosure[];
}

// ─── Filters DTO (frontend → backend) ───────────────────────────────────
export interface ListPreviousNcsParams {
  page?: number;
  limit?: number;
  source?: 'All' | NcSource;
  status?: 'All' | NcStatus;
  nc_type?: string;
  audit_type?: string;
  search?: string;
  date_from?: string;
  date_to?: string;
  /** Filter NCs to those where this user is followed_up_by OR closed_by. */
  user_name?: string;
}

// ─── Update payload (PATCH) ────────────────────────────────────────────
export interface UpdatePreviousNcDto {
  nc?: {
    auditee_name?: string | null;
    audit_type?: string | null;
    nc_type?: string | null;
    status?: NcStatus;
    follow_up_date?: string | null;
    follow_up_notes?: string | null;
    remark?: string | null;
    due_date?: string | null;
  };
  entries?: Array<{
    id: number;
    nc_type?: string;
    ncr_statement?: string;
    criteria_clause?: string;
    corrective_action?: string;
    status?: NcEntryStatus;
  }>;
  new_remark?: string;
  new_findings?: Array<{
    ncr_statement: string;
    nc_type?: string;
    criteria_clause?: string;
    corrective_action?: string;
    status?: NcEntryStatus;
  }>;
}

export interface UpdatePreviousNcResponse {
  ok: true;
  nc_id: number;
  source: NcSource;
  updated_fields: string[];
  updated_entries: number;
  new_remark_id: number | null;
   new_findings_created: number;
}

// ═══════════════════════════════════════════════════════════════════════
// 🆕 PHASE 2A — Closure types
// ═══════════════════════════════════════════════════════════════════════

export interface ClosureRow {
  /** Present only on existing rows from server */
  id?: number;
  /** FK to ncr_entries.id (the finding being closed) */
  entry_id: number;
  evidence_received: string;
  submitted_docs: string;
  result_accepted: boolean;
  remarks: string;
  status?: 'open' | 'closed';
}

export interface ClosureResponse {
  nc_id: number;
  source: NcSource;
  is_finalized: boolean;
  finalized_at: string | null;
  signed_copy_path: string | null;
  signature_path: string | null;
  verification_by_auditor: string;
  auditor_name: string;
  closure_date: string | null;
  send_to_client: boolean;
  rows: ClosureRow[];
}

export interface SaveClosurePayload {
  closed_entry_ids: number[];
  rows: ClosureRow[];
  verification_by_auditor: string;
  auditor_name: string;
  closure_date: string;
  send_to_client: boolean;
  finalize: boolean;
  signed_copy?: File;
  signature?: File;
}
// ═══════════════════════════════════════════════════════════════
// 🆕 AUDIT → NC STATUS report
// ═══════════════════════════════════════════════════════════════
export interface AuditNcStatusRow {
  source: 'QRS' | 'TQS';
  audit_kind: 'client' | 'surveillance' | 'recertification';
  audit_id: number;
  company_name: string | null;
  audit_date: string | null;
  auditor_names: string[];
  nc_count: number;
  nc_status: 'NC Raised' | 'NC Pending';
}

export interface AuditNcStatusResponse {
  rows: AuditNcStatusRow[];
  totals: { total: number; raised: number; pending: number };
}
export interface AuditNcStatusResponse {
  rows: AuditNcStatusRow[];
  totals: { total: number; raised: number; pending: number };
  can_view_all: boolean;
}
export interface AuditNcStatusParams {
  source?: 'All' | 'QRS' | 'TQS';
  status?: 'All' | 'Raised' | 'Pending';
  date_from?: string;
  date_to?: string;
  search?: string;
}
export interface SaveClosureResponse {
  ok: true;
  is_finalized: boolean;
  rows_saved: number;
  entries_closed: number;
  /** 🆕 PHASE 2B — present only when finalize=true */
  merged_pdf_sha256?: string;
  /** 🆕 PHASE 2B — present only when finalize=true */
  merged_pdf_pages?: number;
}