/**
 * API Keys API
 *
 * Endpoints for managing API keys (Gold and Platinum plans only)
 */

import { get, post, del } from './client';

export interface ApiKeyDto {
  id: string;
  name: string;
  keyPrefix: string;
  isActive: boolean;
  lastUsedAt: string | null;
  expiresAt: string | null;
  createdAt: string;
}

export interface CreateApiKeyResultDto extends ApiKeyDto {
  /** Full key — shown only once at creation time */
  fullKey: string;
}

export interface CreateApiKeyRequest {
  name: string;
  expiresAt?: string;
}

/**
 * List all API keys for the current organization
 * GET /api/v1/api-keys
 * Requires: Gold or Platinum plan
 */
export async function listApiKeys(): Promise<ApiKeyDto[]> {
  return get<ApiKeyDto[]>('/api/v1/api-keys');
}

/**
 * Create a new API key
 * POST /api/v1/api-keys
 * Requires: Gold or Platinum plan
 * Returns the full key — shown only once
 */
export async function createApiKey(data: CreateApiKeyRequest): Promise<CreateApiKeyResultDto> {
  return post<CreateApiKeyResultDto>('/api/v1/api-keys', data);
}

/**
 * Revoke an API key (immediate and irreversible)
 * DELETE /api/v1/api-keys/:id
 * Requires: Gold or Platinum plan
 */
export async function revokeApiKey(id: string): Promise<void> {
  return del<void>(`/api/v1/api-keys/${id}`);
}
