// ─── Enums ─────────────────────────────────────────────────────────────────
export type ScheduleStatus =
  | "DRAFT"
  | "PUBLISHED"
  | "IN_PROGRESS"
  | "COMPLETED"
  | "CANCELLED";

export type RowStatus =
  | "PENDING"
  | "CONFIRMED"
  | "IN_PROGRESS"
  | "COMPLETED"
  | "CANCELLED"
  | "RESCHEDULED";

export type AuditType =
  | "INITIAL"
  | "SURVEILLANCE"
  | "RECERTIFICATION"
  | "TRANSFER"
  | "FOLLOW_UP";

export type AuditMode = "ONSITE" | "OFFICE" | "ONLINE" | "REMOTE" | "HYBRID";

export type CancellationReason =
  | "CLIENT_REQUEST"
  | "INTERNAL"
  | "EMERGENCY"
  | "OTHER";

// ─── Related entities (slim) ───────────────────────────────────────────────
export interface MiniUser {
  id: number;
  firstName: string;
  lastName: string;
  email: string;
}

export interface MiniCompany {
  id: number;
  name: string;
  company_code?: string;
  address?: string;
  city?: string;
}

export interface MiniStandard {
  id: number;
  name: string;
  title?: string;
}

// ─── Row entity ────────────────────────────────────────────────────────────
export interface AuditRow {
  id: number;
  schedule_id: number;
  row_no: number;
  audit_code: string;

  audit_type: AuditType;
  audit_stage?: string | null;
  audit_mode: AuditMode;
  accreditation?: string | null;

  company_id: number;
  company?: MiniCompany;

  lead_auditor_id: number;
  co_auditor_ids?: number[];   // 🆕 used when saving/editing

  lead_auditor?: MiniUser;
  co_auditors?: MiniUser[];    // 🆕 populated relation, used for display (names/chips)

  standards?: MiniStandard[];

  audit_time?: string | null;
  audit_time_label?: string | null;

  status: RowStatus;

  cancelled_at?: string | null;
  cancelled_by_id?: number | null;
  cancelled_by?: MiniUser | null;
  cancellation_reason?: string | null;
  cancellation_notes?: string | null;

  original_audit_date?: string | null;
  reschedule_count: number;
  reschedule_reason?: string | null;
  last_rescheduled_at?: string | null;

  notes?: string | null;

  created_at: string;
  updated_at: string;
}

// ─── Schedule entity ───────────────────────────────────────────────────────
export interface AuditSchedule {
  id: number;
  schedule_date: string; // YYYY-MM-DD
  title: string;
  client_group: string;

  coordinator_id: number;
  coordinator?: MiniUser;

  source_email?: string | null;
  status: ScheduleStatus;
  notes?: string | null;

  created_by_id: number;
  created_by?: MiniUser;

  created_at: string;
  updated_at: string;

  // optional aggregates
  rows?: AuditRow[];
  row_count?: number;
}

// ─── DTOs ──────────────────────────────────────────────────────────────────
export interface CreateAuditRowDto {
  audit_type: AuditType;
  audit_stage?: string;
  audit_mode: AuditMode;
  accreditation?: string;
  company_id: number;
  lead_auditor_id: number;
  co_auditor_ids?: number[];   // 🆕 Additional auditors (Auditor 2, 3, ...)
  standard_ids: number[];
  audit_time?: string;
  audit_time_label?: string;
  notes?: string;
}

export interface CreateAuditScheduleDto {
  schedule_date: string;
  title: string;
  client_group: string;
  coordinator_id: number;
  source_email?: string;
  notes?: string;
  rows: CreateAuditRowDto[];
}

export interface UpdateAuditScheduleDto {
  schedule_date?: string;
  title?: string;
  client_group?: string;
  coordinator_id?: number;
  source_email?: string;
  notes?: string;
  status?: ScheduleStatus;
}

export interface UpdateAuditRowDto {
  audit_type?: AuditType;
  audit_stage?: string;
  audit_mode?: AuditMode;
  accreditation?: string;
  company_id?: number;
  lead_auditor_id?: number;
  co_auditor_ids?: number[]; // 🆕 send [] to clear
  standard_ids?: number[];
  audit_time?: string;
  audit_time_label?: string;
  notes?: string;
  status?: RowStatus;
}

export interface CancelRowDto {
  cancellation_reason: CancellationReason;
  cancellation_notes?: string;
}

export interface RescheduleRowDto {
  new_audit_date: string;
  new_audit_time?: string;
  new_audit_time_label?: string;
  reschedule_reason: string;
  new_lead_auditor_id?: number;   // 🆕
  new_co_auditor_ids?: number[];  // 🆕
}

export interface BulkCancelDto {
  cancellation_reason: CancellationReason;
  cancellation_notes?: string;
}

// ─── Query params ──────────────────────────────────────────────────────────
export interface AuditScheduleQueryParams {
  page?: number;
  limit?: number;
  status?: ScheduleStatus;
  date_from?: string;
  date_to?: string;
  client_group?: string;
  search?: string;
}

// ─── Responses ─────────────────────────────────────────────────────────────
// Matches the flat shape your existing PaginatedCertificatesResponse uses.
// If your audit-schedules backend instead returns { data, meta: {...} },
// the API layer in audit-schedule.api.ts normalizes both shapes.
export interface PaginatedAuditSchedulesResponse {
  data: AuditSchedule[];
  total: number;
  page: number;
  lastPage: number;
  limit?: number;
}

export interface AuditAnalytics {
  total_audits: number;
  completed: number;
  pending: number;
  cancelled: number;
  in_progress: number;
  this_week: number;
  completion_rate: number;
}

export interface AuditTrailEntry {
  id: number;
  audit_row_id: number;
  old_status: RowStatus | null;
  new_status: RowStatus;
  reason: string;
  changed_by_id: number;
  changed_by?: MiniUser;
  created_at: string;
}

// ─── Table row shape (flat, like CertificateRow) ───────────────────────────
export interface AuditScheduleRow {
  sno: number;
  id: number;
  schedule_date: string;
  title: string;
  client_group: string;
  status: ScheduleStatus;
  coordinator_name: string;
  coordinator_email: string;
  row_count: number;
  source_email: string;
  notes: string;
  created_at: string;
  rows: AuditRow[]; // nested rows for expanded view
  [key: string]: any;
}