import { clearAuthCache } from "@/lib/auth-cache";
import { request } from "./core";

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

export interface LoginResponse {
  user: {
    id: number;
    username: string;
    email: string;
    role: string;
    list_page_size: number;
  };
}

export interface User {
  id: number;
  username: string;
  email: string;
  role: string;
  source?: string;
  active: boolean;
  tenant_id?: number;
}

export interface MeResponse {
  username: string;
  role: string;
  email: string;
  list_page_size: number;
}

export interface CreateUserRequest {
  username: string;
  email: string;
  password: string;
  role: string;
}

export interface UpdateUserRequest {
  email?: string;
  role?: string;
  active?: boolean;
  password?: string;
}

// ── Auth functions ────────────────────────────────────────────────────────────

export async function login(
  username: string,
  password: string
): Promise<LoginResponse> {
  return request<LoginResponse>("/api/auth/login", {
    method: "POST",
    body: JSON.stringify({ username, password }),
  });
}

export async function getMe(): Promise<MeResponse> {
  return request<MeResponse>("/api/auth/me");
}

export async function logout(): Promise<void> {
  clearAuthCache();
  await request<void>("/api/auth/logout", { method: "POST" });
}

export async function changePassword(
  currentPassword: string,
  newPassword: string
): Promise<{ ok: boolean }> {
  return request<{ ok: boolean }>("/api/auth/password", {
    method: "PATCH",
    body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }),
  });
}

export async function changeEmail(
  email: string
): Promise<{ ok: boolean; email: string }> {
  return request<{ ok: boolean; email: string }>("/api/auth/email", {
    method: "PATCH",
    body: JSON.stringify({ email }),
  });
}

export async function updatePreferences(
  listPageSize: number
): Promise<{ ok: boolean; list_page_size: number }> {
  return request<{ ok: boolean; list_page_size: number }>("/api/auth/preferences", {
    method: "PATCH",
    body: JSON.stringify({ list_page_size: listPageSize }),
  });
}

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

export async function getUsers(): Promise<User[]> {
  return request<User[]>("/api/users");
}

export async function createUser(data: CreateUserRequest): Promise<User> {
  return request<User>("/api/users", {
    method: "POST",
    body: JSON.stringify(data),
  });
}

export async function updateUser(id: number, data: UpdateUserRequest): Promise<User> {
  return request<User>(`/api/users/${id}`, {
    method: "PATCH",
    body: JSON.stringify(data),
  });
}

export async function deleteUser(id: number): Promise<void> {
  await request<void>(`/api/users/${id}`, { method: "DELETE" });
}

// ── TOTP / 2FA ────────────────────────────────────────────────────────────────

export async function getTOTPSetup(): Promise<{ secret: string; otpauth_url: string; qr_code: string }> {
  return request<{ secret: string; otpauth_url: string; qr_code: string }>("/api/auth/totp/setup");
}

export async function confirmTOTPSetup(code: string): Promise<{ ok: boolean }> {
  return request<{ ok: boolean }>("/api/auth/totp/setup", {
    method: "POST",
    body: JSON.stringify({ code }),
  });
}

export async function disableTOTP(code: string): Promise<{ ok: boolean }> {
  return request<{ ok: boolean }>("/api/auth/totp", {
    method: "DELETE",
    body: JSON.stringify({ code }),
  });
}
