import api from './api';
import { AxiosResponse, CustomersResponse } from '../types/models';

export const adminService = {
  // Oil pricing
  getOilPricing: () =>
    api.get('/admin/oil/get'),

  updateOilPricing: (data: any) =>
    api.post('/admin/oil/create', data),

  // VoIP
  getVoipRouting: () =>
    api.get('/admin/voip_routing'),

  // Employee management
  employees: {
    create: (data: any) =>
      api.post('/employee/create', data),

    getById: (id: number) =>
      api.get(`/employee/${id}`),

    getByIdAlt: (id: number) =>
      api.get(`/employee/byid/${id}`),

    getByUserId: (id: number) =>
      api.get(`/employee/userid/${id}`),

    update: (id: number, data: any) =>
      api.put(`/employee/edit/${id}`, data),

    getAll: (page: number = 1) =>
      api.get(`/employee/all/${page}`),

    getDrivers: () =>
      api.get('/employee/drivers'),
  },

  // Promotions
  promos: {
    getAll: () =>
      api.get('/promo/all'),

    getById: (id: number) =>
      api.get(`/promo/${id}`),

    getPrice: (id: number) =>
      api.get(`/promo/promoprice/${id}`),

    create: (data: any) =>
      api.post('/promo/create', data),

    update: (id: number, data: any) =>
      api.put(`/promo/edit/${id}`, data),

    delete: (id: number) =>
      api.delete(`/promo/delete/${id}`),

    enable: (id: number) =>
      api.put(`/promo/on/${id}`),

    disable: (id: number) =>
      api.put(`/promo/off/${id}`),
  },

  // Statistics
  stats: {
    deliveryCountToday: () =>
      api.get('/stats/delivery/count/today'),

    deliveredCountToday: () =>
      api.get('/stats/delivery/count/delivered/today'),

    employeeDeliveryTotal: (employeeId: number) =>
      api.get(`/stats/delivery/total/${employeeId}`),

    employeeGallonsTotal: (employeeId: number) =>
      api.get(`/stats/gallons/total/${employeeId}`),

    weeklyGallons: () =>
      api.get('/stats/gallons/week'),

    customerGallonsTotal: (customerId: number) =>
      api.get(`/stats/gallons/check/total/${customerId}`),

    employeePrimesTotal: (employeeId: number) =>
      api.get(`/stats/primes/total/${employeeId}`),

    userLastDelivery: (userId: number) =>
      api.get(`/stats/user/lastdelivery/${userId}`),

    userStats: (userId: number) =>
      api.get(`/stats/user/${userId}`),

    serviceCallsToday: () =>
      api.get('/stats/call/count/today'),

    sidebarCounts: () =>
      api.get('/deliverystatus/stats/sidebar-counts'),

    todayTotals: () =>
      api.get('/deliverystatus/today-totals'),

    tomorrowTotals: () =>
      api.get('/deliverystatus/tomorrow-totals'),

    waitingTotals: () =>
      api.get('/deliverystatus/waiting-totals'),

    pendingStatus: () =>
      api.get('/deliverystatus/pending'),

    // New methods for Stats section
    getDailyStats: (startDate: string, endDate: string) =>
      api.get(`/stats/deliveries/daily?start_date=${startDate}&end_date=${endDate}`),

    getTotals: (period: string, date: string) =>
      api.get(`/stats/deliveries/totals?period=${period}&date=${date}`),
  },

  // Money & reporting
  money: {
    weeklyProfit: () =>
      api.get('/money/profit/week'),

    yearlyProfit: () =>
      api.get('/money/profit/year'),

    customerListReport: (): Promise<AxiosResponse<CustomersResponse>> =>
      api.get('/report/customers/list'),
  },

  // Admin Settings
  settings: {
    get: () =>
      api.get('/admin/settings'),

    update: (data: Record<string, unknown>) =>
      api.put('/admin/settings', data),

    uploadLogo: (file: File) => {
      const formData = new FormData();
      formData.append('logo', file);
      return api.post('/admin/settings/logo', formData, {
        headers: { 'Content-Type': 'multipart/form-data' },
      });
    },

    deleteLogo: () =>
      api.delete('/admin/settings/logo'),
  },

  // Social/comments
  social: {
    getPosts: (customerId: number, page: number = 1) =>
      api.get(`/social/posts/${customerId}/${page}`),

    createPost: (customerId: number, data: any) =>
      api.post(`/social/create/${customerId}`, data),

    deletePost: (postId: number) =>
      api.delete(`/social/delete/${postId}`),
  },
};

export default adminService;
