/**
 * @ai-summary API client for Community Gas Stations feature
 */

import { apiClient } from '@/core/api/client';
import {
  CommunityStation,
  SubmitStationData,
  ReviewDecision,
  CommunityStationsListResponse,
  StationBounds,
  RemovalReportResponse
} from '../types/community-stations.types';

const API_BASE = '/stations/community';

class CommunityStationsApiClient {
  /**
   * Submit a new community gas station
   */
  async submitStation(data: SubmitStationData): Promise<CommunityStation> {
    try {
      const response = await apiClient.post<CommunityStation>(
        API_BASE,
        data
      );
      return response.data;
    } catch (error) {
      console.error('Submit station failed:', error);
      throw error;
    }
  }

  /**
   * Get user's submitted stations
   */
  async getMySubmissions(): Promise<CommunityStation[]> {
    try {
      const response = await apiClient.get<CommunityStation[]>(
        `${API_BASE}/mine`
      );
      return response.data || [];
    } catch (error) {
      console.error('Get submissions failed:', error);
      throw error;
    }
  }

  /**
   * Withdraw a submission
   */
  async withdrawSubmission(id: string): Promise<void> {
    try {
      await apiClient.delete(`${API_BASE}/${id}`);
    } catch (error) {
      console.error('Withdraw submission failed:', error);
      throw error;
    }
  }

  /**
   * Get approved community stations
   */
  async getApprovedStations(page: number = 0, limit: number = 50): Promise<CommunityStationsListResponse> {
    try {
      const response = await apiClient.get<CommunityStationsListResponse>(
        `${API_BASE}/approved`,
        {
          params: { page, limit }
        }
      );
      return response.data;
    } catch (error) {
      console.error('Get approved stations failed:', error);
      throw error;
    }
  }

  /**
   * Get approved stations nearby
   */
  async getApprovedNearby(
    latitude: number,
    longitude: number,
    radiusMeters: number = 5000
  ): Promise<CommunityStation[]> {
    try {
      const response = await apiClient.post<{ stations: CommunityStation[] }>(
        `${API_BASE}/nearby`,
        { latitude, longitude, radiusMeters }
      );
      return response.data?.stations || [];
    } catch (error) {
      console.error('Get nearby stations failed:', error);
      throw error;
    }
  }

  /**
   * Get approved stations within map bounds
   */
  async getApprovedInBounds(bounds: StationBounds): Promise<CommunityStation[]> {
    try {
      const response = await apiClient.post<{ stations: CommunityStation[] }>(
        `${API_BASE}/bounds`,
        bounds
      );
      return response.data?.stations || [];
    } catch (error) {
      console.error('Get stations in bounds failed:', error);
      throw error;
    }
  }

  /**
   * Report a station as no longer having Premium 93
   */
  async reportRemoval(stationId: string, reason?: string): Promise<RemovalReportResponse> {
    try {
      const response = await apiClient.post<RemovalReportResponse>(
        `${API_BASE}/${stationId}/report-removal`,
        { reason: reason || 'No longer has Premium 93' }
      );
      return response.data;
    } catch (error) {
      console.error('Report removal failed:', error);
      throw error;
    }
  }

  /**
   * Admin: Get all submissions with filtering
   */
  async getAllSubmissions(
    status?: string,
    page: number = 0,
    limit: number = 50
  ): Promise<CommunityStationsListResponse> {
    try {
      const response = await apiClient.get<CommunityStationsListResponse>(
        `${API_BASE}/admin/submissions`,
        {
          params: { status, page, limit }
        }
      );
      return response.data;
    } catch (error) {
      console.error('Get submissions failed:', error);
      throw error;
    }
  }

  /**
   * Admin: Get pending submissions
   */
  async getPendingSubmissions(page: number = 0, limit: number = 50): Promise<CommunityStationsListResponse> {
    try {
      const response = await apiClient.get<CommunityStationsListResponse>(
        `${API_BASE}/admin/pending`,
        {
          params: { page, limit }
        }
      );
      return response.data;
    } catch (error) {
      console.error('Get pending submissions failed:', error);
      throw error;
    }
  }

  /**
   * Admin: Review a submission (approve/reject)
   */
  async reviewStation(id: string, decision: ReviewDecision): Promise<CommunityStation> {
    try {
      const response = await apiClient.patch<CommunityStation>(
        `${API_BASE}/admin/${id}/review`,
        decision
      );
      return response.data;
    } catch (error) {
      console.error('Review station failed:', error);
      throw error;
    }
  }

  /**
   * Admin: Bulk review submissions
   */
  async bulkReviewStations(ids: string[], decision: ReviewDecision): Promise<CommunityStation[]> {
    try {
      const response = await apiClient.post<CommunityStation[]>(
        `${API_BASE}/admin/bulk-review`,
        { ids, ...decision }
      );
      return response.data || [];
    } catch (error) {
      console.error('Bulk review failed:', error);
      throw error;
    }
  }
}

export const communityStationsApi = new CommunityStationsApiClient();
