// ─────────────────────────────────────────────────────────────────────────────
// Audit Report Builder types — mirror backend audit-report-builder module.
// ─────────────────────────────────────────────────────────────────────────────

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';
export type FindingStatus = 'C' | 'NC' | 'OFI' | 'NA';
export type GroupKey = 'clause' | 'performance' | 'risk' | 'feedback' | 'statutory';

// GET /audit-reports  (list)
export interface ReportListItem {
  id: number;
  report_no: string | null;
  audit_schedule_row_id: number;
  company_id: number;
  company_name: string | null;
  standard_id: number;
  stage_type: StageType;
  status: ReportStatus;
  conclusion: Conclusion | null;
  version: number;
  has_pdf: number | boolean;
  created_by: number | null;
  submitted_at: string | null;
  approved_at: string | null;
  updated_at: string;
}

export interface ReportListResponse {
  rows: ReportListItem[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface ReportListParams {
  status?: string;
  stage_type?: string;
  standard_id?: number;
  company_id?: number;
  search?: string;
  page?: number;
  limit?: number;
}

// GET /audit-reports/by-audit/:rowId
export interface ReportByAuditItem {
  id: number;
  report_no: string | null;
  standard_id: number;
  stage_type: StageType;
  status: ReportStatus;
  version: number;
  has_pdf: boolean;
  approved_at: string | null;
}

// GET /audit-reports/context/:rowId
export interface ReportContext {
  audit: {
    id: number;
    audit_code: string;
    audit_type: string;
    audit_stage: string | null;
    status: string;
    company_id: number;
    company_name: string | null;
    scheme: string | null;
    schedule_date: string | null;
  };
  suggested_stage_type: StageType | null;
  can_combine?: boolean;
  standards: { id: number; name: string; title: string }[];
  reports: ReportByAuditItem[];
}

export interface ReportHead {
  id: number;
  report_no: string | null;
  audit_schedule_row_id: number;
  company_id: number;
  /** 0 = the combined report (covers every standard of the audit) */
  standard_id: number;
  is_combined?: boolean;
  standard_label?: string;
  /** combined report: every standard booked for the audit; extras.hidden_standards holds the ids left off the printed report */
  all_standards?: { id: number; name: string; title: string }[];
  stage_type: StageType;
  stage_label: string;
  status: ReportStatus;
  conclusion: Conclusion | null;
  recommendation: Stage1Recommendation | null;
  next_audit_date: string | null;
  version: number;
  return_reason: string | null;
  has_pdf: boolean;
  submitted_at: string | null;
  approved_at: string | null;
  crm_refreshed_at: string | null;
}

export interface ReportClause {
  id: number;
  report_id: number;
  group_key: GroupKey;
  code: string;
  heading: string;
  title: string;
  requirement: string | null;
  status: FindingStatus | null;
  evidence_text: string | null;
  /** original standard wording (from the clause library); shown when the row is C */
  default_evidence?: string | null;
  docs_reviewed: string[] | null;
  nc_id: number | null;
  ncr_entry_id: number | null;
  sort_order: number;
}

export interface ReportSection {
  key: string;
  label: string;
  body: string;
}

export interface ReportAttendee {
  id?: number;
  name: string;
  designation?: string | null;
  signature_path?: string | null;
}

export interface ReportClientDoc {
  id: number;
  file_name: string;
  item_text: string;
  category: string | null;
}

export interface ReportNc {
  entry_id: number;
  nc_id: number;
  nc_type: string;
  ncr_statement: string | null;
  criteria_clause: string | null;
  corrective_action: string | null;
  status: string | null;
}

export interface ReportExtras {
  declarations?: string[];
  attachments?: string[];
  mark_use?: string;
  mark_use_nc_no?: string;
  change_detail?: Record<string, any>;
  improvement?: 'modification' | 'none';
  /** Surveillance reports: which surveillance of the cycle (ticks "Surveillance 1" / "Surveillance 2" on forms that have them, e.g. ISO 14001) */
  surveillance_no?: 1 | 2;
  observations_text?: string;
  /** combined report: standards.id values the auditor removed from the printed report (cover, title, wording) */
  hidden_standards?: number[];
  /** clause rows (audit_report_clauses.id) the auditor switched OFF: not printed on the report. Default: every row enabled */
  disabled_clauses?: number[];
  [k: string]: any;
}

export interface KeyText {
  key: string;
  text: string;
  /** true = a sub-heading row in the list (no tick box), e.g. the ISO 14001 mandatory documents block */
  heading?: boolean;
}

export interface ReportOptions {
  conclusion: { key: Conclusion; text: string }[];
  stage1_recommendation: { key: Stage1Recommendation; text: string }[];
  declarations: KeyText[];
  attachments: KeyText[];
  mark_use: KeyText[];
  change_detail: { key: string; label: string; kind: 'yesno' | 'nonespecify' }[];
  editable_fields: string[];
}

// GET /audit-reports/:id  (also returned by POST /audit-reports)
export interface ReportEditorModel {
  report: ReportHead;
  fields: Record<string, string>;
  crm_fields: Record<string, string>;
  overridden: string[];
  extras: ReportExtras;
  sections: ReportSection[];
  clauses: ReportClause[];
  attendance: ReportAttendee[];
  client_documents: ReportClientDoc[];
  ncs: ReportNc[];
  options: ReportOptions;
  permissions: { can_edit: boolean; can_approve: boolean };
}

export interface CreateReportPayload {
  audit_schedule_row_id: number;
  /** one standard of the audit; leave out when creating the combined report */
  standard_id?: number;
  /** true = one report that covers all standards of the audit */
  combined?: boolean;
  stage_type?: StageType;
  /** combined report: booked standards NOT to show on the report (at least two must remain) */
  exclude_standard_ids?: number[];
}

export interface UpdateHeaderPayload {
  fields?: Record<string, string | null>;
  conclusion?: Conclusion | null;
  recommendation?: Stage1Recommendation | null;
  next_audit_date?: string | null;
  extras?: ReportExtras;
}

export interface ClauseSaveItem {
  id: number;
  status?: FindingStatus | null;
  evidence_text?: string | null;
  docs_reviewed?: string[];
}

export interface RaiseNcPayload {
  nc_type: 'Major' | 'Minor' | 'Observation';
  statement?: string;
  corrective_action?: string;
}