<?php

namespace App\Http\Requests\Admin\Manage;

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

class RestockRequest extends FormRequest
{
    use ValidatesMediaUploads;

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

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'notes' => ['nullable', 'string', 'max:100'],
            'stock_type' => ['required', 'string', 'in:'.implode(',', ProductStockQuality::values())],
            ...$this->photoRules('photos', 1),

            'items' => ['required', 'array', 'min:1'],
            'items.*.product_variant_id' => ['required', 'integer'],
            'items.*.quantity' => ['required', 'numeric', 'decimal:0,4', 'gt:0'],
            'items.*.unit_price' => ['required', 'integer', 'gt:0'],
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'notes' => 'keterangan',
            'stock_type' => 'tipe stok',
            'items' => 'produk',
            'items.*.product_variant_id' => 'varian produk',
            'items.*.quantity' => 'jumlah',
            'items.*.unit_price' => 'harga',
            ...$this->photoUploadAttributes('bukti restock', 'photos'),
        ];
    }
}
