<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\CoinTransaction;
use App\Models\ShowPurchase;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;

class TransactionHistoryController extends Controller
{
    /**
     * Display transaction history page.
     */
    public function index(): Response
    {
        $user = Auth::user();

        // Get coin transactions (topup)
        $coinTransactions = CoinTransaction::where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->get()
            ->map(function ($transaction) {
                return [
                    'id' => $transaction->id,
                    'type' => 'topup',
                    'reference_code' => $transaction->reference_code,
                    'amount' => $transaction->amount,
                    'coins' => $transaction->coins,
                    'fee' => $transaction->fee,
                    'status' => $transaction->status,
                    'payment_channel' => $transaction->payment_channel,
                    'payment_url' => $transaction->payment_url,
                    'qr_code_url' => $transaction->qr_code_url,
                    'expires_at' => $transaction->expires_at?->toIso8601String(),
                    'paid_at' => $transaction->paid_at?->toIso8601String(),
                    'created_at' => $transaction->created_at->toIso8601String(),
                ];
            });

        // Get show purchases
        $showPurchases = ShowPurchase::where('user_id', $user->id)
            ->with('show')
            ->orderBy('created_at', 'desc')
            ->get()
            ->map(function ($purchase) {
                return [
                    'id' => $purchase->id,
                    'type' => 'show_purchase',
                    'show_id' => $purchase->show_id,
                    'show_title' => $purchase->show_title,
                    'coin_price' => $purchase->coin_price,
                    'status' => 'success', // Show purchases are always successful
                    'created_at' => $purchase->created_at->toIso8601String(),
                ];
            });

        // Combine and sort by created_at
        $allTransactions = $coinTransactions->concat($showPurchases)
            ->sortByDesc('created_at')
            ->values();

        // Get pending count for display
        $pendingCount = CoinTransaction::where('user_id', $user->id)
            ->whereIn('status', ['pending', 'processing'])
            ->where(function ($query) {
                $query->whereNull('expires_at')
                    ->orWhere('expires_at', '>', now());
            })
            ->count();

        return Inertia::render('Dashboard/TransactionHistory', [
            'transactions' => $allTransactions,
            'pending_count' => $pendingCount,
            'coin_name' => User::getCoinName(),
            'coin_abbreviation' => User::getCoinAbbreviation(),
        ]);
    }
}
