<?php

namespace App\Http\Requests\Admin\Manage;

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

class StokOpnameAutoSaveRequest extends FormRequest
{
    public function authorize(): bool
    {
        $user = $this->user();

        return $user !== null && (
            $user->can(Permission::STOK_OPNAMES_CREATE->value)
            || $user->can(Permission::STOK_OPNAMES_UPDATE->value)
        );
    }

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