<?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 $this->user()->can('raw_materials.create')
            || $this->user()->can('raw_materials.update');
    }

    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',
        ];
    }
}
