import api from './api';
import {
  Customer,
  CustomerDescription,
  TankInspection,
  CreateCustomerRequest,
  UpdateCustomerRequest,
  CustomersResponse,
  CustomerResponse,
  AxiosResponse
} from '../types/models';

// API Response wrappers for this service
interface CountResponse { ok: boolean; count: number; }
interface DescriptionResponse { ok: boolean; description: CustomerDescription; }
interface TankResponse { ok: boolean; tank: TankInspection; }
interface StatusResponse { ok: boolean; status: number; }
interface SearchResponse { ok: boolean; customers: Customer[]; }

export const customerService = {
  // CRUD operations
  getAll: (page: number = 1): Promise<AxiosResponse<CustomersResponse>> =>
    api.get(`/customer/all/${page}`),

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

  create: (data: CreateCustomerRequest): Promise<AxiosResponse<CustomerResponse>> =>
    api.post('/customer/create', data),

  update: (id: number, data: UpdateCustomerRequest): Promise<AxiosResponse<CustomerResponse>> =>
    api.put(`/customer/edit/${id}`, data),

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

  getCount: (): Promise<AxiosResponse<CountResponse>> =>
    api.get('/customer/count'),

  getActiveCount: (): Promise<AxiosResponse<CountResponse>> =>
    api.get('/customer/count/active'),

  getDedicatedCount: (): Promise<AxiosResponse<CountResponse>> =>
    api.get('/customer/count/dedicated'),

  // Profile & details
  getDescription: (id: number): Promise<AxiosResponse<DescriptionResponse>> =>
    api.get(`/customer/description/${id}`),

  // Tank information
  getTank: (id: number): Promise<AxiosResponse<TankResponse>> =>
    api.get(`/customer/tank/${id}`),

  updateTank: (id: number, data: any): Promise<AxiosResponse<{ ok: boolean }>> =>
    api.put(`/customer/edit/tank/${id}`, data),

  // Automatic delivery
  getAutomaticStatus: (id: number): Promise<AxiosResponse<StatusResponse>> =>
    api.get(`/customer/automatic/status/${id}`),

  assignAutomatic: (id: number, data: { status: number }): Promise<AxiosResponse<StatusResponse>> =>
    api.put(`/customer/automatic/assign/${id}`, data),

  // Search
  search: (query: string): Promise<AxiosResponse<SearchResponse>> =>
    api.get(`/search/customer?q=${encodeURIComponent(query)}`),

  // Customer alerts
  getAlert: (id: number): Promise<AxiosResponse<any>> =>
    api.get(`/deliverydata/customer/alert/${id}`),

  createAlert: (id: number, data: { severity: number; message: string }): Promise<AxiosResponse<any>> =>
    api.post(`/deliverydata/customer/alert/${id}`, data),

  updateAlert: (id: number, data: { severity: number; message: string }): Promise<AxiosResponse<any>> =>
    api.put(`/deliverydata/customer/alert/${id}`, data),

  deleteAlert: (id: number): Promise<AxiosResponse<any>> =>
    api.delete(`/deliverydata/customer/alert/${id}`),
};

export default customerService;
