<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class ImportBlogRequest extends FormRequest
{
    /**
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'file' => [
                'required',
                'file',
                'mimes:xml,csv,txt',
            ],
            'format' => [
                'required',
                'string',
                'in:wordpress,csv',
            ],
            'column_mapping' => [
                'required_if:format,csv',
                'array',
            ],
            'column_mapping.title' => [
                'required_if:format,csv',
                'string',
            ],
            'content_element_mapping' => [
                'sometimes',
                'array',
            ],
            'content_element_mapping.*' => [
                'array',
            ],
            'content_element_mapping.*.*' => [
                'string',
            ],
            'date_format' => [
                'sometimes',
                'nullable',
                'string',
                'max:32',
            ],
            'page_category' => [
                'sometimes',
                'nullable',
                'string',
                Rule::in(collect(config('pages.page_types', []))->pluck('value')->all()),
            ],
            'template_id' => [
                'required',
                'integer',
                'exists:page_templates,id',
            ],
        ];
    }
}
