import client from '../client';

/* ── Types ── */

export interface AuditEntry {
  id: string;
  user_id: string | null;
  username: string | null;
  action: string;
  resource: string | null;
  details: string | null;
  ip: string | null;
  created_at: string;
}

export interface ConfigLogEntry {
  id: string;
  config_type: string;
  config_id: string;
  config_name: string | null;
  severity: string;
  action: string;
  message: string;
  user_id: string | null;
  username: string | null;
  created_at: string;
}

export interface ChatLogEntry {
  id: string;
  session_id: string | null;
  message_id: string | null;
  user_id: string | null;
  severity: string;
  source: string;
  action: string;
  message: string;
  created_at: string;
}

/* ── API calls ── */

export const adminApi = {
  getAuditLog: (limit = 100) =>
    client.get(`/audit-log?limit=${limit}`) as unknown as Promise<AuditEntry[]>,

  getConfigLogs: (params: {
    config_type?: string;
    config_id?: string;
    severity?: string;
    limit?: number;
  } = {}) => {
    const qs = new URLSearchParams();
    if (params.config_type) qs.set('config_type', params.config_type);
    if (params.config_id) qs.set('config_id', params.config_id);
    if (params.severity) qs.set('severity', params.severity);
    qs.set('limit', String(params.limit ?? 50));
    return client.get(`/config-logs?${qs}`) as unknown as Promise<ConfigLogEntry[]>;
  },

  getChatLogs: (params: {
    severity?: string;
    session_id?: string;
    limit?: number;
  } = {}) => {
    const qs = new URLSearchParams();
    if (params.severity) qs.set('severity', params.severity);
    if (params.session_id) qs.set('session_id', params.session_id);
    qs.set('limit', String(params.limit ?? 50));
    return client.get(`/chat-logs?${qs}`) as unknown as Promise<ChatLogEntry[]>;
  },
};
