import { API_BASE, headers, logAPI, parse, } from "../api"; import type { ApiOk } from "../api"; import type { UserProfile } from "./perfis"; // // User Management APIs // export type UserRole = { id: string; user_id: string; role: 'admin' | 'medico' | 'paciente'; created_at: string; }; export type CreateUserInput = { email: string; password?: string; full_name: string; phone?: string; role: 'admin' | 'medico' | 'paciente'; }; export type CreatedUser = { id: string; email: string; full_name: string; phone?: string; role: string; }; export type CompleteUserInfo = { user: { id: string; email: string; email_confirmed_at: string; created_at: string; last_sign_in_at: string; }; profile: UserProfile; roles: string[]; permissions: { isAdmin: boolean; isManager: boolean; isDoctor: boolean; isSecretary: boolean; isAdminOrManager: boolean; }; }; export async function criarUsuario(input: CreateUserInput): Promise<{ success: boolean; user: CreatedUser }> { const url = `${API_BASE}/functions/v1/create-user`; const res = await fetch(url, { method: "POST", headers: headers("json"), body: JSON.stringify(input) }); const data = await parse(res); logAPI("criarUsuario", { url, payload: input, result: data }); return data; } export async function listarUserRoles(): Promise { const url = `${API_BASE}/rest/v1/user_roles`; const res = await fetch(url, { method: "GET", headers: headers("json") }); const data = await parse(res); logAPI("listarUserRoles", { url, result: data }); return data ?? []; } export async function getCompleteUserInfo(userId: string): Promise { const url = `${API_BASE}/functions/v1/user-info`; // Assuming the function takes the user ID in the body of a POST request const res = await fetch(url, { method: "POST", headers: headers("json"), body: JSON.stringify({ id: userId }) }); const data = await parse(res); logAPI("getCompleteUserInfo", { url, payload: { id: userId }, result: data }); return data; }