<?php

namespace App\Http\Controllers\Api\Wallet;

use App\Http\Controllers\Api\BaseController;
use App\Modules\Wallet\Interfaces\WalletServiceInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class WalletController extends BaseController
{
    public function __construct(
        protected WalletServiceInterface $walletService
    ) {}

    public function show(Request $request): JsonResponse
    {
        $wallet = $this->walletService->getOrCreateWallet($request->user()->id);

        return $this->successResponse([
            'wallet' => $wallet,
        ]);
    }

    public function transactions(Request $request): JsonResponse
    {
        $transactions = $this->walletService->getTransactions(
            $request->user()->id,
            $request->get('per_page', 15),
            $request->get('type')
        );

        return $this->paginatedResponse($transactions);
    }

    public function statement(Request $request): JsonResponse
    {
        $request->validate([
            'from_date' => ['nullable', 'date'],
            'to_date' => ['nullable', 'date', 'after_or_equal:from_date'],
        ]);

        $statement = $this->walletService->getStatement(
            $request->user()->id,
            $request->get('from_date'),
            $request->get('to_date')
        );

        return $this->successResponse([
            'statement' => $statement,
        ]);
    }
}
