<?php

namespace App\Http\Requests\Admin\Manage;

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

class PurchaseNewVariantRequest extends FormRequest
{
    use ValidatesMediaUploads;

    public function authorize(): bool
    {
        return $this->user()?->can(Permission::PURCHASES_CREATE->value) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'raw_material_id' => [
                'required',
                'integer',
                Rule::exists('raw_materials', 'id')->whereNull('deleted_at'),
            ],
            'variant' => ['required', 'string', 'max:200'],
            'price' => ['required', 'integer', 'min:0'],
            'stock' => ['nullable', 'numeric', 'decimal:0,4', 'min:0'],
            ...$this->photoRules(),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'raw_material_id' => 'Bahan Baku',
            'variant' => 'Nama Varian',
            'price' => 'Harga',
            'stock' => 'Stok',
            ...$this->photoUploadAttributes('Foto Varian'),
        ];
    }
}
