import axios from 'axios';
import { Filament } from '@/src/types/filament';
import { Customer, Sale, CreateSaleRequest, AnalyticsOverview, TopSeller, RevenueDataPoint, InventoryAlert, TypeBreakdown } from '@/src/types/sales';

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api';

// Create axios instance with default config
const api = axios.create({
  baseURL: API_URL,
  headers: {
    'Content-Type': 'application/json',
  },
});

// Add auth token to requests
api.interceptors.request.use((config) => {
  const token = localStorage.getItem('authToken');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// Handle auth errors
api.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response?.status === 401 || error.response?.status === 403) {
      // Clear auth data
      localStorage.removeItem('authToken');
      localStorage.removeItem('tokenExpiry');
      
      // Only redirect if we're in a protected admin route
      if (window.location.pathname.includes('/upadaj/')) {
        window.location.href = '/upadaj';
      }
    }
    return Promise.reject(error);
  }
);

export const authService = {
  login: async (username: string, password: string) => {
    const response = await api.post('/login', { username, password });
    return response.data;
  },
};

export const colorService = {
  getAll: async () => {
    const response = await api.get('/colors');
    return response.data;
  },
  
  create: async (color: { name: string; hex: string; cena_refill?: number; cena_spulna?: number }) => {
    const response = await api.post('/colors', color);
    return response.data;
  },
  
  update: async (id: string, color: { name: string; hex: string; cena_refill?: number; cena_spulna?: number }) => {
    const response = await api.put(`/colors/${id}`, color);
    return response.data;
  },
  
  delete: async (id: string) => {
    const response = await api.delete(`/colors/${id}`);
    return response.data;
  },
};

export const filamentService = {
  getAll: async () => {
    const cacheBuster = Date.now();
    const response = await api.get(`/filaments?_t=${cacheBuster}`);
    return response.data;
  },
  
  create: async (filament: Partial<Filament>) => {
    const response = await api.post('/filaments', filament);
    return response.data;
  },
  
  update: async (id: string, filament: Partial<Filament>) => {
    const response = await api.put(`/filaments/${id}`, filament);
    return response.data;
  },
  
  delete: async (id: string) => {
    const response = await api.delete(`/filaments/${id}`);
    return response.data;
  },
  
  updateBulkSale: async (data: {
    filamentIds?: string[];
    salePercentage: number;
    saleStartDate?: string;
    saleEndDate?: string;
    enableSale: boolean;
  }) => {
    const response = await api.post('/filaments/sale/bulk', data);
    return response.data;
  },
};

export const colorRequestService = {
  getAll: async () => {
    const response = await api.get('/color-requests');
    return response.data;
  },
  
  submit: async (request: {
    color_name: string;
    material_type: string;
    finish_type?: string;
    user_email: string;
    user_phone: string;
    user_name?: string;
    description?: string;
    reference_url?: string;
  }) => {
    const response = await api.post('/color-requests', request);
    return response.data;
  },
  
  updateStatus: async (id: string, status: string, admin_notes?: string) => {
    const response = await api.put(`/color-requests/${id}`, { status, admin_notes });
    return response.data;
  },
  
  delete: async (id: string) => {
    const response = await api.delete(`/color-requests/${id}`);
    return response.data;
  },
};

export const customerService = {
  getAll: async (): Promise<Customer[]> => {
    const response = await api.get('/customers');
    return response.data;
  },

  search: async (q: string): Promise<Customer[]> => {
    const response = await api.get(`/customers/search?q=${encodeURIComponent(q)}`);
    return response.data;
  },

  getById: async (id: string): Promise<Customer & { sales: Sale[] }> => {
    const response = await api.get(`/customers/${id}`);
    return response.data;
  },

  create: async (customer: Partial<Customer>): Promise<Customer> => {
    const response = await api.post('/customers', customer);
    return response.data;
  },

  update: async (id: string, customer: Partial<Customer>): Promise<Customer> => {
    const response = await api.put(`/customers/${id}`, customer);
    return response.data;
  },
};

export const saleService = {
  getAll: async (page = 1, limit = 50): Promise<{ sales: Sale[]; total: number }> => {
    const response = await api.get(`/sales?page=${page}&limit=${limit}`);
    return response.data;
  },

  getById: async (id: string): Promise<Sale> => {
    const response = await api.get(`/sales/${id}`);
    return response.data;
  },

  create: async (data: CreateSaleRequest): Promise<Sale> => {
    const response = await api.post('/sales', data);
    return response.data;
  },

  delete: async (id: string): Promise<void> => {
    await api.delete(`/sales/${id}`);
  },
};

export const analyticsService = {
  getOverview: async (period = '30d'): Promise<AnalyticsOverview> => {
    const response = await api.get(`/analytics/overview?period=${period}`);
    return response.data;
  },

  getTopSellers: async (period = '30d'): Promise<TopSeller[]> => {
    const response = await api.get(`/analytics/top-sellers?period=${period}`);
    return response.data;
  },

  getInventoryAlerts: async (): Promise<InventoryAlert[]> => {
    const response = await api.get('/analytics/inventory-alerts');
    return response.data;
  },

  getRevenueChart: async (period = '6m', group = 'month'): Promise<RevenueDataPoint[]> => {
    const response = await api.get(`/analytics/revenue-chart?period=${period}&group=${group}`);
    return response.data;
  },

  getTypeBreakdown: async (period = '30d'): Promise<TypeBreakdown[]> => {
    const response = await api.get(`/analytics/type-breakdown?period=${period}`);
    return response.data;
  },
};

export default api;