<?php

namespace App\Http\Requests;

use App\Enums\VideoStatus;
use App\Repositories\VideoRepository;
use App\Support\IdFormatter;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class VideoUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        $organization = $this->attributes->get('current_organization');
        $videoId = $this->route('id');
        $orgHex = IdFormatter::formatModelId($organization);

        $video = (new VideoRepository())->getByIdInOrganization($videoId, $orgHex);

        if (!$video) {
            return false;
        }

        return $this->user()->can('update', [$video, $organization]);
    }

    public function rules(): array
    {
        return [
            'url' => ['nullable', 'string', 'max:2048'],
            'status' => ['nullable', 'string', Rule::in(VideoStatus::values())],
            'spark_ads' => ['nullable', 'string', 'max:2048'],
        ];
    }
}
