<?php

namespace App\Http\Requests\Concerns;

use App\Enums\Role;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * @method FormRequest user()
 */
trait HasProductVariantRules
{
    use ValidatesMediaUploads;

    /**
     * @return array<string, mixed>
     */
    protected function productVariantRules(string $variantsKey = 'variants', ?int $productId = null): array
    {
        $isAdminBahanBaku = $this->user()?->hasRole(Role::ADMIN_BAHAN_BAKU->value) ?? false;

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

        if ($productId) {
            $rules["{$variantsKey}.*.id"] = [
                'nullable',
                'integer',
                Rule::exists('product_variants', 'id')
                    ->where('product_id', $productId)
                    ->whereNull('deleted_at'),
            ];
        }

        if (! $isAdminBahanBaku) {
            $rules["{$variantsKey}.*.prices"] = ['required', 'array'];
            $rules["{$variantsKey}.*.prices.distributor"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.agent"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.sub_agent"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.grosir"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.retail"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.tiktok"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.shopee"] = ['required', 'integer', 'min:0'];
            $rules["{$variantsKey}.*.prices.harga_modal"] = ['required', 'integer', 'min:0'];
        }

        return $rules;
    }

    /**
     * @return array<string, string>
     */
    protected function productVariantAttributes(string $variantsKey = 'variants'): array
    {
        return [
            $variantsKey => 'Varian',
            "{$variantsKey}.*.name" => 'Nama Varian',
            "{$variantsKey}.*.stock" => 'Stok',
            "{$variantsKey}.*.retail_stock" => 'Stok Ecer',
            "{$variantsKey}.*.prices" => 'Harga',
            "{$variantsKey}.*.prices.distributor" => 'Distributor',
            "{$variantsKey}.*.prices.agent" => 'Agen',
            "{$variantsKey}.*.prices.sub_agent" => 'Sub Agen',
            "{$variantsKey}.*.prices.grosir" => 'Grosir',
            "{$variantsKey}.*.prices.retail" => 'Eceran',
            "{$variantsKey}.*.prices.tiktok" => 'TikTok',
            "{$variantsKey}.*.prices.shopee" => 'Shopee',
            "{$variantsKey}.*.prices.harga_modal" => 'Harga Modal',
            ...$this->variantImageAttributes($variantsKey, 'Foto Varian'),
        ];
    }
}
