/**
 * @ai-summary API calls for vehicles feature
 */

import { apiClient } from '../../../core/api/client';
import { Vehicle, CreateVehicleRequest, UpdateVehicleRequest, DecodedVehicleData, TCOResponse } from '../types/vehicles.types';

// All requests (including dropdowns) use authenticated apiClient

export const vehiclesApi = {
  getAll: async (): Promise<Vehicle[]> => {
    const response = await apiClient.get('/vehicles');
    return response.data;
  },
  
  getById: async (id: string): Promise<Vehicle> => {
    const response = await apiClient.get(`/vehicles/${id}`);
    return response.data;
  },
  
  create: async (data: CreateVehicleRequest): Promise<Vehicle> => {
    const response = await apiClient.post('/vehicles', data);
    return response.data;
  },
  
  update: async (id: string, data: UpdateVehicleRequest): Promise<Vehicle> => {
    const response = await apiClient.put(`/vehicles/${id}`, data);
    return response.data;
  },
  
  delete: async (id: string): Promise<void> => {
    await apiClient.delete(`/vehicles/${id}`);
  },

  // Dropdown API methods (authenticated) - proxied through vehicles capsule
  getYears: async (): Promise<number[]> => {
    const response = await apiClient.get('/vehicles/dropdown/years');
    return response.data;
  },

  getMakes: async (year: number): Promise<string[]> => {
    const response = await apiClient.get(`/vehicles/dropdown/makes?year=${year}`);
    return response.data;
  },

  getModels: async (year: number, make: string): Promise<string[]> => {
    const response = await apiClient.get(`/vehicles/dropdown/models?year=${year}&make=${encodeURIComponent(make)}`);
    return response.data;
  },

  getTransmissions: async (year: number, make: string, model: string, trim: string): Promise<string[]> => {
    const response = await apiClient.get(`/vehicles/dropdown/transmissions?year=${year}&make=${encodeURIComponent(make)}&model=${encodeURIComponent(model)}&trim=${encodeURIComponent(trim)}`);
    return response.data;
  },

  getEngines: async (year: number, make: string, model: string, trim: string): Promise<string[]> => {
    const response = await apiClient.get(`/vehicles/dropdown/engines?year=${year}&make=${encodeURIComponent(make)}&model=${encodeURIComponent(model)}&trim=${encodeURIComponent(trim)}`);
    return response.data;
  },

  getTrims: async (year: number, make: string, model: string): Promise<string[]> => {
    const response = await apiClient.get(`/vehicles/dropdown/trims?year=${year}&make=${encodeURIComponent(make)}&model=${encodeURIComponent(model)}`);
    return response.data;
  },

  uploadImage: async (vehicleId: string, file: File): Promise<Vehicle> => {
    const formData = new FormData();
    formData.append('file', file);
    const response = await apiClient.post(`/vehicles/${vehicleId}/image`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
      timeout: 60000 // 60 seconds for file uploads
    });
    return response.data;
  },

  deleteImage: async (vehicleId: string): Promise<void> => {
    await apiClient.delete(`/vehicles/${vehicleId}/image`);
  },

  getImageUrl: (vehicleId: string): string => {
    return `/api/vehicles/${vehicleId}/image`;
  },

  /**
   * Decode VIN using NHTSA vPIC API
   * Requires Pro or Enterprise tier
   */
  decodeVin: async (vin: string): Promise<DecodedVehicleData> => {
    const response = await apiClient.post('/vehicles/decode-vin', { vin });
    return response.data;
  },

  /**
   * Get Total Cost of Ownership data for a vehicle
   */
  getTCO: async (vehicleId: string): Promise<TCOResponse> => {
    const response = await apiClient.get(`/vehicles/${vehicleId}/tco`);
    return response.data;
  }
};
