"use client";

import { useState, useCallback } from "react";
import { getAuditLog, type AuditEntry } from "@/lib/api";

const AUDIT_PAGE_SIZE = 25;

export function useAdminAudit() {
  const [auditEntries, setAuditEntries] = useState<AuditEntry[]>([]);
  const [auditTotal, setAuditTotal] = useState(0);
  const [auditPage, setAuditPage] = useState(1);
  const [auditLoading, setAuditLoading] = useState(false);

  const loadAudit = useCallback(async (p: number) => {
    setAuditLoading(true);
    try {
      const data = await getAuditLog({ page: p, page_size: AUDIT_PAGE_SIZE });
      setAuditEntries(data.entries || []);
      setAuditTotal(data.total);
      setAuditPage(p);
    } catch {
      setAuditEntries([]);
    } finally {
      setAuditLoading(false);
    }
  }, []);

  return {
    auditEntries,
    auditTotal,
    auditPage,
    auditLoading,
    loadAudit,
  };
}
