<?php

namespace App\Http\Requests\Api\V1;

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

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

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

        return [
            'folder_key' => [
                'sometimes',
                'string',
                'max:50',
                'regex:/^[a-z0-9_-]+$/',
                Rule::unique('folders')->where(function ($query) use ($folderId) {
                    return $query->where('tenant_id', auth()->user()->tenant_id ?? 0)
                        ->where('id', '!=', $folderId);
                }),
            ],
            'folder_name' => 'sometimes|string|max:100',
            'description' => 'nullable|string|max:500',
            'display_order' => 'sometimes|integer|min:0',
            'is_active' => 'sometimes|boolean',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:20|regex:/^#[0-9A-Fa-f]{6}$/',
        ];
    }

    public function messages(): array
    {
        return [
            'folder_key.unique' => __('error.folder_key_duplicate'),
            'folder_key.regex' => __('error.folder_key_format'),
            'color.regex' => __('error.color_format'),
        ];
    }
}
