<?php

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

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

class ProductRequest extends FormRequest
{
    use CurrencyStripping;

    public function authorize(): bool
    {
        return true;
    }

    #[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(['active', 'inactive', 'draft'])],
            'category_ids' => ['required', 'array', 'min:1'],
            'category_ids.*' => ['exists:categories,id'],
            'use_same_price' => ['nullable', 'boolean'],
            'shared_prices' => ['required_if:use_same_price,true', 'nullable', 'array', ...($useSamePrice ? ['size:9'] : [])],
            'shared_prices.*.type' => ['required_if:use_same_price,true', 'nullable', Rule::in(PriceType::values())],
            'shared_prices.*.price' => ['required_if:use_same_price,true', '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' => ['required_if:use_same_price,false', 'nullable', 'array', ...(! $useSamePrice ? ['size:9'] : [])],
            'variants.*.prices.*.id' => ['nullable', 'integer'],
            'variants.*.prices.*.type' => ['required_if:use_same_price,false', 'nullable', Rule::in(PriceType::values())],
            'variants.*.prices.*.price' => ['required_if:use_same_price,false', 'nullable', 'integer', 'min:0'],
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'Nama Produk',
            'description' => 'Deskripsi',
            'status' => 'Status',
            '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',
        ];
    }
}
