// ─────────────────────────────────────────────────────────────────────────────
// My Audits types — matches the backend AuditScheduleRow + workspace shape.
// ─────────────────────────────────────────────────────────────────────────────

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

export type AuditType = 'INITIAL' | 'SURVEILLANCE' | 'RECERTIFICATION';

export type AuditMode = 'ONSITE' | 'OFFICE';

export type MyAuditsDatePreset =
  | 'all'
  | 'today'
  | 'this_week'
  | 'this_month'
  | 'past';

// A user reference returned inside the row payload
export interface UserRef {
  id: number;
  firstName?: string;
  lastName?: string;
  email?: string;
}

// A standard reference returned inside the row payload
export interface StandardRef {
  id: number;
  name: string;
}

// A company reference returned inside the row payload
export interface CompanyRef {
  id: number;
  name: string;
  company_code?: string;
  contact_person?: string;
  mobile?: string;
  email?: string;
  city?: string;
  country_id?: number;
}

// The nested schedule on each row
export interface ScheduleRef {
  id: number;
  schedule_date: string;
  title?: string;
  client_group?: string;
  coordinator_id?: number;
  coordinator?: UserRef;
}

// The row shape returned by GET /my-audits
export interface MyAuditRow {
  id: number;
  audit_code: string;
  row_no: number;
  audit_type: AuditType;
  audit_stage: string | null;
  audit_mode: AuditMode;
  accreditation: string | null;
  status: RowStatus;
  audit_time: string | null;
  audit_time_label: string | null;
  notes: string | null;
  company_id: number;
  company?: CompanyRef;
  lead_auditor_id: number;
  lead_auditor?: UserRef;
  co_auditors?: UserRef[];        // 🆕 populated relation, for display
  reassigned_from_you?: boolean;  // 🆕 true if this used to be yours, now reassigned via reschedule
  schedule_id: number;
  schedule?: ScheduleRef;
  standards?: StandardRef[];
  audit_request_id?: number | null;
  created_at: string;
  updated_at: string;
}

// GET /my-audits paginated wrapper
export interface MyAuditsListResponse {
  data: MyAuditRow[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
}

// GET /my-audits/analytics
export interface MyAuditsAnalytics {
  today: number;
  this_week: number;
  in_progress: number;
  completed_this_month: number;
}

// GET /audits/:rowId/workspace
export interface AuditWorkspaceResponse {
  row: MyAuditRow;
  audit_request: AuditRequestSnapshot | null;
  history: AuditHistoryEntry[];
}

export interface AuditRequestSnapshot {
  id: number;
  auditee_name: string;
  auditee_contact: string;
  auditee_email: string;
  proposed_date: string;
  proposed_time: string;
  location: string;
  mode: 'ONLINE' | 'ONSITE' | 'HYBRID';
  certification_type: string;
  accreditation: string;
  marketing_remarks: string | null;
  coordinator_remarks: string | null;
  standard_ids: number[];
  status: string;
  requested_by_id: number;
  created_at: string;
  updated_at: string;
    documents?: { filename: string; path: string; mimetype: string; size: number }[] | null;

}

export interface AuditHistoryEntry {
  id: number;
  row_id: number;
  old_status: string | null;
  new_status: string;
  changed_by_id: number | null;
  changed_by?: UserRef;
  reason: string | null;
  notes: string | null;
  created_at: string;
}

// Filter state used by the page
export interface MyAuditsFilters {
  search: string;
  status: RowStatus | 'all';
  datePreset: MyAuditsDatePreset;
  dateFrom: string;
  dateTo: string;
  auditType: AuditType | 'all';
  standardId: string;
}
