<?php

namespace Database\Seeders;

use App\Domain\Commissions\Models\CommissionRule;
use App\Domain\Organization\Models\Company;
use Illuminate\Database\Seeder;

/**
 * The four schemes from §13A, as a starting point an agency can edit or retire.
 *
 * Amounts are minor units and rates are basis points, so nothing here is a
 * float (§112). All four are inactive on install: commission should not start
 * accruing until someone has read the numbers and agreed to them.
 */
class CommissionRuleSeeder extends Seeder
{
    /** @var array<int, array<string, mixed>> */
    private const RULES = [
        [
            'code' => 'registration-bonus',
            'name' => 'Registration bonus',
            'description' => 'BDT 500 once an applicant profile has been verified by staff.',
            'trigger_event' => 'applicant_verified',
            'calculation_basis' => 'fixed',
            'fixed_amount' => 50000,
            'priority' => 100,
        ],
        [
            'code' => 'visa-success',
            'name' => 'Visa success commission',
            'description' => 'BDT 2,500 once a visa is approved.',
            'trigger_event' => 'visa_approved',
            'calculation_basis' => 'fixed',
            'fixed_amount' => 250000,
            'priority' => 100,
        ],
        [
            'code' => 'deployment',
            'name' => 'Deployment commission',
            'description' => 'BDT 5,000 once the worker has joined the employer.',
            'trigger_event' => 'deployment_completed',
            'calculation_basis' => 'fixed',
            'fixed_amount' => 500000,
            'priority' => 100,
        ],
        [
            'code' => 'collection',
            'name' => 'Collection commission',
            'description' => '2% of each confirmed applicant payment.',
            'trigger_event' => 'payment_confirmed',
            'calculation_basis' => 'percent_of_collection',
            'rate_basis_points' => 200,
            'priority' => 100,
        ],
    ];

    public function run(): void
    {
        $company = Company::query()->first();

        if ($company === null) {
            return;
        }

        $currency = config('mams.money.base_currency');

        foreach (self::RULES as $rule) {
            CommissionRule::firstOrCreate(
                ['company_id' => $company->getKey(), 'code' => $rule['code']],
                array_merge([
                    'currency' => $currency,
                    'fixed_amount' => 0,
                    'rate_basis_points' => 0,
                    // Off until an agency confirms the numbers are theirs.
                    'is_active' => false,
                ], $rule),
            );
        }
    }
}
