<?php

namespace App\Http\Requests\Admin;

use App\Models\Basket;
use Illuminate\Foundation\Http\FormRequest;

class BasketDestroyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        // For Redis baskets, check general basket delete permission
        if ($this->input('source') === 'redis') {
            return $this->user()->can('delete', Basket::class);
        }

        // For MySQL baskets, check specific basket permission
        $basket = Basket::find($this->route('id'));
        if ($basket) {
            return $this->user()->can('delete', $basket);
        }

        // If basket not found, allow and let controller handle 404
        return $this->user()->can('delete', Basket::class);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'source' => ['sometimes', 'string', 'in:redis,mysql'],
            'visitor_id' => ['sometimes', 'string'],
        ];
    }
}
