/**
 * Inventory Dashboard Service - Mirror backend dashboard endpoints
 */
import { apiClient } from '../client';
import {
  InventoryDashboardSummary,
  InventoryAnalytics,
  BusinessModelInsights,
  DashboardFilter,
  AlertsFilter,
  RecentActivity,
} from '../types/dashboard';

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

  async getDashboardSummary(
    tenantId: string,
    filter?: DashboardFilter
  ): Promise<InventoryDashboardSummary> {
    const queryParams = new URLSearchParams();
    
    if (filter?.date_range?.start) queryParams.append('start_date', filter.date_range.start);
    if (filter?.date_range?.end) queryParams.append('end_date', filter.date_range.end);
    if (filter?.categories?.length) queryParams.append('categories', filter.categories.join(','));
    if (filter?.include_expired !== undefined) 
      queryParams.append('include_expired', filter.include_expired.toString());
    if (filter?.include_unavailable !== undefined) 
      queryParams.append('include_unavailable', filter.include_unavailable.toString());

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

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

  async getInventoryAnalytics(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<InventoryAnalytics> {
    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}/dashboard/analytics?${queryParams.toString()}`
      : `${this.baseUrl}/${tenantId}/dashboard/analytics`;

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

  async getBusinessModelInsights(tenantId: string): Promise<BusinessModelInsights> {
    return apiClient.get<BusinessModelInsights>(
      `${this.baseUrl}/${tenantId}/dashboard/business-insights`
    );
  }

  async getRecentActivity(
    tenantId: string,
    limit: number = 20
  ): Promise<RecentActivity[]> {
    const queryParams = new URLSearchParams();
    queryParams.append('limit', limit.toString());

    return apiClient.get<RecentActivity[]>(
      `${this.baseUrl}/${tenantId}/dashboard/recent-activity?${queryParams.toString()}`
    );
  }

  async getAlerts(
    tenantId: string,
    filter?: AlertsFilter
  ): Promise<{
    items: any[];
    total: number;
  }> {
    const queryParams = new URLSearchParams();
    
    if (filter?.severity) queryParams.append('severity', filter.severity);
    if (filter?.type) queryParams.append('type', filter.type);
    if (filter?.resolved !== undefined) queryParams.append('resolved', filter.resolved.toString());
    if (filter?.limit !== undefined) queryParams.append('limit', filter.limit.toString());
    if (filter?.offset !== undefined) queryParams.append('offset', filter.offset.toString());

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

    return apiClient.get(url);
  }

  async getStockSummary(tenantId: string): Promise<{
    in_stock: number;
    low_stock: number;
    out_of_stock: number;
    overstock: number;
    total_value: number;
  }> {
    return apiClient.get(`${this.baseUrl}/${tenantId}/dashboard/stock-summary`);
  }

  async getTopCategories(tenantId: string, limit: number = 10): Promise<Array<{
    category: string;
    ingredient_count: number;
    total_value: number;
    low_stock_count: number;
  }>> {
    const queryParams = new URLSearchParams();
    queryParams.append('limit', limit.toString());

    return apiClient.get(
      `${this.baseUrl}/${tenantId}/dashboard/top-categories?${queryParams.toString()}`
    );
  }

  async getExpiryCalendar(
    tenantId: string,
    daysAhead: number = 30
  ): Promise<Array<{
    date: string;
    items: Array<{
      ingredient_name: string;
      quantity: number;
      batch_number?: string;
    }>;
  }>> {
    const queryParams = new URLSearchParams();
    queryParams.append('days_ahead', daysAhead.toString());

    return apiClient.get(
      `${this.baseUrl}/${tenantId}/dashboard/expiry-calendar?${queryParams.toString()}`
    );
  }
}

export const inventoryDashboardService = new InventoryDashboardService();