import { apiClient } from '../client';
import type {
  ProductionBatchCreate,
  ProductionBatchStatusUpdate,
  ProductionBatchResponse,
  ProductionBatchListResponse,
  ProductionDashboardSummary,
  DailyProductionRequirements,
  ProductionScheduleData,
  ProductionCapacityStatus,
  ProductionRequirements,
  ProductionYieldMetrics,
} from '../types/production';

export class ProductionService {
  private readonly baseUrl = '/production';

  getDashboardSummary(tenantId: string): Promise<ProductionDashboardSummary> {
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/dashboard-summary`);
  }

  getDailyRequirements(tenantId: string, date?: string): Promise<DailyProductionRequirements> {
    const params = date ? { date } : {};
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/daily-requirements`, { params });
  }

  getProductionRequirements(tenantId: string, date?: string): Promise<ProductionRequirements> {
    const params = date ? { date } : {};
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/requirements`, { params });
  }

  createProductionBatch(tenantId: string, batchData: ProductionBatchCreate): Promise<ProductionBatchResponse> {
    return apiClient.post(`/tenants/${tenantId}${this.baseUrl}/batches`, batchData);
  }

  getActiveBatches(tenantId: string): Promise<ProductionBatchListResponse> {
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/batches/active`);
  }

  getBatchDetails(tenantId: string, batchId: string): Promise<ProductionBatchResponse> {
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}`);
  }

  updateBatchStatus(
    tenantId: string,
    batchId: string,
    statusUpdate: ProductionBatchStatusUpdate
  ): Promise<ProductionBatchResponse> {
    return apiClient.put(`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}/status`, statusUpdate);
  }

  getProductionSchedule(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<ProductionScheduleData> {
    const params: Record<string, string> = {};
    if (startDate) params.start_date = startDate;
    if (endDate) params.end_date = endDate;
    
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/schedule`, { params });
  }

  getCapacityStatus(tenantId: string, date?: string): Promise<ProductionCapacityStatus> {
    const params = date ? { date } : {};
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/capacity/status`, { params });
  }

  getYieldMetrics(
    tenantId: string,
    startDate: string,
    endDate: string
  ): Promise<ProductionYieldMetrics> {
    const params = { start_date: startDate, end_date: endDate };
    return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/metrics/yield`, { params });
  }
}

export const productionService = new ProductionService();