<?php

namespace Database\Seeders;

use App\Http\Controllers\Admin\PaymentGatewayController;
use App\Models\PaymentGatewaySetting;
use App\Services\PaymentManager;
use Illuminate\Database\Seeder;
use ReflectionClass;

class PaymentGatewaySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Payment gateway modül ayarları - sadece PaymentGatewaySetting tablosuna kaydet
        // Sync metodu ile PaymentGateway tablosuna otomatik aktarılacak
        $moduleSettings = [
            'banktransfer' => [
                'status' => 'active',
                'display_name' => 'Bank Transfer',
                'sort_order' => 0,
                'fee_percentage' => 3.0000,
                'fee_fixed' => 0.00,
                'minimum_amount' => 0.00,
                'maximum_amount' => 0.00,
                'test_mode' => 'true',
                'currency_converter' => null,
                // PRC-424 — Fresh install placeholders. Clearly signal
                // "configure me" so a new admin notices in the gateway
                // settings, and isBankTransferConfigured() treats these as
                // unconfigured (customer sees a warning instead of junk IBAN).
                'bank_name' => 'LÜTFEN DOLDURUN — Banka Adı',
                'account_holder' => 'LÜTFEN DOLDURUN — Hesap Sahibi',
                'iban' => 'TR00 0000 0000 0000 0000 0000 00',
                'swift_code' => null,
                'branch_code' => null,
                'payment_instructions' => null,
                'auto_approve' => 'false',
            ],
            'payu' => [
                'status' => 'active',
                'display_name' => 'PayU',
                'sort_order' => 0,
                'fee_percentage' => 9.3100,
                'fee_fixed' => 0.00,
                'minimum_amount' => 0.00,
                'maximum_amount' => 0.00,
                'test_mode' => 'true',
                'currency_converter' => null,
                'security_type' => '3d',
                'allow_save_card' => 'true',
                'allow_saved_card_payment' => 'true',
                'merchant' => 'Zaza ali',
                'secret_key' => '123412123123',
                'lu_url' => 'https://secure.payu.com.tr/order/lu.php',
                'idn_url' => 'https://secure.payu.com.tr/order/idn.php',
                'irn_url' => 'https://secure.payu.com.tr/order/irn.php',
                'currency' => 'TRY',
                'language' => 'TR',
                'sandbox_mode' => 'true',
            ],
        ];

        $paymentManager = app(PaymentManager::class);
        $controller = new PaymentGatewayController($paymentManager);

        // Reflection ile private sync metoduna erişim
        $reflection = new ReflectionClass($controller);
        $syncMethod = $reflection->getMethod('syncModuleToPaymentGateway');
        $syncMethod->setAccessible(true);

        foreach ($moduleSettings as $moduleName => $settings) {
            // PaymentGatewaySetting tablosuna ayarları kaydet
            foreach ($settings as $key => $value) {
                PaymentGatewaySetting::updateModuleSetting($moduleName, $key, $value);
            }

            // PaymentGateway tablosuna sync et
            try {
                $syncMethod->invoke($controller, $moduleName, $settings);
            } catch (\Exception $e) {
            }
        }

    }
}
