import { api } from '@/auth/http';

export type WorkOrderStatus = 'OPEN' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED';
export type WorkOrderPriority = 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';

export interface WorkOrder {
  id: string;
  vehicleId: string;
  title: string;
  description?: string | null;
  status: WorkOrderStatus;
  priority: WorkOrderPriority;
  assignedTo?: string | null;
  openedAt: string;
  closedAt?: string | null;
  /** API sends numeric as string; coerce with Number. */
  laborCost?: number | string | null;
  partsCost?: number | string | null;
  createdAt: string;
  updatedAt: string;
}

export interface SaveWorkOrderPayload {
  vehicleId: string;
  title: string;
  description?: string;
  status?: WorkOrderStatus;
  priority?: WorkOrderPriority;
  assignedTo?: string;
  openedAt?: string;
  closedAt?: string;
  laborCost?: number;
  partsCost?: number;
}

export interface WorkOrderFilters {
  vehicleId?: string;
  status?: WorkOrderStatus;
}

export interface Part {
  id: string;
  name: string;
  sku?: string | null;
  category?: string | null;
  quantityInStock: number;
  reorderLevel: number;
  /** API sends numeric as string; coerce with Number. */
  unitCost?: number | string | null;
  location?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface SavePartPayload {
  name: string;
  sku?: string;
  category?: string;
  quantityInStock?: number;
  reorderLevel?: number;
  unitCost?: number;
  location?: string;
}

export interface PartFilters {
  category?: string;
  lowStock?: boolean;
}

export interface Warranty {
  id: string;
  vehicleId: string;
  component: string;
  provider?: string | null;
  startDate?: string | null;
  expiryDate: string;
  coverageNotes?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface SaveWarrantyPayload {
  vehicleId: string;
  component: string;
  provider?: string;
  startDate?: string;
  expiryDate: string;
  coverageNotes?: string;
}

export const maintenanceDepthService = {
  // Work Orders
  getWorkOrders: (filters: WorkOrderFilters = {}) => {
    const params = new URLSearchParams();
    if (filters.vehicleId) params.set('vehicleId', filters.vehicleId);
    if (filters.status) params.set('status', filters.status);
    const qs = params.toString();
    return api.get<WorkOrder[]>(`/maintenance/work-orders${qs ? `?${qs}` : ''}`);
  },
  getWorkOrder: (id: string) => api.get<WorkOrder>(`/maintenance/work-orders/${id}`),
  createWorkOrder: (data: SaveWorkOrderPayload) =>
    api.post<WorkOrder>('/maintenance/work-orders', data),
  updateWorkOrder: (id: string, data: Partial<SaveWorkOrderPayload>) =>
    api.patch<WorkOrder>(`/maintenance/work-orders/${id}`, data),
  deleteWorkOrder: (id: string) => api.delete(`/maintenance/work-orders/${id}`),

  // Parts / Tires
  getParts: (filters: PartFilters = {}) => {
    const params = new URLSearchParams();
    if (filters.category) params.set('category', filters.category);
    if (filters.lowStock) params.set('lowStock', 'true');
    const qs = params.toString();
    return api.get<Part[]>(`/maintenance/parts${qs ? `?${qs}` : ''}`);
  },
  createPart: (data: SavePartPayload) => api.post<Part>('/maintenance/parts', data),
  updatePart: (id: string, data: Partial<SavePartPayload>) =>
    api.patch<Part>(`/maintenance/parts/${id}`, data),
  deletePart: (id: string) => api.delete(`/maintenance/parts/${id}`),

  // Warranties
  getWarranties: (vehicleId?: string) => {
    const params = new URLSearchParams();
    if (vehicleId) params.set('vehicleId', vehicleId);
    const qs = params.toString();
    return api.get<Warranty[]>(`/maintenance/warranties${qs ? `?${qs}` : ''}`);
  },
  createWarranty: (data: SaveWarrantyPayload) =>
    api.post<Warranty>('/maintenance/warranties', data),
  deleteWarranty: (id: string) => api.delete(`/maintenance/warranties/${id}`),
};
