<?php

declare(strict_types=1);

namespace App\Http\Requests\Artworks;

use Illuminate\Foundation\Http\FormRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class ArtworkCreateRequest extends FormRequest
{
    public function authorize(): bool
    {
        if (! $this->user()) {
            $this->logUnauthorized('missing_user');
            $this->denyAsNotFound();
        }

        return true;
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:150',
            'description' => 'nullable|string',
            'category' => 'nullable|integer|exists:categories,id',
            'tags' => 'nullable|string|max:200',
            'license' => 'nullable|boolean',
            'is_mature' => 'nullable|boolean',
            'group' => 'nullable|string|max:90',
        ];
    }

    private function denyAsNotFound(): void
    {
        throw new NotFoundHttpException();
    }

    private function logUnauthorized(string $reason): void
    {
        logger()->warning('Artwork create unauthorized access', [
            'reason' => $reason,
            'user_id' => $this->user()?->id,
            'ip' => $this->ip(),
        ]);
    }
}
