import { apiClient, ensureCsrfCookie } from './client';

/**
 * API-ключи тенанта (audit D2/D3). Backend: ApiKeyController.
 * Полный ключ доступен только в ответе regenerateApiKey().
 */
export interface ApiKeyInfo {
    id: number;
    name: string;
    key_prefix: string;
    last_used_at: string | null;
    expires_at: string | null;
    created_at: string | null;
}

export interface RegeneratedApiKey {
    id: number;
    name: string;
    key: string;
    key_prefix: string;
}

export async function listApiKeys(): Promise<ApiKeyInfo[]> {
    const { data } = await apiClient.get<{ data: ApiKeyInfo[] }>('/api/api-keys');
    return data.data;
}

export async function regenerateApiKey(): Promise<RegeneratedApiKey> {
    await ensureCsrfCookie();
    const { data } = await apiClient.post<RegeneratedApiKey>('/api/api-keys/regenerate');
    return data;
}
