import api, { autoApi } from './api';
import {
  Delivery,
  CreateDeliveryRequest,
  UpdateDeliveryRequest,
  DeliveriesResponse,
  DeliveryResponse,
  DeliveriesMapResponse,
  DeliveryHistoryResponse,
  AxiosResponse
} from '../types/models';

// API Response wrappers for this service
interface DeliveryTotalResponse { ok: boolean; total: number; priceprime?: number; pricesameday?: number; priceemergency?: number; total_amount?: number; discount?: number; total_amount_after_discount?: number; }
interface CashResponse { ok: boolean; amount: number; }

export const deliveryService = {
  // CRUD operations
  create: (customerId: number, data: CreateDeliveryRequest): Promise<AxiosResponse<DeliveryResponse>> =>
    api.post(`/delivery/create/${customerId}`, data),

  getById: (id: number): Promise<AxiosResponse<DeliveryResponse>> =>
    api.get(`/delivery/${id}`),

  getOrder: (id: number): Promise<AxiosResponse<DeliveryResponse>> =>
    api.get(`/delivery/order/${id}`),

  getForMap: (date: string, all = false): Promise<AxiosResponse<DeliveriesMapResponse>> =>
    api.get(`/delivery/map`, { params: all ? { all: true } : { date } }),

  getHistory: (startDate: string, endDate: string): Promise<AxiosResponse<DeliveryHistoryResponse>> =>
    api.get(`/delivery/history`, { params: { start_date: startDate, end_date: endDate } }),

  update: (id: number, data: UpdateDeliveryRequest): Promise<AxiosResponse<DeliveryResponse>> =>
    api.put(`/delivery/edit/${id}`, data),

  delete: (id: number): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.delete(`/delivery/delete/${id}`),

  cancel: (id: number): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.put(`/delivery/cancel/${id}`),

  markCancelled: (id: number): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.put(`/delivery/cancelled/${id}`),

  // List operations
  getAll: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/all/${page}`),

  getByCustomer: (customerId: number, page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/customer/${customerId}/${page}`),

  getPast1: (customerId: number): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/past1/${customerId}`),

  getPast2: (customerId: number): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/past2/${customerId}`),

  getTicketPast: (customerId: number): Promise<AxiosResponse<{ ok: boolean; deliveries: { when_delivered: string; gallons_delivered: number; automatic: number }[] }>> =>
    api.get(`/delivery/ticket/past/${customerId}`),

  // Status-based lists
  getWaiting: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/waiting/${page}`),

  getTomorrow: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/tommorrow/${page}`),

  getOutForDelivery: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/outfordelivery/${page}`),

  getDelivered: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/delivered/${page}`),

  getFinalized: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/finalized/${page}`),

  getPending: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/pending/${page}`),

  getIssues: (page: number = 1): Promise<AxiosResponse<DeliveriesResponse>> =>
    api.get(`/delivery/issue/${page}`),

  deleteCancelled: (id: number): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.delete(`/delivery/cancelled/${id}`),

  // Totals
  getWaitingTotals: (): Promise<AxiosResponse<{ totals: any[]; grand_total: number }>> =>
    api.get('/deliverystatus/waiting-totals'),

  getTodayTotals: (): Promise<AxiosResponse<{ totals: any[]; grand_total: number }>> =>
    api.get('/deliverystatus/today-totals'),

  getTomorrowTotals: (): Promise<AxiosResponse<{ totals: any[]; grand_total: number }>> =>
    api.get('/deliverystatus/tomorrow-totals'),

  // Status & totals
  updateStatus: (data: { id: number; status: number }): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.put('/delivery/updatestatus', data),

  getTotal: (id: number): Promise<AxiosResponse<DeliveryTotalResponse>> =>
    api.get(`/delivery/total/${id}`),

  // Cash handling
  handleCash: (id: number, type: string): Promise<AxiosResponse<CashResponse>> =>
    api.get(`/delivery/cash/${id}/${type}`),

  // Finalize
  finalize: (id: number, data?: { final_price: number }): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.put(`/deliverydata/finalize/${id}`, data),

  // Auto system endpoints (VITE_AUTO_URL)
  auto: {
    getDelivery: (id: number) =>
      autoApi.get(`/delivery/delivery/${id}`),

    getProfileDeliveries: (id: number) =>
      autoApi.get(`/delivery/all/profile/${id}`),

    getAllCustomers: () =>
      autoApi.get('/delivery/all/customers'),

    getByCustomer: (id: number) =>
      autoApi.get(`/delivery/auto/customer/${id}`),

    getTicket: (id: number) =>
      autoApi.get(`/delivery/autoticket/${id}`),

    findDelivery: (id: number) =>
      autoApi.get(`/delivery/finddelivery/${id}`),

    updateStatus: (id: number, data: any) =>
      autoApi.put(`/delivery/update_status/${id}`, data),

    confirm: (data: any) =>
      autoApi.post('/confirm/delivery', data),

    createTicket: (id: number, data: any) =>
      autoApi.post(`/confirm/auto/create/${id}`, data),

    createTicketNoPreauth: (id: number, data: any) =>
      autoApi.post(`/confirm/auto/create/nopreauth/${id}`, data),

    closeTicket: (id: number, data?: any) =>
      autoApi.put(`/confirm/auto/close_ticket/${id}`, data),

    updateTicket: (id: number, data: any) =>
      autoApi.put(`/confirm/auto/update/${id}`, data),

    estimateGallons: (customerId: number) =>
      autoApi.get(`/fixstuff_customer/estimate_gallons/customer/${customerId}`),

    updateHouseFactor: (customerId: number, data: { house_factor: number }) =>
      autoApi.put(`/delivery/auto/customer/${customerId}/house_factor`, data),

    updateCustomerHouseFactor: (customerId: number, data: { house_factor: number }) =>
      autoApi.put(`/fixstuff_customer/house_factor/${customerId}`, data),
  },

  // Convenience wrappers for commonly used auto operations
  getAutoDelivery: (id: number) =>
    autoApi.get(`/delivery/delivery/${id}`),

  getAutoDeliveriesByCustomer: (customerId: number) =>
    autoApi.get(`/delivery/all/profile/${customerId}`),
};

export default deliveryService;
