<?php

namespace App\Domain\Pricing\Support;

/**
 * The twelve discounts a sports academy actually gives.
 *
 * Icons are icon NAMES resolved by <x-ui.icon>, never emoji.
 * A recipe pre-fills the builder — rule type, conditions, adjustment, stacking,
 * pin — so authoring a sibling discount is "change two numbers", not five steps
 * of database columns. Recipes are templates, not rows: once created the rule is
 * an ordinary pricing_rule that carries `recipe_key` for reporting.
 */
final class DiscountRecipe
{
    public const RECIPES = [
        'sibling' => [
            'icon' => 'user-group',
            'name_ar' => 'خصم الإخوة',
            'hint_ar' => 'الابن الثاني فأكثر',
            'rule_type' => 'sibling_order',
            'adjustment_type' => 'percentage_discount',
            'percent' => 20,
            'conditions' => ['min' => 2],
            'is_stackable' => false,
            'is_pinned' => true,
        ],
        'early_bird' => [
            'icon' => 'clock',
            'name_ar' => 'الحجز المبكر',
            'hint_ar' => 'قبل بداية الموسم',
            'rule_type' => 'enrollment_timing',
            'adjustment_type' => 'percentage_discount',
            'percent' => 10,
            'conditions' => ['days_before_start' => 30],
            'is_stackable' => false,
            'is_pinned' => true,
        ],
        'scholarship' => [
            'icon' => 'academic-cap',
            'name_ar' => 'منحة دراسية',
            'hint_ar' => 'حسب التصنيف',
            'rule_type' => 'classification',
            'adjustment_type' => 'percentage_discount',
            'percent' => 50,
            'conditions' => ['values' => ['scholarship']],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'extra_program' => [
            'icon' => 'squares-plus',
            'name_ar' => 'برنامج إضافي',
            'hint_ar' => 'من البرنامج الثاني',
            'rule_type' => 'enrollment_volume',
            'adjustment_type' => 'percentage_discount',
            'percent' => 10,
            'conditions' => ['min' => 2],
            'is_stackable' => false,
            'is_pinned' => true,
        ],
        'morning_slot' => [
            'icon' => 'sun',
            'name_ar' => 'مواعيد الصباح',
            'hint_ar' => 'قبل الساعة ٣',
            'rule_type' => 'day_time',
            'adjustment_type' => 'percentage_discount',
            'percent' => 15,
            'conditions' => ['before_hour' => 15],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'annual' => [
            'icon' => 'calendar-days',
            'name_ar' => 'اشتراك سنوي',
            'hint_ar' => 'دفعة واحدة',
            'rule_type' => 'membership_duration',
            'adjustment_type' => 'percentage_discount',
            'percent' => 15,
            'conditions' => ['min' => 12],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'staff_child' => [
            'icon' => 'briefcase',
            'name_ar' => 'أبناء العاملين',
            'hint_ar' => 'يُجمع مع غيره',
            'rule_type' => 'classification',
            'adjustment_type' => 'percentage_discount',
            'percent' => 50,
            'conditions' => ['values' => ['staff_child']],
            'is_stackable' => true,
            'is_pinned' => false,
        ],
        'loyalty' => [
            'icon' => 'shield-check',
            'name_ar' => 'عضو قديم',
            'hint_ar' => 'بعد ١٢ شهر',
            'rule_type' => 'loyalty',
            'adjustment_type' => 'percentage_discount',
            'percent' => 10,
            'conditions' => ['min' => 12],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'season_end' => [
            'icon' => 'flag',
            'name_ar' => 'نهاية الموسم',
            'hint_ar' => 'شهور محددة',
            'rule_type' => 'seasonal',
            'adjustment_type' => 'percentage_discount',
            'percent' => 25,
            'conditions' => ['months' => [6, 7, 8]],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'birthday' => [
            'icon' => 'cake',
            'name_ar' => 'عرض عيد الميلاد',
            'hint_ar' => 'شهر الميلاد',
            'rule_type' => 'seasonal',
            'adjustment_type' => 'percentage_discount',
            'percent' => 10,
            'conditions' => ['months' => [1]],
            'is_stackable' => true,
            'is_pinned' => false,
        ],
        'juniors' => [
            'icon' => 'user',
            'name_ar' => 'خصم البراعم',
            'hint_ar' => 'أقل من ٧ سنوات',
            'rule_type' => 'age',
            'adjustment_type' => 'percentage_discount',
            'percent' => 10,
            'conditions' => ['max' => 6],
            'is_stackable' => false,
            'is_pinned' => false,
        ],
        'manual' => [
            'icon' => 'hand-raised',
            'name_ar' => 'خصم استثنائي',
            'hint_ar' => 'يدوي باعتماد',
            'rule_type' => 'custom',
            'adjustment_type' => 'percentage_discount',
            'percent' => 0,
            'conditions' => [],
            'is_stackable' => true,
            'is_pinned' => true,
            'is_manual' => true,
            'requires_approval' => true,
        ],
    ];

    /**
     * The recipes, with the branch's own vocabulary substituted in.
     *
     * RECIPES is a const, so the loyalty card cannot say "مقيم قديم" from the
     * array itself. Relabelling on the way out keeps the constant a constant
     * and still gives a compound the right word.
     */
    public static function all(): array
    {
        $recipes = self::RECIPES;

        $recipes['loyalty']['name_ar'] = term('member') . ' قديم';

        return $recipes;
    }

    public static function find(string $key): ?array
    {
        return self::all()[$key] ?? null;
    }

    /** Recipe defaults as builder field values (percent stays human here). */
    public static function prefill(string $key): array
    {
        $r = self::find($key);
        if (! $r) {
            return [];
        }

        return [
            'recipe_key'        => $key,
            'name_ar'           => $r['name_ar'],
            'rule_type'         => $r['rule_type'],
            'adjustment_type'   => $r['adjustment_type'],
            'percent'           => $r['percent'],
            'conditions'        => $r['conditions'],
            'is_stackable'      => $r['is_stackable'],
            'is_pinned'         => $r['is_pinned'],
            'pin_icon'          => $r['icon'],
            'is_manual'         => $r['is_manual'] ?? false,
            'requires_approval' => $r['requires_approval'] ?? false,
        ];
    }
}
