<?php

declare(strict_types=1);

namespace App\Http\Requests\V1\Purchase;

use App\Models\Tenants\Purchase;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * 매입유형 일괄 변경 요청 검증
 */
class BulkUpdatePurchaseTypeRequest extends FormRequest
{
    /**
     * 권한 확인
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * 유효성 검사 규칙
     *
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'ids' => ['required', 'array', 'min:1'],
            'ids.*' => ['required', 'integer', 'min:1'],
            'purchase_type' => ['required', 'string', Rule::in(array_keys(Purchase::PURCHASE_TYPES))],
        ];
    }

    /**
     * 유효성 검사 메시지
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'ids.required' => __('validation.required', ['attribute' => 'ID 목록']),
            'ids.array' => __('validation.array', ['attribute' => 'ID 목록']),
            'ids.min' => __('validation.min.array', ['attribute' => 'ID 목록', 'min' => 1]),
            'ids.*.required' => __('validation.required', ['attribute' => 'ID']),
            'ids.*.integer' => __('validation.integer', ['attribute' => 'ID']),
            'ids.*.min' => __('validation.min.numeric', ['attribute' => 'ID', 'min' => 1]),
            'purchase_type.required' => __('validation.required', ['attribute' => '매입유형']),
            'purchase_type.string' => __('validation.string', ['attribute' => '매입유형']),
            'purchase_type.in' => __('validation.in', ['attribute' => '매입유형']),
        ];
    }

    /**
     * 검증된 ID 배열 반환
     *
     * @return array<int, int>
     */
    public function getIds(): array
    {
        return $this->validated('ids');
    }

    /**
     * 검증된 매입유형 반환
     */
    public function getPurchaseType(): string
    {
        return $this->validated('purchase_type');
    }
}
