<?php

namespace App\Http\Requests\Concerns;

use Illuminate\Validation\Rule;

trait HasRawMaterialPriceRules
{
    use ValidatesMediaUploads;

    /**
     * @return array<string, mixed>
     */
    protected function rawMaterialPriceRules(string $pricesKey = 'prices', ?int $rawMaterialId = null): array
    {
        $rules = [
            "{$pricesKey}" => ['required', 'array', 'min:1'],
            "{$pricesKey}.*.variant" => ['required', 'string', 'max:200'],
            "{$pricesKey}.*.price" => ['required', 'integer', 'gt:0'],
            "{$pricesKey}.*.stock" => ['required', 'numeric', 'decimal:0,4', 'min:0'],
            ...$this->variantImageRules($pricesKey),
        ];

        if ($rawMaterialId) {
            $rules["{$pricesKey}.*.id"] = [
                'nullable',
                'integer',
                Rule::exists('raw_material_prices', 'id')
                    ->where('raw_material_id', $rawMaterialId)
                    ->whereNull('deleted_at'),
            ];
        }

        return $rules;
    }

    /**
     * @return array<string, string>
     */
    protected function rawMaterialPriceAttributes(string $pricesKey = 'prices'): array
    {
        return [
            $pricesKey => 'Varian',
            "{$pricesKey}.*.variant" => 'Nama Varian',
            "{$pricesKey}.*.price" => 'Harga',
            "{$pricesKey}.*.stock" => 'Stok',
            ...$this->variantImageAttributes($pricesKey, 'Foto Varian'),
        ];
    }
}
