import { BaseEndpoint } from '../base';
import { CreateOrganizationRequest, APIKeyResponse, CreateAppRequest, AppAPIKey, CreateTenantRequest, TenantAPIKey, RollAPIKeyRequest, RolledAPIKey, CreateAPIKeyRequest, CreateOrgKeyRequest, CreateAppKeyRequest, ErrorResponse } from './types';

export class APIKeyEndpoint extends BaseEndpoint {
  constructor(client: any) {
    super(client, '/api/apikey');
  }

  /**
   * Create an organization and get an API key
   * @param data - The organization creation request
   * @returns Promise with the API key response
   */
  async createOrganization(data: CreateOrganizationRequest): Promise<APIKeyResponse> {
    return this.post<APIKeyResponse>('/org', data);
  }

  /**
   * Create an app and get an app API key
   * @param data - The app creation request
   * @returns Promise with the app API key response
   */
  async createApp(data: CreateAppRequest): Promise<AppAPIKey> {
    return this.post<AppAPIKey>('/app', data);
  }

  /**
   * Create a tenant and get a tenant API key
   * @param data - The tenant creation request
   * @returns Promise with the tenant API key response
   */
  async createTenant(data: CreateTenantRequest): Promise<TenantAPIKey> {
    return this.post<TenantAPIKey>('/tenant', data);
  }

  /**
   * Roll an API key (organization, app, or tenant)
   * @param data - The roll API key request
   * @returns Promise with the new rolled API key
   */
  async rollAPIKey(data: RollAPIKeyRequest): Promise<RolledAPIKey> {
    return this.post<RolledAPIKey>('/roll', data);
  }

  /**
   * Create a new API key for an existing organization, app, or tenant
   * @param data - The API key creation request
   * @returns Promise with the new API key
   */
  async createAPIKey(data: CreateAPIKeyRequest): Promise<APIKeyResponse> {
    return this.post<APIKeyResponse>('/create', data);
  }

  /**
   * Creates a new API key for an existing organization using JWT authentication
   * 
   * @param organizationId - ID of the existing organization
   * @param jwtToken - Valid Supabase JWT token
   * @returns Promise<APIKeyResponse>
   * @throws Error if request fails
   */
  async createOrgKey(organizationId: string, jwtToken: string): Promise<APIKeyResponse> {
    try {
      return await this.post<APIKeyResponse>('/api/apikey/org/jwt', {
        organization_id: organizationId
      }, {
        headers: {
          'Authorization': `Bearer ${jwtToken}`,
          'Content-Type': 'application/json'
        }
      });
    } catch (error: any) {
      throw new Error(error.response?.data?.error || 'Failed to create organization API key');
    }
  }

  /**
   * Creates a new API key for an app in the user's organization using JWT authentication
   * 
   * @param appName - Name of the app
   * @param tenancyModel - App's tenancy model ("single" or "multi")
   * @param jwtToken - Valid Supabase JWT token
   * @returns Promise<APIKeyResponse>
   * @throws Error if request fails
   */
  async createAppKey(
    appName: string,
    tenancyModel: 'single' | 'multi',
    jwtToken: string
  ): Promise<APIKeyResponse> {
    try {
      return await this.post<APIKeyResponse>('/api/apikey/app/jwt', {
        app_name: appName,
        tenancy_model: tenancyModel
      }, {
        headers: {
          'Authorization': `Bearer ${jwtToken}`,
          'Content-Type': 'application/json'
        }
      });
    } catch (error: any) {
      throw new Error(error.response?.data?.error || 'Failed to create app API key');
    }
  }
} 