<?php

namespace App\Http\Requests\Admin\Master;

use App\Enums\Permission;
use App\Enums\RawMaterialUnit;
use App\Http\Requests\Concerns\HasRawMaterialPriceRules;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RawMaterialRequest extends FormRequest
{
    use HasRawMaterialPriceRules;

    public function authorize(): bool
    {
        $permission = $this->isMethod('POST')
            ? Permission::RAW_MATERIALS_CREATE
            : Permission::RAW_MATERIALS_UPDATE;

        return $this->user()?->can($permission->value) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:200'],
            'unit' => ['required', Rule::enum(RawMaterialUnit::class)],

            ...$this->rawMaterialPriceRules(
                rawMaterialId: $this->route('rawMaterial')?->id,
                imagesRequired: $this->isMethod('POST'),
            ),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => 'nama bahan baku',
            'unit' => 'satuan',
            ...$this->rawMaterialPriceAttributes(),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'prices.*.s3_keys.required' => 'Foto varian wajib diisi.',
        ];
    }
}
