<?php

declare(strict_types=1);

namespace App\Http\Requests\Academy;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpsertAcademyCourseRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->hasStaffAccess();
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_featured' => $this->boolean('is_featured'),
            'order_num' => $this->filled('order_num') ? (int) $this->input('order_num') : 0,
            'estimated_minutes' => $this->filled('estimated_minutes') ? (int) $this->input('estimated_minutes') : null,
        ]);
    }

    public function rules(): array
    {
        $courseId = $this->route('academyCourse')?->id;

        return [
            'title' => ['required', 'string', 'max:180'],
            'slug' => ['required', 'string', 'max:180', Rule::unique('academy_courses', 'slug')->ignore($courseId)],
            'subtitle' => ['nullable', 'string', 'max:255'],
            'excerpt' => ['nullable', 'string'],
            'description' => ['nullable', 'string'],
            'cover_image' => ['nullable', 'string', 'max:2048'],
            'teaser_image' => ['nullable', 'string', 'max:2048'],
            'access_level' => ['required', 'string', Rule::in(['free', 'premium', 'mixed'])],
            'difficulty' => ['required', 'string', Rule::in(['beginner', 'intermediate', 'advanced'])],
            'status' => ['required', 'string', Rule::in(['draft', 'review', 'published', 'archived'])],
            'is_featured' => ['required', 'boolean'],
            'order_num' => ['required', 'integer', 'min:0'],
            'estimated_minutes' => ['nullable', 'integer', 'min:1', 'max:10000'],
            'published_at' => ['nullable', 'date'],
            'seo_title' => ['nullable', 'string', 'max:180'],
            'seo_description' => ['nullable', 'string', 'max:255'],
            'meta_keywords' => ['nullable', 'string'],
            'og_title' => ['nullable', 'string', 'max:180'],
            'og_description' => ['nullable', 'string', 'max:255'],
            'og_image' => ['nullable', 'string', 'max:2048'],
        ];
    }
}