<?php

/**
 * 文件功能：后台更新商店商品请求验证。
 *
 * 统一校验商店商品编辑字段。
 */

namespace App\Http\Requests;

use App\Models\ShopItem;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * 后台更新商店商品请求
 * 负责编辑商品时的权限与字段校验。
 */
class UpdateShopItemRequest extends FormRequest
{
    /**
     * 判断当前用户是否允许编辑商品。
     */
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    /**
     * 获取更新商品验证规则。
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $shopItem = $this->route('shopItem');

        return [
            'name' => ['required', 'string', 'max:100'],
            'slug' => ['required', 'string', 'max:100', Rule::unique('shop_items', 'slug')->ignore($shopItem?->id)],
            'icon' => ['required', 'string', 'max:20'],
            'description' => ['nullable', 'string', 'max:500'],
            'price' => ['required', 'integer', 'min:0'],
            'type' => ['required', Rule::in($this->allowedTypes())],
            'duration_days' => ['nullable', 'integer', 'min:0'],
            'duration_minutes' => ['nullable', 'integer', 'min:0'],
            'intimacy_bonus' => ['nullable', 'integer', 'min:0'],
            'charm_bonus' => ['nullable', 'integer', 'min:0'],
            'sort_order' => ['required', 'integer', 'min:0'],
            'is_active' => ['boolean'],
        ];
    }

    /**
     * 获取允许后台配置的商品类型。
     *
     * @return array<int, string>
     */
    protected function allowedTypes(): array
    {
        return [
            'instant',
            'duration',
            'one_time',
            'ring',
            'auto_fishing',
            ShopItem::TYPE_SIGN_REPAIR,
            'msg_bubble',
            'msg_name_color',
            'msg_text_color',
            'avatar_frame',
        ];
    }
}
