<?php

namespace App\Http\Controllers\Frontend\TopUp\History;

use App\Http\Controllers\Frontend\TopUp\TopUpController;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;

class HistoryController extends TopUpController
{
    public function index()
    {
        $user = Auth::user();

        // Check if Ipaymu service is active
        $ipaymuSettings = DB::table('ipaymu_settings')->first();
        $ipaymuEnabled = $ipaymuSettings && $ipaymuSettings->is_active;

        // Check if Duitku service is active
        $duitkuSettings = DB::table('duitku_settings')->first();
        $duitkuEnabled = $duitkuSettings && $duitkuSettings->is_active;

        // Check if Tripay service is active
        $tripaySettings = DB::table('tripay_settings')->first();
        $tripayEnabled = $tripaySettings && $tripaySettings->is_active;

        // Check if Pakasir service is active
        $pakasirSettings = DB::table('pakasir_settings')->first();
        $pakasirEnabled = $pakasirSettings && $pakasirSettings->is_active;

        // Only fetch transactions for enabled services
        $ipaymuTransactions = $ipaymuEnabled ? DB::table('ipaymu_transactions')
            ->where('user_id', $user->ID)
            ->orderBy('created_at', 'desc')
            ->paginate(10, ['*'], 'ipaymu_page') : new LengthAwarePaginator([], 0, 10, 1, ['pageName' => 'ipaymu_page']);

        $duitkuTransactions = $duitkuEnabled ? DB::table('duitku_transactions')
            ->where('user_id', $user->ID)
            ->orderBy('created_at', 'desc')
            ->paginate(10, ['*'], 'duitku_page') : new LengthAwarePaginator([], 0, 10, 1, ['pageName' => 'duitku_page']);

        $tripayTransactions = $tripayEnabled ? DB::table('tripay_transactions')
            ->where('user_id', $user->ID)
            ->orderBy('created_at', 'desc')
            ->paginate(10, ['*'], 'tripay_page') : new LengthAwarePaginator([], 0, 10, 1, ['pageName' => 'tripay_page']);

        $pakasirTransactions = $pakasirEnabled ? DB::table('pakasir_transactions')
            ->where('user_id', $user->ID)
            ->orderBy('created_at', 'desc')
            ->paginate(10, ['*'], 'pakasir_page') : new LengthAwarePaginator([], 0, 10, 1, ['pageName' => 'pakasir_page']);

        $ingameTransactions = DB::table('pw_gold')
            ->where('user_id', $user->ID)
            ->orderBy('created_at', 'desc')
            ->paginate(10, ['*'], 'ingame_page');

        // Get current cash from ingame (cash_add - cash_used) / 100
        $currentCash = 0;
        try {
            $currentCash = (int) $this->api->getCurrentCash($user->ID);
        } catch (\Exception $e) {
            // Ignore error
        }

        return Inertia::render('frontend/topup/history/index', [
            'ipaymuTransactions' => $ipaymuTransactions,
            'duitkuTransactions' => $duitkuTransactions,
            'tripayTransactions' => $tripayTransactions,
            'pakasirTransactions' => $pakasirTransactions,
            'ingameTransactions' => $ingameTransactions,
            'userGold' => $user->gold ?? 0,
            'currentCash' => $currentCash,
            'ipaymuEnabled' => $ipaymuEnabled,
            'duitkuEnabled' => $duitkuEnabled,
            'tripayEnabled' => $tripayEnabled,
            'pakasirEnabled' => $pakasirEnabled,
        ]);
    }
}
