/**
 * POI Context API Client
 *
 * API client for POI detection and context management
 */

import { apiClient } from '../../api/client/apiClient';
import type {
  POIDetectionResponse,
  POIContextResponse,
  FeatureImportanceResponse,
  CompetitorAnalysis,
  POICacheStats
} from '@/types/poi';

const POI_BASE_URL = '/tenants';

export const poiContextApi = {
  /**
   * Detect POIs for a tenant's bakery location
   */
  async detectPOIs(
    tenantId: string,
    latitude: number,
    longitude: number,
    forceRefresh: boolean = false
  ): Promise<POIDetectionResponse> {
    const response = await apiClient.post<POIDetectionResponse>(
      `/tenants/${tenantId}/external/poi-context/detect`,
      null,
      {
        params: {
          latitude,
          longitude,
          force_refresh: forceRefresh
        }
      }
    );
    return response;
  },

  /**
   * Get POI context for a tenant
   */
  async getPOIContext(tenantId: string): Promise<POIContextResponse> {
    const response = await apiClient.get<POIContextResponse>(
      `/tenants/${tenantId}/external/poi-context`
    );
    return response;
  },

  /**
   * Refresh POI context for a tenant
   */
  async refreshPOIContext(tenantId: string): Promise<POIDetectionResponse> {
    const response = await apiClient.post<POIDetectionResponse>(
      `/tenants/${tenantId}/external/poi-context/refresh`
    );
    return response;
  },

  /**
   * Delete POI context for a tenant
   */
  async deletePOIContext(tenantId: string): Promise<void> {
    await apiClient.delete(`/tenants/${tenantId}/external/poi-context`);
  },

  /**
   * Get feature importance summary
   */
  async getFeatureImportance(tenantId: string): Promise<FeatureImportanceResponse> {
    const response = await apiClient.get<FeatureImportanceResponse>(
      `/tenants/${tenantId}/external/poi-context/feature-importance`
    );
    return response;
  },

  /**
   * Get competitor analysis
   */
  async getCompetitorAnalysis(tenantId: string): Promise<{
    tenant_id: string;
    location: { latitude: number; longitude: number };
    competitor_analysis: CompetitorAnalysis;
    insights: string[];
  }> {
    const response = await apiClient.get(
      `/tenants/${tenantId}/external/poi-context/competitor-analysis`
    );
    return response;
  },

  /**
   * Check POI service health (system level)
   */
  async checkHealth(): Promise<{ status: string; overpass_api: any }> {
    const response = await apiClient.get(`/health/poi-context`);
    return response;
  },

  /**
   * Get cache statistics (system level)
   */
  async getCacheStats(): Promise<{ status: string; cache_stats: POICacheStats }> {
    const response = await apiClient.get(`/cache/poi-context/stats`);
    return response;
  }
};
