<?php

namespace App\Http\Requests\Admin\Master\Product;

use App\Concerns\CurrencyStripping;
use App\Enums\PriceType;
use App\Enums\ProductStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Override;

class ProductRequest extends FormRequest
{
    use CurrencyStripping;

    public function authorize(): bool
    {
        return $this->user()->can('products.create')
            || $this->user()->can('products.update');
    }

    #[Override]
    public function prepareForValidation(): void
    {
        $this->merge($this->stripCurrencyDot($this->all(), 'shared_prices.*.price', 'variants.*.prices.*.price'));
    }

    public function rules(): array
    {
        $useSamePrice = $this->boolean('use_same_price');

        return [
            'name' => [
                'required',
                'string',
                'max:200',
            ],
            'description' => ['nullable', 'string'],
            'status' => ['nullable', Rule::in(ProductStatus::values())],
            'is_featured' => ['nullable', 'boolean'],
            'category_ids' => ['required', 'array', 'min:1'],
            'category_ids.*' => [Rule::exists('categories', 'id')],
            'use_same_price' => ['nullable', 'boolean'],
            'shared_prices' => [Rule::requiredIf(fn () => $useSamePrice), 'nullable', 'array'],
            'shared_prices.*.type' => [Rule::requiredIf(fn () => $useSamePrice), 'nullable', Rule::in(PriceType::values())],
            'shared_prices.*.price' => [Rule::requiredIf(fn () => $useSamePrice), 'nullable', 'integer', 'min:0'],
            'variants' => ['required', 'array', 'min:1'],
            'variants.*.id' => ['nullable', 'integer'],
            'variants.*.name' => ['required', 'string', 'max:200'],
            'variants.*.stock' => ['required', 'integer', 'min:0'],
            'variants.*.reject_stock' => ['required', 'integer', 'min:0'],
            'variants.*.retail_stock' => ['required', 'integer', 'min:0'],
            'variants.*.photo_keys' => ['required', 'array', 'min:1', 'max:5'],
            'variants.*.photo_keys.*' => ['required', 'string', 'max:500'],
            'variants.*.prices' => [Rule::requiredIf(fn () => ! $useSamePrice), 'nullable', 'array'],
            'variants.*.prices.*.id' => ['nullable', 'integer'],
            'variants.*.prices.*.type' => [Rule::requiredIf(fn () => ! $useSamePrice), 'nullable', Rule::in(PriceType::values())],
            'variants.*.prices.*.price' => [Rule::requiredIf(fn () => ! $useSamePrice), 'nullable', 'integer', 'min:0'],
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'nama produk',
            'description' => 'deskripsi',
            'status' => 'status',
            'is_featured' => 'ditampilkan di halaman depan',
            'category_ids' => 'kategori',
            'variants' => 'varian',
            'variants.*.name' => 'nama varian',
            'variants.*.stock' => 'stok',
            'variants.*.reject_stock' => 'stok reject',
            'variants.*.retail_stock' => 'stok retail',
            'variants.*.photo_keys' => 'foto',
            'variants.*.photo_keys.*' => 'foto',
            'variants.*.prices' => 'harga',
            'variants.*.prices.*.type' => 'tipe harga',
            'variants.*.prices.*.price' => 'harga',
        ];
    }
}
