<?php

use App\Domain\Shared\Models\SystemSetting;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

/**
 * Pricing guardrails, per academy.
 *
 * The global discount ceiling was a hardcoded constant with a TODO; the manual
 * discount caps replace the unbounded super-admin price override.
 */
return new class extends Migration
{
    private const SETTINGS = [
        ['pricing_max_discount_percent', '50',  'الحد الأقصى لإجمالي الخصم على الفاتورة (%)'],
        ['pricing_manual_cap_staff',     '10',  'حد الخصم اليدوي — موظف الاستقبال (%)'],
        ['pricing_manual_cap_manager',   '25',  'حد الخصم اليدوي — مدير الفرع (%)'],
        ['pricing_manual_cap_owner',     '100', 'حد الخصم اليدوي — مالك الأكاديمية (%)'],
    ];

    public function up(): void
    {
        $academies = DB::table('academies')->pluck('id');

        foreach ($academies as $academyId) {
            foreach (self::SETTINGS as [$key, $value, $label]) {
                DB::table('system_settings')->updateOrInsert(
                    ['academy_id' => $academyId, 'key' => $key],
                    [
                        'value'      => $value,
                        'group'      => 'pricing',
                        'type'       => 'integer',
                        'label_ar'   => $label,
                        'is_public'  => false,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]
                );
            }
        }
    }

    public function down(): void
    {
        DB::table('system_settings')
            ->whereIn('key', array_column(self::SETTINGS, 0))
            ->delete();
    }
};
