/** API client for the Authentik service (directory + messaging). */
import { get, post } from "./shared";

export interface AuthentikUser {
	pk: number;
	username: string;
	name: string;
	email: string;
	is_active: boolean;
	avatar: string | null;
	[key: string]: unknown;
}

export interface AuthentikUsersResponse {
	items: AuthentikUser[];
	total: number;
	page: number;
	page_size: number;
	error?: string;
}

export async function fetchAuthentikUsers(
	serviceId: string,
	params: { search?: string; page?: number; page_size?: number },
): Promise<AuthentikUsersResponse> {
	return get<AuthentikUsersResponse>(
		`/api/services/authentik/${serviceId}/users`,
		{
			search: params.search ?? "",
			page: String(params.page ?? 1),
			page_size: String(params.page_size ?? 50),
		},
	);
}

export interface AuthentikMessageInput {
	recipient_emails: string[];
	subject: string;
	html_body: string;
}

export interface AuthentikMessageResponse {
	status: string;
	request_id?: string;
	recipient_count?: number;
	error?: string;
}

export async function sendAuthentikMessage(
	serviceId: string,
	input: AuthentikMessageInput,
): Promise<AuthentikMessageResponse> {
	return post<AuthentikMessageResponse>(
		`/api/services/authentik/${serviceId}/message`,
		input,
	);
}

export async function fetchAuthentikMessageStatus(
	serviceId: string,
): Promise<Record<string, unknown>> {
	return get<Record<string, unknown>>(
		`/api/services/authentik/${serviceId}/message/status`,
	);
}
