import {
  Category,
  MeatCustomization,
  ProductCustomization,
} from '@/domain/entities';

export type Translator = (
  path: string,
  params?: Record<string, string | number>
) => string;

/**
 * DOMAIN SERVICE — Product customization rules
 */
export class CustomizationDomainService {
  static getDefaultCustomization(category: Category): ProductCustomization {
    if (category === 'fish') {
      return { type: 'fish' };
    }
    return {
      type: category,
      cuts: 8,
      cuttingStyle: 'karahi',
    };
  }

  static getCustomizationKey(customization: ProductCustomization): string {
    if (customization.type === 'fish') {
      return 'standard';
    }
    return `cuts-${customization.cuts}::${customization.cuttingStyle}`;
  }

  static getCustomizationLabel(
    customization: ProductCustomization,
    t: Translator
  ): string {
    if (customization.type === 'fish') {
      return t('product.standardCut');
    }
    return t('product.cutsAndStyle', {
      cuts: customization.cuts,
      style: t(`cutting.${customization.cuttingStyle}`),
    });
  }

  static getCartItemKey(
    productId: string,
    customization: ProductCustomization
  ): string {
    return `${productId}::${CustomizationDomainService.getCustomizationKey(customization)}`;
  }

  static updateMeatCustomization(
    current: ProductCustomization,
    category: Category,
    update: Partial<Pick<MeatCustomization, 'cuts' | 'cuttingStyle'>>
  ): MeatCustomization {
    const base =
      current.type !== 'fish' && current.type === category
        ? current
        : (CustomizationDomainService.getDefaultCustomization(
            category
          ) as MeatCustomization);

    return {
      type: category as MeatCustomization['type'],
      cuts: update.cuts ?? base.cuts,
      cuttingStyle: update.cuttingStyle ?? base.cuttingStyle,
    };
  }
}