import axios from "axios";

export const http = axios.create({
  baseURL: "/api"
});

http.interceptors.request.use((config) => {
  const token = localStorage.getItem("admin_token");
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

export interface PublicCertificate {
  certificate_no: string;
  learner_name: string;
  certificate_name: string;
  project_code: string;
  course_name: string | null;
  stage_name: string | null;
  course_start_date: string | null;
  course_end_date: string | null;
  issue_date: string;
  template_code: string;
  status: string;
  pdf_status: string;
  public_token: string | null;
  public_url: string | null;
  download_url: string | null;
  can_download_pdf: boolean;
  disclaimer: string;
}

export interface Captcha {
  captcha_id: string;
  image: string;
}

export interface ProjectCourse {
  id: number;
  code: string;
  name: string;
  default_certificate_name: string;
  default_course_name: string | null;
  default_stage_name: string | null;
  default_template_code: string;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface Learner {
  id: number;
  phone: string;
  current_name: string;
  student_no: string | null;
  status: string;
  remark: string | null;
  certificate_count: number;
  created_at: string;
  updated_at: string;
}

export interface AdminCertificate {
  id: number;
  learner_id: number;
  learner_name: string | null;
  learner_phone: string | null;
  import_batch_id: number | null;
  project_code: string;
  certificate_no: string;
  certificate_name: string;
  class_name: string | null;
  course_name: string | null;
  stage_name: string | null;
  course_start_date: string | null;
  course_end_date: string | null;
  issue_date: string;
  template_code: string;
  status: string;
  pdf_status: string;
  created_at: string;
  updated_at: string;
}

export interface ImportBatch {
  id: number;
  filename: string;
  template_code: string;
  status: string;
  total_rows: number;
  valid_rows: number;
  failed_rows: number;
  conflict_rows: number;
  error_report_path: string | null;
  created_at: string;
  updated_at: string;
}

export interface CertificateTemplate {
  code: string;
  name: string;
  description: string;
  dynamic_fields: string[];
  fields: CertificateTemplateField[];
  preview_url: string;
  status: string;
}

export interface CertificateTemplateField {
  key: string;
  label: string;
  field_type: "text" | "date";
  required: boolean;
  example: string;
  description: string;
}

export interface LearnerImportResult {
  total_rows: number;
  created_rows: number;
  updated_rows: number;
  unchanged_rows: number;
  failed_rows: number;
  errors: Array<{ row_no: number; message: string }>;
}

export interface OperationLog {
  id: number;
  admin_user_id: number | null;
  action: string;
  action_label: string;
  object_type: string;
  object_label: string;
  object_id: string | null;
  ip_address: string | null;
  detail_json: string | null;
  risk: string;
  risk_label: string;
  summary: string;
  created_at: string;
}

export interface DatabaseBackup {
  filename: string;
  database_type: string;
  created_at: string;
  size_bytes: number;
  size_label: string;
  reason: string | null;
  note: string | null;
}

export interface RestoreDatabaseBackupResult {
  restored_backup: DatabaseBackup;
  pre_restore_backup: DatabaseBackup;
}

export interface PdfSettings {
  pdf_generation_concurrency_limit: number;
  updated_at: string | null;
}

export interface PdfPregenerationJob {
  id: string;
  status: string;
  total: number;
  completed: number;
  generated: number;
  cached: number;
  skipped: number;
  failed: number;
  percent: number;
  concurrency_limit: number;
  errors: string[];
  created_at: string;
  finished_at: string | null;
}
