<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\NewCar;
use Mtc\MercuryDataModels\Page;
use Mtc\MercuryDataModels\Property;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleOffer;

class BulkDeleteRequest extends FormRequest
{
    public static array $allowedModels = [
        'vehicles' => Vehicle::class,
        'new-cars' => NewCar::class,
        'offers' => VehicleOffer::class,
        'dealerships' => Dealership::class,
        'properties' => Property::class,
        'pages' => Page::class,
    ];

    protected static array $modelPermissions = [
        'vehicles' => 'edit-vehicles',
        'new-cars' => 'edit-new-vehicles',
        'offers' => 'edit-offers',
        'dealerships' => 'edit-dealerships',
        'properties' => 'manage-properties',
        'pages' => 'edit-content',
    ];

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        $permission = self::$modelPermissions[$this->input('model')] ?? null;

        // Unknown models fall through to validation, which rejects them.
        return $permission === null || $this->user()?->can($permission) === true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'model' => 'required|string|in:' . implode(',', array_keys(self::$allowedModels)),
            'ids' => 'required|array',
            'ids.*' => 'integer',
        ];
    }

    public function getClass()
    {
        return self::$allowedModels[$this->input('model')];
    }
}
