<?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 CuttingQuickCreateProductRequest extends FormRequest
{
    use ValidatesMediaUploads;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:200'],
            'description' => ['nullable', 'string'],

            'category_ids' => ['nullable', 'array'],
            'category_ids.*' => ['integer', Rule::exists('categories', 'id')->whereNull('deleted_at')],

            'variants' => ['required', 'array', 'min:1'],
            'variants.*.name' => ['required', 'string', 'max:200'],
            'variants.*.stock' => ['required', 'integer', 'min:0'],
            'variants.*.retail_stock' => ['required', 'integer', 'min:0'],
            ...$this->variantImageRules('variants'),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => 'Nama Produk',
            'description' => 'Deskripsi',
            'category_ids' => 'Kategori',
            'category_ids.*' => 'Kategori',
            'variants' => 'Varian',
            'variants.*.name' => 'Nama Varian',
            'variants.*.stock' => 'Stok',
            'variants.*.retail_stock' => 'Stok Ecer',
            ...$this->variantImageAttributes('variants', 'Foto Varian'),
        ];
    }
}
