<?php

namespace Models;

class WalletModel
{
    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 getPrimaryWalletByCompanyId(int $companyId): ?array
    {
        $stmt = $this->pdo->prepare(
            'SELECT wallet_id, wallet_address, wallet_public_key, wallet_flag
             FROM "wallet"
             WHERE company_id = :company_id
             ORDER BY wallet_id ASC
             LIMIT 1'
        );

        $stmt->execute(['company_id' => $companyId]);
        $wallet = $stmt->fetch(\PDO::FETCH_ASSOC);

        return $wallet ?: null;
    }
}
