<?php

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

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

class RawMaterialRequest extends FormRequest
{
    use CurrencyStripping;

    public function authorize(): bool
    {
        return true;
    }

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

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:200'],
            'unit' => ['required', Rule::in(RawMaterialUnit::values())],
            'is_active' => ['nullable', 'boolean'],
            'variants' => ['required', 'array', 'min:1'],
            'variants.*.id' => ['nullable', 'integer'],
            'variants.*.variant' => ['required', 'string', 'max:200'],
            'variants.*.price' => ['required', 'integer', 'min:0'],
            'variants.*.stock' => ['required', 'integer', 'min:0'],
            'variants.*.photo_key' => ['required', 'string', 'max:500'],
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'Nama Bahan Baku',
            'unit' => 'Satuan',
            'is_active' => 'Status',
            'variants' => 'Varian',
            'variants.*.variant' => 'Nama Varian',
            'variants.*.price' => 'Harga',
            'variants.*.stock' => 'Stok',
            'variants.*.photo_key' => 'Foto Varian',
        ];
    }
}
