/**
 * @ai-summary API client for maintenance records and schedules
 * @ai-context Follows pattern from documents.api.ts with full CRUD operations
 */

import { apiClient } from '../../../core/api/client';
import type {
  CreateMaintenanceRecordRequest,
  UpdateMaintenanceRecordRequest,
  MaintenanceRecordResponse,
  CreateScheduleRequest,
  UpdateScheduleRequest,
  MaintenanceScheduleResponse,
  MaintenanceCategory
} from '../types/maintenance.types';

export const maintenanceApi = {
  // Maintenance Records
  async createRecord(data: CreateMaintenanceRecordRequest): Promise<MaintenanceRecordResponse> {
    const res = await apiClient.post<MaintenanceRecordResponse>('/maintenance/records', data);
    return res.data;
  },

  async getRecords(): Promise<MaintenanceRecordResponse[]> {
    const res = await apiClient.get<MaintenanceRecordResponse[]>('/maintenance/records');
    return res.data;
  },

  async getRecord(id: string): Promise<MaintenanceRecordResponse> {
    const res = await apiClient.get<MaintenanceRecordResponse>(`/maintenance/records/${id}`);
    return res.data;
  },

  async updateRecord(id: string, data: UpdateMaintenanceRecordRequest): Promise<MaintenanceRecordResponse> {
    const res = await apiClient.put<MaintenanceRecordResponse>(`/maintenance/records/${id}`, data);
    return res.data;
  },

  async deleteRecord(id: string): Promise<void> {
    await apiClient.delete(`/maintenance/records/${id}`);
  },

  async getRecordsByVehicle(vehicleId: string): Promise<MaintenanceRecordResponse[]> {
    const res = await apiClient.get<MaintenanceRecordResponse[]>(`/maintenance/records/vehicle/${vehicleId}`);
    return res.data;
  },

  // Maintenance Schedules
  async createSchedule(data: CreateScheduleRequest): Promise<MaintenanceScheduleResponse> {
    const res = await apiClient.post<MaintenanceScheduleResponse>('/maintenance/schedules', data);
    return res.data;
  },

  async getSchedulesByVehicle(vehicleId: string): Promise<MaintenanceScheduleResponse[]> {
    const res = await apiClient.get<MaintenanceScheduleResponse[]>(`/maintenance/schedules/vehicle/${vehicleId}`);
    return res.data;
  },

  async updateSchedule(id: string, data: UpdateScheduleRequest): Promise<MaintenanceScheduleResponse> {
    const res = await apiClient.put<MaintenanceScheduleResponse>(`/maintenance/schedules/${id}`, data);
    return res.data;
  },

  async deleteSchedule(id: string): Promise<void> {
    await apiClient.delete(`/maintenance/schedules/${id}`);
  },

  async getUpcoming(vehicleId: string, currentMileage?: number): Promise<MaintenanceScheduleResponse[]> {
    const params = currentMileage ? { current_mileage: currentMileage } : {};
    const res = await apiClient.get<MaintenanceScheduleResponse[]>(
      `/maintenance/schedules/vehicle/${vehicleId}/upcoming`,
      { params }
    );
    return res.data;
  },

  // Utility endpoints
  async getSubtypes(category: MaintenanceCategory): Promise<string[]> {
    const res = await apiClient.get<{ subtypes: string[] }>(`/maintenance/subtypes/${category}`);
    return res.data.subtypes;
  }
};
