<?php

namespace App\Http\Requests\Admin\Settings;

use Illuminate\Foundation\Http\FormRequest;

class UpdateMarketplaceRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('settings.update_marketplace');
    }

    private function feeRuleRules(): array
    {
        return ['required', 'array'];
    }

    private function feeFieldRules(string $prefix): array
    {
        return [
            "{$prefix}.base" => ['required', 'string', 'in:per_transaksi,per_produk'],
            "{$prefix}.type" => ['required', 'string', 'in:flat,persentase'],
            "{$prefix}.value" => ['required', 'numeric', 'min:0'],
        ];
    }

    public function rules(): array
    {
        $keys = [
            'tiktok_shop_platform_commission',
            'tiktok_shop_logistics_service_fee',
            'tiktok_shop_dynamic_commission',
            'tiktok_shop_order_processing_fee',
            'tiktok_shop_affiliate',
            'tiktok_shop_pre_order_service_fee',
            'shopee_admin_fee',
            'shopee_program_fee',
            'shopee_shipping_savings',
            'shopee_premium',
            'shopee_service_fee',
            'shopee_order_processing_fee',
            'shopee_ams_commission_fee',
            'shopee_pre_order',
            'shopee_live_extra',
        ];

        $rules = [];

        foreach ($keys as $key) {
            $rules[$key] = $this->feeRuleRules();
            $rules = array_merge($rules, $this->feeFieldRules($key));
        }

        return $rules;
    }

    public function attributes(): array
    {
        return [
            'tiktok_shop_platform_commission.base' => 'dasar komisi platform tiktok shop',
            'tiktok_shop_platform_commission.type' => 'tipe komisi platform tiktok shop',
            'tiktok_shop_platform_commission.value' => 'nilai komisi platform tiktok shop',
            'tiktok_shop_logistics_service_fee.base' => 'dasar layanan logistik tiktok shop',
            'tiktok_shop_logistics_service_fee.type' => 'tipe layanan logistik tiktok shop',
            'tiktok_shop_logistics_service_fee.value' => 'nilai layanan logistik tiktok shop',
            'tiktok_shop_dynamic_commission.base' => 'dasar komisi dinamis tiktok shop',
            'tiktok_shop_dynamic_commission.type' => 'tipe komisi dinamis tiktok shop',
            'tiktok_shop_dynamic_commission.value' => 'nilai komisi dinamis tiktok shop',
            'tiktok_shop_order_processing_fee.base' => 'dasar pemrosesan pesanan tiktok shop',
            'tiktok_shop_order_processing_fee.type' => 'tipe pemrosesan pesanan tiktok shop',
            'tiktok_shop_order_processing_fee.value' => 'nilai pemrosesan pesanan tiktok shop',
            'tiktok_shop_affiliate.base' => 'dasar affiliate tiktok shop',
            'tiktok_shop_affiliate.type' => 'tipe affiliate tiktok shop',
            'tiktok_shop_affiliate.value' => 'nilai affiliate tiktok shop',
            'tiktok_shop_pre_order_service_fee.base' => 'dasar layanan PO tiktok shop',
            'tiktok_shop_pre_order_service_fee.type' => 'tipe layanan PO tiktok shop',
            'tiktok_shop_pre_order_service_fee.value' => 'nilai layanan PO tiktok shop',
            'shopee_admin_fee.base' => 'dasar biaya administrasi shopee',
            'shopee_admin_fee.type' => 'tipe biaya administrasi shopee',
            'shopee_admin_fee.value' => 'nilai biaya administrasi shopee',
            'shopee_program_fee.base' => 'dasar biaya program shopee',
            'shopee_program_fee.type' => 'tipe biaya program shopee',
            'shopee_program_fee.value' => 'nilai biaya program shopee',
            'shopee_shipping_savings.base' => 'dasar hemat biaya kirim shopee',
            'shopee_shipping_savings.type' => 'tipe hemat biaya kirim shopee',
            'shopee_shipping_savings.value' => 'nilai hemat biaya kirim shopee',
            'shopee_premium.base' => 'dasar premi shopee',
            'shopee_premium.type' => 'tipe premi shopee',
            'shopee_premium.value' => 'nilai premi shopee',
            'shopee_service_fee.base' => 'dasar biaya layanan shopee',
            'shopee_service_fee.type' => 'tipe biaya layanan shopee',
            'shopee_service_fee.value' => 'nilai biaya layanan shopee',
            'shopee_order_processing_fee.base' => 'dasar biaya proses pesanan shopee',
            'shopee_order_processing_fee.type' => 'tipe biaya proses pesanan shopee',
            'shopee_order_processing_fee.value' => 'nilai biaya proses pesanan shopee',
            'shopee_ams_commission_fee.base' => 'dasar biaya komisi AMS shopee',
            'shopee_ams_commission_fee.type' => 'tipe biaya komisi AMS shopee',
            'shopee_ams_commission_fee.value' => 'nilai biaya komisi AMS shopee',
            'shopee_pre_order.base' => 'dasar PO shopee',
            'shopee_pre_order.type' => 'tipe PO shopee',
            'shopee_pre_order.value' => 'nilai PO shopee',
            'shopee_live_extra.base' => 'dasar live extra shopee',
            'shopee_live_extra.type' => 'tipe live extra shopee',
            'shopee_live_extra.value' => 'nilai live extra shopee',
        ];
    }
}
