import { request } from '@umijs/max';
import type {
  VehicleFormValues,
  VehicleItem,
  VehicleListParams,
  VehicleListResponse,
  VehicleStatus,
} from '@/pages/VehicleManagement/data';

/** 获取车辆列表 GET /api/vehicles */
export async function getVehicleList(params?: VehicleListParams) {
  return request<VehicleListResponse>('/api/vehicles', {
    method: 'GET',
    params,
  });
}

/** 获取车辆详情 GET /api/vehicles/:id */
export async function getVehicleDetail(id: string) {
  return request<VehicleItem>(`/api/vehicles/${id}`, {
    method: 'GET',
  });
}

/** 新增车辆 POST /api/vehicles */
export async function createVehicle(data: VehicleFormValues) {
  return request<VehicleItem>('/api/vehicles', {
    method: 'POST',
    data,
  });
}

/** 编辑车辆 PUT /api/vehicles/:id */
export async function updateVehicle(data: VehicleFormValues) {
  if (!data.id) {
    throw new Error('Vehicle id is required');
  }
  return request<VehicleItem>(`/api/vehicles/${data.id}`, {
    method: 'PUT',
    data,
  });
}

/** 更新车辆状态 PUT /api/vehicles/:id/status */
export async function updateVehicleStatus(id: string, status: VehicleStatus) {
  return request<{ success: boolean }>(`/api/vehicles/${id}/status`, {
    method: 'PUT',
    data: { status },
  });
}

/** 删除车辆 DELETE /api/vehicles/:id */
export async function deleteVehicle(id: string) {
  return request<{ success: boolean }>(`/api/vehicles/${id}`, {
    method: 'DELETE',
  });
}

