<?php

namespace App\Http\Requests\Roadmap;

use App\Models\Admin\AdminRoadmapPlan;
use Illuminate\Foundation\Http\FormRequest;

class StorePlanRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:200',
            'description' => 'nullable|string|max:2000',
            'content' => 'nullable|string',
            'category' => 'nullable|in:'.implode(',', array_keys(AdminRoadmapPlan::getCategories())),
            'status' => 'nullable|in:'.implode(',', array_keys(AdminRoadmapPlan::getStatuses())),
            'priority' => 'nullable|in:'.implode(',', array_keys(AdminRoadmapPlan::getPriorities())),
            'phase' => 'nullable|in:'.implode(',', array_keys(AdminRoadmapPlan::getPhases())),
            'start_date' => 'nullable|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
            'progress' => 'nullable|integer|min:0|max:100',
            'color' => 'nullable|string|max:7',
            'sort_order' => 'nullable|integer',
        ];
    }

    public function attributes(): array
    {
        return [
            'title' => '계획 제목',
            'description' => '설명',
            'content' => '상세 내용',
            'category' => '카테고리',
            'status' => '상태',
            'priority' => '우선순위',
            'phase' => 'Phase',
            'start_date' => '시작일',
            'end_date' => '종료일',
            'progress' => '진행률',
            'color' => '색상',
        ];
    }

    public function messages(): array
    {
        return [
            'title.required' => '계획 제목은 필수입니다.',
            'title.max' => '계획 제목은 최대 200자까지 입력 가능합니다.',
            'end_date.after_or_equal' => '종료일은 시작일 이후여야 합니다.',
            'progress.min' => '진행률은 0 이상이어야 합니다.',
            'progress.max' => '진행률은 100 이하여야 합니다.',
        ];
    }

    protected function prepareForValidation(): void
    {
        if (! $this->has('status')) {
            $this->merge(['status' => AdminRoadmapPlan::STATUS_PLANNED]);
        }
        if (! $this->has('category')) {
            $this->merge(['category' => AdminRoadmapPlan::CATEGORY_GENERAL]);
        }
        if (! $this->has('priority')) {
            $this->merge(['priority' => AdminRoadmapPlan::PRIORITY_MEDIUM]);
        }
        if (! $this->has('phase')) {
            $this->merge(['phase' => AdminRoadmapPlan::PHASE_1]);
        }
    }
}
