<?php

namespace App\Http\Requests\Admin\Manage;

use App\Enums\Permission;
use Illuminate\Foundation\Http\FormRequest;

class StokOpnameRequest extends FormRequest
{
    public function authorize(): bool
    {
        if ($this->route('stokOpname')) {
            return $this->user()->can(Permission::STOK_OPNAMES_UPDATE->value);
        }

        return $this->user()->can(Permission::STOK_OPNAMES_CREATE->value);
    }

    public function rules(): array
    {
        return [
            'opname_date' => ['required', 'date'],
            'notes' => ['nullable', 'string', 'max:1000'],
            'items' => ['required', 'array', 'min:1'],
            'items.*.product_variant_id' => ['required', 'integer', 'exists:product_variants,id'],
            'items.*.physical_stock' => ['required', 'integer', 'min:0'],
            'items.*.notes' => ['nullable', 'string', 'max:500'],
        ];
    }

    public function messages(): array
    {
        return [
            'opname_date.required' => 'Tanggal opname wajib diisi.',
            'items.required' => 'Minimal 1 item stok opname harus ditambahkan.',
            'items.min' => 'Minimal 1 item stok opname harus ditambahkan.',
            'items.*.product_variant_id.required' => 'Varian produk wajib dipilih.',
            'items.*.product_variant_id.exists' => 'Varian produk tidak valid.',
            'items.*.physical_stock.required' => 'Stok fisik wajib diisi.',
            'items.*.physical_stock.min' => 'Stok fisik tidak boleh negatif.',
        ];
    }

    public function attributes(): array
    {
        return [
            'opname_date' => 'Tanggal Opname',
            'notes' => 'Catatan',
            'items' => 'Item Stok Opname',
            'items.*.product_variant_id' => 'Varian Produk',
            'items.*.physical_stock' => 'Stok Fisik',
            'items.*.notes' => 'Catatan',
        ];
    }
}
