<?php

namespace Models;

class PaymentModel
{
    private \PDO $pdo;

    public function __construct()
    {
        if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
            $this->pdo = $GLOBALS['pdo'];
            return;
        }

        throw new \RuntimeException('Global PDO connection not initialized');
    }

    public function create(
        string $paymentExternalId,
        int $statusId,
        int $userId,
        string $paymentTs = '',
        string $paymentE2e = '',
        string $paymentFlag = ''
    ): int {
        $stmt = $this->pdo->prepare(
            'INSERT INTO "payment" (
                payment_external_id,
                status_id,
                user_id,
                payment_ts,
                payment_e2e,
                payment_flag
            ) VALUES (
                :external_id,
                :status_id,
                :user_id,
                :payment_ts,
                :payment_e2e,
                :payment_flag
            ) RETURNING payment_id'
        );

        $stmt->execute([
            'external_id' => $paymentExternalId,
            'status_id' => $statusId,
            'user_id' => $userId,
            'payment_ts' => $paymentTs,
            'payment_e2e' => $paymentE2e,
            'payment_flag' => $paymentFlag,
        ]);

        return (int)$stmt->fetchColumn();
    }
}
