import { apiClient } from "@shared/api/client";

export interface AdminUser {
  id: string;
  email: string;
  role: string;
  is_superuser: boolean;
  status: string;
}

export interface AdminUsersResponse {
  data: AdminUser[];
  meta: { total: number; page: number; limit: number };
}

export interface AdminStats {
  users_count: number;
  registrations_day: number;
}

export interface AdminSummary extends AdminStats {
  admins_count: number;
  superusers_count: number;
}

export interface AdminSettingsPayload {
  values: Record<string, unknown>;
  locks: Record<string, boolean>;
  settings_path: string;
  secrets: Record<string, boolean>;
}

export interface DiagnosticsCheck {
  id: string;
  status: "ok" | "warn" | "fail";
  message: string;
}

export interface InstallSecretsPayload {
  initialized: boolean;
  locked: boolean;
  secrets_path: string;
  database: {
    host: string | null;
    port: number | null;
    database: string;
    user: string | null;
    password_configured: boolean;
  };
  connection_string_masked: string;
  secrets_status: Record<string, string>;
}

export async function getAdminUsers(page = 1, limit = 20): Promise<AdminUsersResponse> {
  const { data } = await apiClient.get<AdminUsersResponse>("/api/v1/admin/users", {
    params: { page, limit }
  });
  return data;
}

export async function patchAdminUser(
  userId: string,
  payload: { role?: string; status?: string; is_superuser?: boolean }
): Promise<AdminUser> {
  const { data } = await apiClient.patch<AdminUser>(`/api/v1/admin/users/${userId}`, payload);
  return data;
}

export async function getAdminStats(): Promise<AdminStats> {
  const { data } = await apiClient.get<AdminStats>("/api/v1/admin/stats");
  return data;
}

export async function getAdminSummary(): Promise<AdminSummary> {
  const { data } = await apiClient.get<AdminSummary>("/api/v1/admin/summary");
  return data;
}

export async function createAdminUser(payload: {
  email: string;
  password: string;
  role: string;
  is_superuser: boolean;
  status: string;
}): Promise<AdminUser> {
  const { data } = await apiClient.post<AdminUser>("/api/v1/admin/users", payload);
  return data;
}

export async function resetAdminUserPassword(userId: string, password: string): Promise<{ status: string }> {
  const { data } = await apiClient.patch<{ status: string }>(
    `/api/v1/admin/users/${userId}/password`,
    { password }
  );
  return data;
}

export async function deleteAdminUser(userId: string): Promise<{ status: string }> {
  const { data } = await apiClient.delete<{ status: string }>(`/api/v1/admin/users/${userId}`);
  return data;
}

export async function getAdminSettings(): Promise<AdminSettingsPayload> {
  const { data } = await apiClient.get<AdminSettingsPayload>("/api/v1/admin/settings");
  return data;
}

export async function patchAdminSettings(values: Record<string, unknown>): Promise<AdminSettingsPayload> {
  const { data } = await apiClient.patch<AdminSettingsPayload>("/api/v1/admin/settings", { values });
  return data;
}

export async function getAdminDiagnostics(): Promise<{ checks: DiagnosticsCheck[] }> {
  const { data } = await apiClient.get<{ checks: DiagnosticsCheck[] }>("/api/v1/admin/diagnostics/report");
  return data;
}

export async function getAdminActivityFeed(limit = 200): Promise<{ events: Array<Record<string, unknown>> }> {
  const { data } = await apiClient.get<{ events: Array<Record<string, unknown>> }>(
    "/api/v1/admin/activity-feed",
    { params: { limit } }
  );
  return data;
}

export async function postAdminUiActivity(event: string, meta?: Record<string, unknown>): Promise<{ status: string }> {
  const { data } = await apiClient.post<{ status: string }>("/api/v1/admin/ui-activity", { event, meta });
  return data;
}

export async function getAdminServerLog(lines = 200): Promise<{ lines: string[] }> {
  const { data } = await apiClient.get<{ lines: string[] }>("/api/v1/admin/server-log", {
    params: { lines }
  });
  return data;
}

export async function getInstallSecrets(): Promise<InstallSecretsPayload> {
  const { data } = await apiClient.get<InstallSecretsPayload>("/api/v1/admin/secrets");
  return data;
}

export async function revealInstallSecret(
  key: "database_password" | "jwt_access_secret" | "jwt_refresh_pepper" | "s3_secret_key"
): Promise<{ key: string; value: string }> {
  const { data } = await apiClient.post<{ key: string; value: string }>("/api/v1/admin/secrets/reveal", { key });
  return data;
}
