<?php

namespace App\Http\Controllers;

use App\Models\Wallet;
use App\Models\WalletTransaction;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class WalletController extends Controller
{

    public function wallet()
    {
        $user = Auth::user();

        return response()->json([
            'success' => true,
            'data' => $user?->wallet?->balance ?? 0,
        ]);
    }
    public function show()
    {
        $user = Auth::user();

        $wallet = Wallet::firstOrCreate(
            ['user_id' => $user->id],
            ['balance' => 0, 'is_active' => true]
        );

        return response()->json([
            'success' => true,
            'data' => $wallet,
        ]);
    }

    /**
     * Wallet transactions list
     */
    public function transactions(Request $request)
    {
        $user = Auth::user();

        $transactions = WalletTransaction::where('user_id', $user->id)
            ->latest()
            ->paginate(20);

        return response()->json([
            'success' => true,
            'data' => $transactions,
        ]);
    }

    /**
     * Credit wallet (Top-up)
     */
    public function credit(Request $request)
    {
        $request->validate([
            'amount' => ['required', 'numeric', 'min:1'],
            'pm_type' => ['required', 'in:wallet,stripe'],
        ]);

        $user = Auth::user();

        $wallet = Wallet::firstOrCreate(
            ['user_id' => $user->id],
            ['balance' => 0, 'is_active' => true]
        );

        DB::transaction(function () use ($wallet, $user, $request) {
            WalletTransaction::create([
                'wallet_id' => $wallet->id,
                'user_id'   => $user->id,
                'type'      => 'credit',
                'pm_type'   => $request->pm_type,
                'amount'    => $request->amount,
            ]);
        });

        return response()->json([
            'success' => true,
            'message' => 'Wallet credited successfully',
            'balance' => $wallet->fresh()->balance,
        ]);
    }

    /**
     * Debit wallet (Payment)
     */
    public function debit(Request $request)
    {
        $request->validate([
            'amount' => ['required', 'numeric', 'min:1'],
            'pm_type' => ['required', 'in:wallet'],
        ]);

        $user = Auth::user();

        $wallet = Wallet::where('user_id', $user->id)->firstOrFail();

        if ($wallet->balance < $request->amount) {
            return response()->json([
                'success' => false,
                'message' => 'Insufficient wallet balance',
            ], 422);
        }

        DB::transaction(function () use ($wallet, $user, $request) {
            WalletTransaction::create([
                'wallet_id' => $wallet->id,
                'user_id'   => $user->id,
                'type'      => 'debit',
                'pm_type'   => $request->pm_type,
                'amount'    => $request->amount,
            ]);
        });

        return response()->json([
            'success' => true,
            'message' => 'Wallet debited successfully',
            'balance' => $wallet->fresh()->balance,
        ]);
    }
}
