<?php

namespace App\Http\Requests\V1\Popup;

use App\Models\Popups\Popup;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StorePopupRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return 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 [
            'target_type' => ['sometimes', 'string', Rule::in(array_keys(Popup::TARGET_TYPES))],
            'target_id' => ['nullable', 'integer', 'exists:departments,id'],
            'title' => ['required', 'string', 'max:200'],
            'content' => ['required', 'string'],
            'status' => ['sometimes', 'string', Rule::in(array_keys(Popup::STATUSES))],
            'started_at' => ['nullable', 'date'],
            'ended_at' => ['nullable', 'date', 'after_or_equal:started_at'],
            'options' => ['nullable', 'array'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'target_type.in' => __('validation.in', ['attribute' => '대상 유형']),
            'target_id.exists' => __('validation.exists', ['attribute' => '대상 부서']),
            'title.required' => __('validation.required', ['attribute' => '제목']),
            'title.max' => __('validation.max.string', ['attribute' => '제목', 'max' => 200]),
            'content.required' => __('validation.required', ['attribute' => '내용']),
            'status.in' => __('validation.in', ['attribute' => '상태']),
            'ended_at.after_or_equal' => __('validation.after_or_equal', ['attribute' => '종료일', 'date' => '시작일']),
        ];
    }
}
