<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class ProductUpgradePathSyncRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('update products') ?? false;
    }

    public function rules(): array
    {
        return [
            'target_product_ids'            => ['array'],
            'target_product_ids.*'          => ['integer', 'exists:products,id'],
            'downgrade_credit_mode'         => ['required', 'in:balance_only,gateway_first,none'],
            'same_product_downgrade_refund' => ['boolean'],
            'refund_dropped_options'        => ['boolean'],
            'upgrades_enabled'              => ['boolean'],
        ];
    }

    public function prepareForValidation(): void
    {
        $this->merge([
            'same_product_downgrade_refund' => $this->boolean('same_product_downgrade_refund'),
            'refund_dropped_options'        => $this->boolean('refund_dropped_options'),
            'upgrades_enabled'              => $this->boolean('upgrades_enabled'),
            'target_product_ids'            => $this->input('target_product_ids', []),
        ]);
    }
}
