<?php

namespace App\Http\Requests\Admin\System\Setting;

use App\Enums\MarketplaceFeeScope;
use App\Enums\MarketplaceFeeValueType;
use App\Enums\Permission;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class MarketplaceRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can(Permission::SETTINGS_UPDATE->value) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $rules = [];

        foreach ($this->feeFieldKeys() as $key => $label) {
            $rules["{$key}.scope"] = ['required', Rule::enum(MarketplaceFeeScope::class)];
            $rules["{$key}.value_type"] = ['required', Rule::enum(MarketplaceFeeValueType::class)];
            $rules["{$key}.value"] = ['required', 'numeric', 'min:0'];
        }

        return $rules;
    }

    public function withValidator(Validator $validator): void
    {
        $validator->after(function (Validator $validator): void {
            foreach ($this->feeFieldKeys() as $key => $label) {
                $valueType = $this->input("{$key}.value_type");
                $value = $this->input("{$key}.value");

                if ($valueType === MarketplaceFeeValueType::PERCENT->value && is_numeric($value) && (float) $value > 100) {
                    $validator->errors()->add("{$key}.value", "{$label} tidak boleh lebih dari 100%.");
                }

                if ($valueType === MarketplaceFeeValueType::FLAT->value && is_numeric($value) && (float) $value != (int) $value) {
                    $validator->errors()->add("{$key}.value", "{$label} harus berupa nominal rupiah tanpa desimal.");
                }
            }
        });
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        $attributes = [];

        foreach ($this->feeFieldKeys() as $key => $label) {
            $attributes["{$key}.scope"] = "dasar {$label}";
            $attributes["{$key}.value_type"] = "tipe {$label}";
            $attributes["{$key}.value"] = $label;
        }

        return $attributes;
    }

    /**
     * @return array<string, string>
     */
    private function feeFieldKeys(): array
    {
        return [
            'tiktok_shop_platform_commission' => 'biaya komisi platform TikTok Shop',
            'tiktok_shop_logistics_service_fee' => 'biaya layanan logistik TikTok Shop',
            'tiktok_shop_dynamic_commission' => 'komisi dinamis TikTok Shop',
            'tiktok_shop_order_processing_fee' => 'biaya pemrosesan pesanan TikTok Shop',
            'tiktok_shop_affiliate' => 'affiliate TikTok Shop',
            'tiktok_shop_pre_order_service_fee' => 'biaya layanan pre order TikTok Shop',
            'shopee_admin_fee' => 'biaya administrasi Shopee',
            'shopee_program_fee' => 'biaya program Shopee',
            'shopee_shipping_savings' => 'hemat biaya kirim Shopee',
            'shopee_premium' => 'premi Shopee',
            'shopee_service_fee' => 'biaya layanan Shopee',
            'shopee_order_processing_fee' => 'biaya proses pesanan Shopee',
            'shopee_ams_commission_fee' => 'biaya komisi AMS Shopee',
            'shopee_pre_order' => 'pre order Shopee',
            'shopee_live_extra' => 'live extra Shopee',
        ];
    }
}
