<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Http\Controllers\Controller;
use App\Models\IngameServiceSetting;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ServiceController extends Controller
{
    public function index()
    {
        $settings = IngameServiceSetting::all()->pluck('value', 'key')->toArray();

        // Add teleport position settings
        $settings['teleport_posx'] = $settings['teleport_posx'] ?? '448.0';
        $settings['teleport_posy'] = $settings['teleport_posy'] ?? '900.0';
        $settings['teleport_posz'] = $settings['teleport_posz'] ?? '210.0';
        $settings['teleport_worldtag'] = $settings['teleport_worldtag'] ?? '1';

        // Add cultivation reset settings
        $settings['cultivation_reset_price'] = $settings['cultivation_reset_price'] ?? '0';
        $settings['cultivation_reset_enabled'] = $settings['cultivation_reset_enabled'] ?? '0';
        $settings['cultivation_reset_hex'] = $settings['cultivation_reset_hex'] ?? '060000009f000000000000000a000000a700000000000000010000007a050000000000000a000000a1000000000000000a0000009e000000000000000a000000a0000000000000000a000000';

        // Add skill reset settings
        $settings['skill_reset_price'] = $settings['skill_reset_price'] ?? '0';
        $settings['skill_reset_enabled'] = $settings['skill_reset_enabled'] ?? '0';
        $settings['skill_reset_hex'] = $settings['skill_reset_hex'] ?? '060000009f000000000000000a000000a700000000000000010000007a050000000000000a000000a1000000000000000a0000009e000000000000000a000000a0000000000000000a000000';

        return Inertia::render('admin/manage/ingame/index', [
            'settings' => $settings,
        ]);
    }

    public function update(Request $request)
    {
        $request->validate([
            'teleport_price' => 'required|integer|min:0',
            'teleport_enabled' => 'required|boolean',
            'teleport_posx' => 'required|numeric',
            'teleport_posy' => 'required|numeric',
            'teleport_posz' => 'required|numeric',
            'teleport_worldtag' => 'required|integer|min:1',
            'max_meridian_price' => 'required|integer|min:0',
            'max_meridian_enabled' => 'required|boolean',
            'reset_storepass_price' => 'required|integer|min:0',
            'reset_storepass_enabled' => 'required|boolean',
            'reset_title_price' => 'required|integer|min:0',
            'reset_title_enabled' => 'required|boolean',
            'rename_role_price' => 'required|integer|min:0',
            'rename_role_enabled' => 'required|boolean',
            'item_exchange_item_id' => 'required|integer|min:1',
            'item_exchange_price' => 'required|integer|min:0',
            'item_exchange_enabled' => 'required|boolean',
            'cultivation_reset_price' => 'required|integer|min:0',
            'cultivation_reset_enabled' => 'required|boolean',
            'cultivation_reset_hex' => 'required|string',
            'skill_reset_price' => 'required|integer|min:0',
            'skill_reset_enabled' => 'required|boolean',
            'skill_reset_hex' => 'required|string',
        ]);

        IngameServiceSetting::set('teleport_price', $request->teleport_price);
        IngameServiceSetting::set('teleport_enabled', $request->teleport_enabled ? '1' : '0');
        IngameServiceSetting::set('teleport_posx', $request->teleport_posx);
        IngameServiceSetting::set('teleport_posy', $request->teleport_posy);
        IngameServiceSetting::set('teleport_posz', $request->teleport_posz);
        IngameServiceSetting::set('teleport_worldtag', $request->teleport_worldtag);
        IngameServiceSetting::set('max_meridian_price', $request->max_meridian_price);
        IngameServiceSetting::set('max_meridian_enabled', $request->max_meridian_enabled ? '1' : '0');
        IngameServiceSetting::set('reset_storepass_price', $request->reset_storepass_price);
        IngameServiceSetting::set('reset_storepass_enabled', $request->reset_storepass_enabled ? '1' : '0');
        IngameServiceSetting::set('reset_title_price', $request->reset_title_price);
        IngameServiceSetting::set('reset_title_enabled', $request->reset_title_enabled ? '1' : '0');
        IngameServiceSetting::set('rename_role_price', $request->rename_role_price);
        IngameServiceSetting::set('rename_role_enabled', $request->rename_role_enabled ? '1' : '0');
        IngameServiceSetting::set('item_exchange_item_id', $request->item_exchange_item_id);
        IngameServiceSetting::set('item_exchange_price', $request->item_exchange_price);
        IngameServiceSetting::set('item_exchange_enabled', $request->item_exchange_enabled ? '1' : '0');
        IngameServiceSetting::set('cultivation_reset_price', $request->cultivation_reset_price);
        IngameServiceSetting::set('cultivation_reset_enabled', $request->cultivation_reset_enabled ? '1' : '0');
        IngameServiceSetting::set('cultivation_reset_hex', $request->cultivation_reset_hex);
        IngameServiceSetting::set('skill_reset_price', $request->skill_reset_price);
        IngameServiceSetting::set('skill_reset_enabled', $request->skill_reset_enabled ? '1' : '0');
        IngameServiceSetting::set('skill_reset_hex', $request->skill_reset_hex);

        return back()->with('success', 'Ingame service settings updated successfully.');
    }
}
