<?php

namespace App\Http\Requests\Admin\Manage;

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

class PurchaseNewRawMaterialRequest 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 [
            'name' => ['required', 'string', 'max:200'],
            'unit' => ['required', Rule::enum(RawMaterialUnit::class)],
            'variant' => ['required', 'string', 'max:200'],
            'price' => ['required', 'integer', 'gt:0'],
            'quantity' => ['required', 'numeric', 'decimal:0,4', 'gt:0'],
            'photos' => ['required_without:s3_keys', 'array', 'min:1', 'max:5'],
            'photos.*' => ['image', 'mimes:jpg,jpeg,png,webp', 'max:5120'],
            's3_keys' => ['required_without:photos', 'array', 'min:1', 'max:5'],
            's3_keys.*' => ['required', 'string'],
        ];
    }

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

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => 'nama bahan baku',
            'unit' => 'satuan',
            'variant' => 'nama varian',
            'price' => 'harga',
            'quantity' => 'jumlah',
            ...$this->photoUploadAttributes('foto varian'),
        ];
    }
}
