// ================================================================
// frontend/src/api/services/production.ts
// ================================================================
/**
 * Production Service - Complete backend alignment
 *
 * Backend API structure (3-tier architecture):
 * - ATOMIC: production_batches.py, production_schedules.py
 * - OPERATIONS: production_operations.py (batch lifecycle, capacity management)
 * - ANALYTICS: analytics.py, production_dashboard.py
 *
 * Last Updated: 2025-10-05
 * Status: ✅ Complete - Zero drift with backend
 */

import { apiClient } from '../client/apiClient';
import {
  // Batches
  ProductionBatchResponse,
  ProductionBatchCreate,
  ProductionBatchUpdate,
  ProductionBatchStatusUpdate,
  ProductionBatchListResponse,
  ProductionBatchFilters,
  BatchStatistics,
  // Schedules
  ProductionScheduleResponse,
  ProductionScheduleCreate,
  ProductionScheduleUpdate,
  ProductionScheduleFilters,
  // Capacity
  ProductionCapacityResponse,
  ProductionCapacityFilters,
  // Quality
  QualityCheckResponse,
  QualityCheckCreate,
  QualityCheckFilters,
  // Analytics
  ProductionPerformanceAnalytics,
  YieldTrendsAnalytics,
  TopDefectsAnalytics,
  EquipmentEfficiencyAnalytics,
  CapacityBottlenecks,
  // Dashboard
  ProductionDashboardSummary,
} from '../types/production';

export class ProductionService {
  private baseUrl = '/tenants';

  // ===================================================================
  // ATOMIC: Production Batches CRUD
  // Backend: services/production/app/api/production_batches.py
  // ===================================================================

  async getBatches(
    tenantId: string,
    filters?: ProductionBatchFilters
  ): Promise<ProductionBatchListResponse> {
    const params = new URLSearchParams();
    if (filters?.status) params.append('status', filters.status);
    if (filters?.product_id) params.append('product_id', filters.product_id);
    if (filters?.order_id) params.append('order_id', filters.order_id);
    if (filters?.start_date) params.append('start_date', filters.start_date);
    if (filters?.end_date) params.append('end_date', filters.end_date);
    if (filters?.page) params.append('page', filters.page.toString());
    if (filters?.page_size) params.append('page_size', filters.page_size.toString());

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/batches${queryString ? `?${queryString}` : ''}`;

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

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

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

  async updateBatch(
    tenantId: string,
    batchId: string,
    batchData: ProductionBatchUpdate
  ): Promise<ProductionBatchResponse> {
    return apiClient.put<ProductionBatchResponse>(
      `${this.baseUrl}/${tenantId}/production/batches/${batchId}`,
      batchData
    );
  }

  async deleteBatch(tenantId: string, batchId: string): Promise<void> {
    return apiClient.delete<void>(`${this.baseUrl}/${tenantId}/production/batches/${batchId}`);
  }

  async getBatchStatistics(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<BatchStatistics> {
    const params = new URLSearchParams();
    if (startDate) params.append('start_date', startDate);
    if (endDate) params.append('end_date', endDate);

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/batches/stats${queryString ? `?${queryString}` : ''}`;

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


  // ===================================================================
  // ATOMIC: Production Schedules CRUD
  // Backend: services/production/app/api/production_schedules.py
  // ===================================================================

  async getSchedules(
    tenantId: string,
    filters?: ProductionScheduleFilters
  ): Promise<{ schedules: ProductionScheduleResponse[]; total_count: number; page: number; page_size: number }> {
    const params = new URLSearchParams();
    if (filters?.start_date) params.append('start_date', filters.start_date);
    if (filters?.end_date) params.append('end_date', filters.end_date);
    if (filters?.is_finalized !== undefined)
      params.append('is_finalized', filters.is_finalized.toString());
    if (filters?.page) params.append('page', filters.page.toString());
    if (filters?.page_size) params.append('page_size', filters.page_size.toString());

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/schedules${queryString ? `?${queryString}` : ''}`;

    return apiClient.get(url);
  }

  async getSchedule(tenantId: string, scheduleId: string): Promise<ProductionScheduleResponse> {
    return apiClient.get<ProductionScheduleResponse>(
      `${this.baseUrl}/${tenantId}/production/schedules/${scheduleId}`
    );
  }

  async createSchedule(
    tenantId: string,
    scheduleData: ProductionScheduleCreate
  ): Promise<ProductionScheduleResponse> {
    return apiClient.post<ProductionScheduleResponse>(
      `${this.baseUrl}/${tenantId}/production/schedules`,
      scheduleData
    );
  }

  async updateSchedule(
    tenantId: string,
    scheduleId: string,
    scheduleData: ProductionScheduleUpdate
  ): Promise<ProductionScheduleResponse> {
    return apiClient.put<ProductionScheduleResponse>(
      `${this.baseUrl}/${tenantId}/production/schedules/${scheduleId}`,
      scheduleData
    );
  }

  async deleteSchedule(tenantId: string, scheduleId: string): Promise<void> {
    return apiClient.delete<void>(`${this.baseUrl}/${tenantId}/production/schedules/${scheduleId}`);
  }

  async getTodaysSchedule(tenantId: string): Promise<ProductionScheduleResponse | null> {
    return apiClient.get<ProductionScheduleResponse | null>(
      `${this.baseUrl}/${tenantId}/production/schedules/today`
    );
  }

  // ===================================================================
  // OPERATIONS: Batch Lifecycle Management
  // Backend: services/production/app/api/production_operations.py
  // ===================================================================

  async updateBatchStatus(
    tenantId: string,
    batchId: string,
    statusData: ProductionBatchStatusUpdate
  ): Promise<ProductionBatchResponse> {
    return apiClient.patch<ProductionBatchResponse>(
      `${this.baseUrl}/${tenantId}/production/batches/${batchId}/status`,
      statusData
    );
  }

  async startBatch(tenantId: string, batchId: string): Promise<ProductionBatchResponse> {
    return apiClient.post<ProductionBatchResponse>(
      `${this.baseUrl}/${tenantId}/production/batches/${batchId}/start`
    );
  }

  async completeBatch(
    tenantId: string,
    batchId: string,
    completionData?: { actual_quantity?: number; notes?: string }
  ): Promise<ProductionBatchResponse> {
    return apiClient.post<ProductionBatchResponse>(
      `${this.baseUrl}/${tenantId}/production/batches/${batchId}/complete`,
      completionData || {}
    );
  }

  async finalizeSchedule(tenantId: string, scheduleId: string): Promise<ProductionScheduleResponse> {
    return apiClient.post<ProductionScheduleResponse>(
      `${this.baseUrl}/${tenantId}/production/schedules/${scheduleId}/finalize`
    );
  }

  // ===================================================================
  // OPERATIONS: Capacity Management
  // Backend: services/production/app/api/production_operations.py
  // ===================================================================

  async getCapacity(
    tenantId: string,
    filters?: ProductionCapacityFilters
  ): Promise<{ capacity: ProductionCapacityResponse[]; total_count: number; page: number; page_size: number }> {
    const params = new URLSearchParams();
    if (filters?.resource_type) params.append('resource_type', filters.resource_type);
    if (filters?.date) params.append('date', filters.date);
    if (filters?.availability !== undefined)
      params.append('availability', filters.availability.toString());
    if (filters?.page) params.append('page', filters.page.toString());
    if (filters?.page_size) params.append('page_size', filters.page_size.toString());

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/capacity${queryString ? `?${queryString}` : ''}`;

    return apiClient.get(url);
  }

  async getCapacityByDate(tenantId: string, date: string): Promise<ProductionCapacityResponse[]> {
    return apiClient.get<ProductionCapacityResponse[]>(
      `${this.baseUrl}/${tenantId}/production/capacity/date/${date}`
    );
  }

  async getCapacityByResource(
    tenantId: string,
    resourceId: string
  ): Promise<ProductionCapacityResponse[]> {
    return apiClient.get<ProductionCapacityResponse[]>(
      `${this.baseUrl}/${tenantId}/production/capacity/resource/${resourceId}`
    );
  }

  // ===================================================================
  // OPERATIONS: Quality Checks
  // Backend: services/production/app/api/production_operations.py
  // ===================================================================

  async getQualityChecks(
    tenantId: string,
    filters?: QualityCheckFilters
  ): Promise<{ quality_checks: QualityCheckResponse[]; total_count: number; page: number; page_size: number }> {
    const params = new URLSearchParams();
    if (filters?.batch_id) params.append('batch_id', filters.batch_id);
    if (filters?.product_id) params.append('product_id', filters.product_id);
    if (filters?.start_date) params.append('start_date', filters.start_date);
    if (filters?.end_date) params.append('end_date', filters.end_date);
    if (filters?.pass_fail !== undefined) params.append('pass_fail', filters.pass_fail.toString());
    if (filters?.page) params.append('page', filters.page.toString());
    if (filters?.page_size) params.append('page_size', filters.page_size.toString());

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/quality-checks${queryString ? `?${queryString}` : ''}`;

    return apiClient.get(url);
  }

  async getQualityCheck(tenantId: string, checkId: string): Promise<QualityCheckResponse> {
    return apiClient.get<QualityCheckResponse>(
      `${this.baseUrl}/${tenantId}/production/quality-checks/${checkId}`
    );
  }

  async createQualityCheck(
    tenantId: string,
    checkData: QualityCheckCreate
  ): Promise<QualityCheckResponse> {
    return apiClient.post<QualityCheckResponse>(
      `${this.baseUrl}/${tenantId}/production/quality-checks`,
      checkData
    );
  }

  async getQualityChecksByBatch(
    tenantId: string,
    batchId: string
  ): Promise<QualityCheckResponse[]> {
    return apiClient.get<QualityCheckResponse[]>(
      `${this.baseUrl}/${tenantId}/production/quality-checks/batch/${batchId}`
    );
  }

  // ===================================================================
  // ANALYTICS: Performance & Trends
  // Backend: services/production/app/api/analytics.py
  // ===================================================================

  async getPerformanceAnalytics(
    tenantId: string,
    startDate: string,
    endDate: string
  ): Promise<ProductionPerformanceAnalytics> {
    return apiClient.get<ProductionPerformanceAnalytics>(
      `${this.baseUrl}/${tenantId}/production/analytics/performance?start_date=${startDate}&end_date=${endDate}`
    );
  }

  async getYieldTrends(
    tenantId: string,
    period: 'week' | 'month' = 'week'
  ): Promise<YieldTrendsAnalytics> {
    return apiClient.get<YieldTrendsAnalytics>(
      `${this.baseUrl}/${tenantId}/production/analytics/yield-trends?period=${period}`
    );
  }

  async getTopDefects(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<TopDefectsAnalytics> {
    const params = new URLSearchParams();
    if (startDate) params.append('start_date', startDate);
    if (endDate) params.append('end_date', endDate);

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/analytics/defects${queryString ? `?${queryString}` : ''}`;

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

  async getEquipmentEfficiency(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<EquipmentEfficiencyAnalytics> {
    const params = new URLSearchParams();
    if (startDate) params.append('start_date', startDate);
    if (endDate) params.append('end_date', endDate);

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/analytics/equipment-efficiency${queryString ? `?${queryString}` : ''}`;

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

  async getCapacityBottlenecks(tenantId: string, days: number = 7): Promise<CapacityBottlenecks> {
    return apiClient.get<CapacityBottlenecks>(
      `${this.baseUrl}/${tenantId}/production/analytics/capacity-bottlenecks?days=${days}`
    );
  }

  // ===================================================================
  // ANALYTICS: Dashboard
  // Backend: services/production/app/api/production_dashboard.py
  // ===================================================================

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

  async getDailyProductionPlan(tenantId: string, date?: string): Promise<any> {
    const queryString = date ? `?date=${date}` : '';
    return apiClient.get(`${this.baseUrl}/${tenantId}/production/dashboard/daily-plan${queryString}`);
  }

  async getProductionRequirements(tenantId: string, date: string): Promise<any> {
    return apiClient.get(`${this.baseUrl}/${tenantId}/production/dashboard/requirements?date=${date}`);
  }

  async getCapacityOverview(tenantId: string, date?: string): Promise<any> {
    const queryString = date ? `?date=${date}` : '';
    return apiClient.get(
      `${this.baseUrl}/${tenantId}/production/dashboard/capacity-overview${queryString}`
    );
  }

  async getQualityOverview(
    tenantId: string,
    startDate?: string,
    endDate?: string
  ): Promise<any> {
    const params = new URLSearchParams();
    if (startDate) params.append('start_date', startDate);
    if (endDate) params.append('end_date', endDate);

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/production/dashboard/quality-overview${queryString ? `?${queryString}` : ''}`;

    return apiClient.get(url);
  }

  // ===================================================================
  // ANALYTICS: Batch Production Summary (Enterprise Feature)
  // Backend: services/production/app/api/analytics.py
  // ===================================================================

  async getBatchProductionSummary(tenantIds: string[]): Promise<Record<string, any>> {
    return apiClient.post<Record<string, any>>(
      '/tenants/batch/production-summary',
      {
        tenant_ids: tenantIds,
      }
    );
  }

  // ===================================================================
  // OPERATIONS: Scheduler
  // ===================================================================

  /**
   * Trigger production scheduler manually (for testing/development)
   * POST /tenants/{tenant_id}/production/operations/scheduler/trigger
   */
  static async triggerProductionScheduler(tenantId: string): Promise<{
    success: boolean;
    message: string;
    tenant_id: string
  }> {
    return apiClient.post(
      `/tenants/${tenantId}/production/operations/scheduler/trigger`,
      {}
    );
  }
}

export const productionService = new ProductionService();
export default productionService;
