<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\Controller;
use App\Http\Controllers\User\TransactionController;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TransactionApiController extends Controller
{
    protected $transactionController;

    public function __construct()
    {
        $this->transactionController = new TransactionController();
    }

    /**
     * Get user transactions
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        try {
            $user = auth()->user();

            $transactions = $user->transactions()
                ->orderBy('id', 'DESC')
                ->paginate(site('pagination'));

            return response()->json([
                'status' => 'success',
                'data' => [
                    'transactions' => $transactions
                ]
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to fetch transactions',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get transaction details
     * @param int $id
     * @return JsonResponse
     */
    public function show($id): JsonResponse
    {
        try {
            $user = auth()->user();

            $transaction = $user->transactions()
                ->where('id', $id)
                ->first();

            if (!$transaction) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Transaction not found'
                ], 404);
            }

            return response()->json([
                'status' => 'success',
                'data' => [
                    'transaction' => $transaction
                ]
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to fetch transaction details',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get transaction statistics
     * @return JsonResponse
     */
    public function stats(): JsonResponse
    {
        try {
            $user = auth()->user();

            $totalCredit = $user->transactions()->where('type', 'credit')->sum('amount');
            $totalDebit = $user->transactions()->where('type', 'debit')->sum('amount');
            $totalTransactions = $user->transactions()->count();

            return response()->json([
                'status' => 'success',
                'data' => [
                    'total_credit' => $totalCredit,
                    'total_debit' => $totalDebit,
                    'total_transactions' => $totalTransactions,
                    'net_balance' => $totalCredit - $totalDebit
                ]
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to fetch transaction statistics',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}
