<?php

namespace App\Actions\Promo;

use App\DTOs\AutoPromoData;
use App\Models\PwPromo;

final class GetAutoPromoAction
{
    public function handle(int $amount, int $userId, int $goldRate = 1000): ?AutoPromoData
    {
        $promo = PwPromo::auto()
            ->active()
            ->minAmount($amount)
            ->notUsedBy($userId)
            ->withinPeriod()
            ->orderByDesc('min_amount')
            ->first();

        if (! $promo) {
            return null;
        }

        $discountInfo = $promo->calculateDiscount($amount, $goldRate);

        return new AutoPromoData(promo: $promo, bonusGold: $discountInfo['bonus_gold']);
    }
}
