// frontend/src/api/services/qualityTemplates.ts
/**
 * Quality Check Template API service
 */

import { apiClient } from '../client';
import type {
  QualityCheckTemplate,
  QualityCheckTemplateCreate,
  QualityCheckTemplateUpdate,
  QualityCheckTemplateList,
  QualityTemplateQueryParams,
  ProcessStage,
  QualityCheckExecutionRequest,
  QualityCheckExecutionResponse
} from '../types/qualityTemplates';

class QualityTemplateService {
  private readonly baseURL = '/tenants';

  /**
   * Create a new quality check template
   */
  async createTemplate(
    tenantId: string,
    templateData: QualityCheckTemplateCreate
  ): Promise<QualityCheckTemplate> {
    const data = await apiClient.post(`${this.baseURL}/${tenantId}/production/quality-templates`, templateData, {
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Get quality check templates with filtering and pagination
   */
  async getTemplates(
    tenantId: string,
    params?: QualityTemplateQueryParams
  ): Promise<QualityCheckTemplateList> {
    const data = await apiClient.get(`${this.baseURL}/${tenantId}/production/quality-templates`, {
      params,
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Get a specific quality check template
   */
  async getTemplate(
    tenantId: string,
    templateId: string
  ): Promise<QualityCheckTemplate> {
    const data = await apiClient.get(`${this.baseURL}/${tenantId}/production/quality-templates/${templateId}`, {
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Update a quality check template
   */
  async updateTemplate(
    tenantId: string,
    templateId: string,
    templateData: QualityCheckTemplateUpdate
  ): Promise<QualityCheckTemplate> {
    const data = await apiClient.put(`${this.baseURL}/${tenantId}/production/quality-templates/${templateId}`, templateData, {
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Delete a quality check template
   */
  async deleteTemplate(tenantId: string, templateId: string): Promise<void> {
    await apiClient.delete(`${this.baseURL}/${tenantId}/production/quality-templates/${templateId}`, {
      headers: { 'X-Tenant-ID': tenantId }
    });
  }

  /**
   * Get templates applicable to a specific process stage
   */
  async getTemplatesForStage(
    tenantId: string,
    stage: ProcessStage,
    isActive: boolean = true
  ): Promise<QualityCheckTemplateList> {
    const data = await apiClient.get(`${this.baseURL}/${tenantId}/production/quality-templates/stages/${stage}`, {
      params: { is_active: isActive },
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Duplicate an existing quality check template
   */
  async duplicateTemplate(
    tenantId: string,
    templateId: string
  ): Promise<QualityCheckTemplate> {
    const data = await apiClient.post(`${this.baseURL}/${tenantId}/production/quality-templates/${templateId}/duplicate`, {}, {
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Execute a quality check using a template
   */
  async executeQualityCheck(
    tenantId: string,
    executionData: QualityCheckExecutionRequest
  ): Promise<QualityCheckExecutionResponse> {
    const data = await apiClient.post(`${this.baseURL}/${tenantId}/production/quality-checks/execute`, executionData, {
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Get quality check history for a batch
   */
  async getQualityCheckHistory(
    tenantId: string,
    batchId: string,
    stage?: ProcessStage
  ): Promise<any[]> {
    const data = await apiClient.get(`${this.baseURL}/${tenantId}/production/quality-checks`, {
      params: { batch_id: batchId, process_stage: stage },
      headers: { 'X-Tenant-ID': tenantId }
    });
    return data;
  }

  /**
   * Get quality check templates for recipe configuration
   */
  async getTemplatesForRecipe(
    tenantId: string,
    recipeId: string
  ): Promise<Record<ProcessStage, QualityCheckTemplate[]>> {
    const allTemplates = await this.getTemplates(tenantId, { is_active: true });

    // Group templates by applicable stages
    const templatesByStage: Record<ProcessStage, QualityCheckTemplate[]> = {} as any;

    Object.values(ProcessStage).forEach(stage => {
      templatesByStage[stage] = allTemplates.templates.filter(template =>
        !template.applicable_stages ||
        template.applicable_stages.length === 0 ||
        template.applicable_stages.includes(stage)
      );
    });

    return templatesByStage;
  }

  /**
   * Validate template configuration
   */
  async validateTemplate(
    tenantId: string,
    templateData: Partial<QualityCheckTemplateCreate | QualityCheckTemplateUpdate>
  ): Promise<{ valid: boolean; errors: string[] }> {
    try {
      const data = await apiClient.post(`${this.baseURL}/${tenantId}/production/quality-templates/validate`, templateData, {
        headers: { 'X-Tenant-ID': tenantId }
      });
      return data;
    } catch (error: any) {
      if (error.response?.status === 400) {
        return {
          valid: false,
          errors: [error.response?.data?.detail || 'Validation failed']
        };
      }
      throw error;
    }
  }

  /**
   * Get default template suggestions based on product type
   */
  async getDefaultTemplates(
    tenantId: string,
    productCategory: string
  ): Promise<QualityCheckTemplate[]> {
    const templates = await this.getTemplates(tenantId, {
      is_active: true,
      category: productCategory
    });

    // Return commonly used templates for the product category
    return templates.templates.filter(template =>
      template.is_required || template.weight > 5.0
    ).sort((a, b) => b.weight - a.weight);
  }
}

export const qualityTemplateService = new QualityTemplateService();