/**
 * Admin API client.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

const API_BASE = '/api/admin';

async function request<T>(path: string, options?: RequestInit): Promise<T> {
  const res = await fetch(`${API_BASE}${path}`, {
    credentials: 'include',
    headers: { 'Content-Type': 'application/json', ...options?.headers },
    ...options,
  });
  if (!res.ok) {
    const body = await res.json().catch(() => ({}));
    throw new Error(body.detail || `Request failed: ${res.status}`);
  }
  if (res.status === 204) return undefined as unknown as T;
  return res.json();
}

// ── Types ───────────────────────────────────────────────────────

export interface AdminUser {
  id: number;
  {{ username_field }}: string;
  username?: string | null;
  is_active: boolean;
  roles: string[];
{% if email_verification %}
  email_verified: boolean;
{% endif %}
}

export interface AdminUserListResponse {
  users: AdminUser[];
  total: number;
  skip: number;
  limit: number;
}

export interface WhitelistRule {
  id: number;
  rule_type: string;
  value: string;
  created_by?: string | null;
  created_at: string;
}

// ── User management ─────────────────────────────────────────────

export const adminApi = {
  listUsers: (skip = 0, limit = 50) =>
    request<AdminUserListResponse>(`/users?skip=${skip}&limit=${limit}`),

  getUser: (id: number) => request<AdminUser>(`/users/${id}`),

  updateUser: (id: number, data: { is_active?: boolean; roles?: string[] }) =>
    request<AdminUser>(`/users/${id}`, {
      method: 'PATCH',
      body: JSON.stringify(data),
    }),

  deleteUser: (id: number) =>
    request<void>(`/users/${id}`, { method: 'DELETE' }),

  promoteUser: (id: number) =>
    request<AdminUser>(`/users/${id}/promote`, { method: 'POST' }),

  demoteUser: (id: number) =>
    request<AdminUser>(`/users/${id}/demote`, { method: 'POST' }),

  inviteUser: (email: string) =>
    request<{ message: string }>('/invite', {
      method: 'POST',
      body: JSON.stringify({ email }),
    }),

  // ── Whitelist ───────────────────────────────────────────────────

  listWhitelist: () => request<WhitelistRule[]>('/whitelist'),

  addWhitelistRule: (rule_type: string, value: string) =>
    request<WhitelistRule>('/whitelist', {
      method: 'POST',
      body: JSON.stringify({ rule_type, value }),
    }),

  deleteWhitelistRule: (id: number) =>
    request<void>(`/whitelist/${id}`, { method: 'DELETE' }),

  // ── Roles ────────────────────────────────────────────────────

  listRoles: () => request<{ name: string; permissions: string[]; description?: string; userCount?: number }[]>('/roles'),

  // ── Permissions ─────────────────────────────────────────────

  listPermissions: () => request<{ key: string; description?: string; roles: string[] }[]>('/permissions'),

  // ── Activity Log ────────────────────────────────────────────

  getActivityLog: (params?: { page?: number; pageSize?: number }) => {
    const searchParams = new URLSearchParams();
    if (params?.page) searchParams.set('page', String(params.page));
    if (params?.pageSize) searchParams.set('page_size', String(params.pageSize));
    const qs = searchParams.toString();
    return request<{ id: number; action: string; user_email: string; target_type?: string; target_id?: string; timestamp: string; details?: string }[]>(
      `/activity-log${qs ? `?${qs}` : ''}`
    );
  },

  // ── Bootstrap ─────────────────────────────────────────────────

  bootstrap: (token: string, password: string) =>
    request<{ message: string; user: AdminUser }>('/bootstrap', {
      method: 'POST',
      body: JSON.stringify({ token, password }),
    }),
};
