<?php

namespace App\Http\Requests\Admin\Master\Product;

use App\Concerns\CurrencyStripping;
use App\Enums\PriceType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProductVariantRequest extends FormRequest
{
    use CurrencyStripping;

    public function authorize(): bool
    {
        return $this->user()->can('products.create')
            || $this->user()->can('products.update');
    }

    public function prepareForValidation(): void
    {
        $this->merge($this->stripCurrencyDot($this->all(), 'prices.*.price'));
    }

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:200'],
            'stock' => ['required', 'integer', 'min:0'],
            'reject_stock' => ['required', 'integer', 'min:0'],
            'retail_stock' => ['required', 'integer', 'min:0'],
            'photo_keys' => ['required', 'array', 'min:1', 'max:5'],
            'photo_keys.*' => ['required', 'string', 'max:500'],
            'prices' => ['required', 'array'],
            'prices.*.type' => ['required', Rule::in(PriceType::values())],
            'prices.*.price' => ['required', 'integer', 'min:0'],
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'nama varian',
            'stock' => 'stok bagus',
            'reject_stock' => 'stok reject',
            'retail_stock' => 'stok ecer',
            'photo_keys' => 'foto',
            'photo_keys.*' => 'foto',
            'prices' => 'harga',
            'prices.*.type' => 'tipe harga',
            'prices.*.price' => 'harga',
        ];
    }
}
