<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Add missing ingame service settings
        $settings = [
            // Reset Storepass
            ['key' => 'reset_storepass_price', 'value' => '50', 'description' => 'Price for reset storehouse password service (in gold)', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'reset_storepass_enabled', 'value' => '1', 'description' => 'Enable/disable reset storehouse password service', 'created_at' => now(), 'updated_at' => now()],
            
            // Reset Title
            ['key' => 'reset_title_price', 'value' => '30', 'description' => 'Price for reset title service (in gold)', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'reset_title_enabled', 'value' => '1', 'description' => 'Enable/disable reset title service', 'created_at' => now(), 'updated_at' => now()],
            
            // Rename Role
            ['key' => 'rename_role_price', 'value' => '200', 'description' => 'Price for rename character service (in gold)', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'rename_role_enabled', 'value' => '1', 'description' => 'Enable/disable rename character service', 'created_at' => now(), 'updated_at' => now()],
            
            // Item Exchange
            ['key' => 'item_exchange_price', 'value' => '1', 'description' => 'Gold per item for item exchange service', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'item_exchange_enabled', 'value' => '1', 'description' => 'Enable/disable item exchange service', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'item_exchange_item_id', 'value' => '11208', 'description' => 'Item ID for item exchange service', 'created_at' => now(), 'updated_at' => now()],
            
            // Teleport settings (if missing)
            ['key' => 'teleport_posx', 'value' => '448.0', 'description' => 'Teleport X position', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'teleport_posy', 'value' => '900.0', 'description' => 'Teleport Y position', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'teleport_posz', 'value' => '210.0', 'description' => 'Teleport Z position', 'created_at' => now(), 'updated_at' => now()],
            ['key' => 'teleport_worldtag', 'value' => '1', 'description' => 'Teleport world tag', 'created_at' => now(), 'updated_at' => now()],
        ];

        foreach ($settings as $setting) {
            // Only insert if not exists
            if (!DB::table('ingame_service_settings')->where('key', $setting['key'])->exists()) {
                DB::table('ingame_service_settings')->insert($setting);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove the added settings
        DB::table('ingame_service_settings')->whereIn('key', [
            'reset_storepass_price',
            'reset_storepass_enabled',
            'reset_title_price',
            'reset_title_enabled',
            'rename_role_price',
            'rename_role_enabled',
            'item_exchange_price',
            'item_exchange_enabled',
            'item_exchange_item_id',
            'teleport_posx',
            'teleport_posy',
            'teleport_posz',
            'teleport_worldtag',
        ])->delete();
    }
};
