<?php

namespace App\Http\Requests\Approval;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        $stepTypes = implode(',', \App\Models\Tenants\ApprovalLine::STEP_TYPES);

        return [
            // form_id 또는 form_code
            'form_id' => 'nullable|integer|exists:approval_forms,id',
            'form_code' => 'nullable|string|exists:approval_forms,code',
            'title' => 'nullable|string|max:200',
            'content' => 'nullable|array',
            'attachments' => 'nullable|array',
            'attachments.*' => 'integer|exists:files,id',
            // 결재선 수정 지원
            'steps' => 'nullable|array',
            'steps.*.type' => "nullable|string|in:{$stepTypes}",
            'steps.*.step_type' => "nullable|string|in:{$stepTypes}",
            'steps.*.user_id' => 'nullable|integer|exists:users,id',
            'steps.*.approver_id' => 'nullable|integer|exists:users,id',
            'steps.*.step_order' => 'nullable|integer|min:1',
        ];
    }

    public function messages(): array
    {
        return [
            'form_id.exists' => __('validation.exists', ['attribute' => '결재양식']),
        ];
    }
}
