/**
 * Sales Service - Mirror backend sales endpoints
 */
import { apiClient } from '../client';
import {
  SalesDataCreate,
  SalesDataUpdate,
  SalesDataResponse,
  SalesDataQuery,
  SalesAnalytics,
} from '../types/sales';

export class SalesService {
  private readonly baseUrl = '/tenants';

  // Sales Data CRUD Operations
  async createSalesRecord(
    tenantId: string, 
    salesData: SalesDataCreate
  ): Promise<SalesDataResponse> {
    return apiClient.post<SalesDataResponse>(`${this.baseUrl}/${tenantId}/sales`, salesData);
  }

  async getSalesRecords(
    tenantId: string, 
    query?: SalesDataQuery
  ): Promise<SalesDataResponse[]> {
    const queryParams = new URLSearchParams();
    
    if (query?.start_date) queryParams.append('start_date', query.start_date);
    if (query?.end_date) queryParams.append('end_date', query.end_date);
    if (query?.product_name) queryParams.append('product_name', query.product_name);
    if (query?.product_category) queryParams.append('product_category', query.product_category);
    if (query?.location_id) queryParams.append('location_id', query.location_id);
    if (query?.sales_channel) queryParams.append('sales_channel', query.sales_channel);
    if (query?.source) queryParams.append('source', query.source);
    if (query?.is_validated !== undefined) queryParams.append('is_validated', query.is_validated.toString());
    if (query?.limit !== undefined) queryParams.append('limit', query.limit.toString());
    if (query?.offset !== undefined) queryParams.append('offset', query.offset.toString());
    if (query?.order_by) queryParams.append('order_by', query.order_by);
    if (query?.order_direction) queryParams.append('order_direction', query.order_direction);

    const url = queryParams.toString() 
      ? `${this.baseUrl}/${tenantId}/sales?${queryParams.toString()}`
      : `${this.baseUrl}/${tenantId}/sales`;

    return apiClient.get<SalesDataResponse[]>(url);
  }

  async getSalesRecord(
    tenantId: string, 
    recordId: string
  ): Promise<SalesDataResponse> {
    return apiClient.get<SalesDataResponse>(`${this.baseUrl}/${tenantId}/sales/${recordId}`);
  }

  async updateSalesRecord(
    tenantId: string, 
    recordId: string, 
    updateData: SalesDataUpdate
  ): Promise<SalesDataResponse> {
    return apiClient.put<SalesDataResponse>(`${this.baseUrl}/${tenantId}/sales/${recordId}`, updateData);
  }

  async deleteSalesRecord(
    tenantId: string, 
    recordId: string
  ): Promise<{ message: string }> {
    return apiClient.delete<{ message: string }>(`${this.baseUrl}/${tenantId}/sales/${recordId}`);
  }

  async validateSalesRecord(
    tenantId: string, 
    recordId: string, 
    validationNotes?: string
  ): Promise<SalesDataResponse> {
    const queryParams = new URLSearchParams();
    if (validationNotes) queryParams.append('validation_notes', validationNotes);

    const url = queryParams.toString() 
      ? `${this.baseUrl}/${tenantId}/sales/${recordId}/validate?${queryParams.toString()}`
      : `${this.baseUrl}/${tenantId}/sales/${recordId}/validate`;

    return apiClient.post<SalesDataResponse>(url);
  }

  // Analytics & Reporting
  async getSalesAnalytics(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<SalesAnalytics> {
    const queryParams = new URLSearchParams();
    if (startDate) queryParams.append('start_date', startDate);
    if (endDate) queryParams.append('end_date', endDate);

    const url = queryParams.toString() 
      ? `${this.baseUrl}/${tenantId}/sales/analytics/summary?${queryParams.toString()}`
      : `${this.baseUrl}/${tenantId}/sales/analytics/summary`;

    return apiClient.get<SalesAnalytics>(url);
  }

  async getProductSales(
    tenantId: string,
    inventoryProductId: string,
    startDate?: string,
    endDate?: string
  ): Promise<SalesDataResponse[]> {
    const queryParams = new URLSearchParams();
    if (startDate) queryParams.append('start_date', startDate);
    if (endDate) queryParams.append('end_date', endDate);

    const url = queryParams.toString() 
      ? `${this.baseUrl}/${tenantId}/inventory-products/${inventoryProductId}/sales?${queryParams.toString()}`
      : `${this.baseUrl}/${tenantId}/inventory-products/${inventoryProductId}/sales`;

    return apiClient.get<SalesDataResponse[]>(url);
  }

  async getProductCategories(tenantId: string): Promise<string[]> {
    return apiClient.get<string[]>(`${this.baseUrl}/${tenantId}/sales/categories`);
  }
}

export const salesService = new SalesService();