<?php

defined('APP_BOOTSTRAPPED') or exit('No direct access.');

class TransactionRepository
{
    private $pdo;

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function findByTransactionId($transactionId)
    {
        $stmt = $this->pdo->prepare(
            'SELECT *
             FROM provider_transactions
             WHERE transaction_id = :transaction_id
             LIMIT 1'
        );

        $stmt->execute(array(
            'transaction_id' => (string) $transactionId,
        ));

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        return $row ?: null;
    }

    public function create(array $data)
    {
        $stmt = $this->pdo->prepare(
            'INSERT INTO provider_transactions (
                transaction_id,
                user_id,
                api_provider,
                transaction_type,
                amount,
                status,
                provider_reference,
                request_payload,
                response_payload,
                created_at,
                updated_at
            ) VALUES (
                :transaction_id,
                :user_id,
                :api_provider,
                :transaction_type,
                :amount,
                :status,
                :provider_reference,
                :request_payload,
                :response_payload,
                NOW(),
                NOW()
            )'
        );

        return $stmt->execute(array(
            'transaction_id' => $data['transaction_id'],
            'user_id' => (int) $data['user_id'],
            'api_provider' => $data['api_provider'],
            'transaction_type' => $data['transaction_type'],
            'amount' => number_format((float) $data['amount'], 2, '.', ''),
            'status' => $data['status'],
            'provider_reference' => isset($data['provider_reference'])
                ? $data['provider_reference']
                : null,
            'request_payload' => isset($data['request_payload'])
                ? $data['request_payload']
                : null,
            'response_payload' => isset($data['response_payload'])
                ? $data['response_payload']
                : null,
        ));
    }

    public function updateStatus(
        $transactionId,
        $status,
        $providerReference = null,
        $responsePayload = null
    ) {
        $stmt = $this->pdo->prepare(
            'UPDATE provider_transactions
             SET
                status = :status,
                provider_reference = :provider_reference,
                response_payload = :response_payload,
                updated_at = NOW()
             WHERE transaction_id = :transaction_id'
        );

        return $stmt->execute(array(
            'status' => (string) $status,
            'provider_reference' => $providerReference,
            'response_payload' => $responsePayload,
            'transaction_id' => (string) $transactionId,
        ));
    }

    public function getByUserId($userId, $limit = 100)
    {
        $limit = max(1, min(500, (int) $limit));

        $stmt = $this->pdo->prepare(
            'SELECT *
             FROM provider_transactions
             WHERE user_id = :user_id
             ORDER BY id DESC
             LIMIT ' . $limit
        );

        $stmt->execute(array('user_id' => (int) $userId));

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
