<?php
namespace Bot\Services;

use Bot\Database;
use PDO;

class AdminUserService
{
    private PDO $pdo;

    public function __construct()
    {
        $this->pdo = Database::connection();
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function search(string $term, int $limit = 10): array
    {
        $like = '%' . $term . '%';
        $stmt = $this->pdo->prepare(
            'SELECT id, telegram_id, username, first_name, kyc_status, is_banned
             FROM users
             WHERE username LIKE :q OR first_name LIKE :q OR CAST(telegram_id AS CHAR) LIKE :q
             ORDER BY id ASC
             LIMIT :limit'
        );
        $stmt->bindValue(':q', $like, PDO::PARAM_STR);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function listAll(int $limit = 50, int $offset = 0): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT id, telegram_id, username, first_name, kyc_status, is_banned, created_at
             FROM users
             ORDER BY id ASC
             LIMIT :limit OFFSET :offset'
        );
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function findByIdentifier(string $identifier): ?array
    {
        if (ctype_digit($identifier)) {
            $stmt = $this->pdo->prepare('SELECT * FROM users WHERE id = ? OR telegram_id = ?');
            $stmt->execute([(int) $identifier, (int) $identifier]);
        } else {
            $stmt = $this->pdo->prepare('SELECT * FROM users WHERE username = ?');
            $stmt->execute([$identifier]);
        }
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ?: null;
    }

    public function setBanStatus(int $userId, bool $banned, ?string $reason = null): void
    {
        $stmt = $this->pdo->prepare(
            'UPDATE users SET is_banned = ?, ban_reason = ?, banned_at = IF(? = 1, NOW(), NULL) WHERE id = ?'
        );
        $stmt->execute([$banned ? 1 : 0, $reason, $banned ? 1 : 0, $userId]);
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function listOrders(int $userId, int $limit = 5): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT id, type, currency, amount, status, payment_status, created_at
             FROM orders WHERE user_id = ? ORDER BY id DESC LIMIT ?'
        );
        $stmt->bindValue(1, $userId, PDO::PARAM_INT);
        $stmt->bindValue(2, $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function listWithdrawals(int $userId, int $limit = 5): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT id, currency, amount, status, review_status, network, to_address, created_at
             FROM withdrawals WHERE user_id = ? ORDER BY id DESC LIMIT ?'
        );
        $stmt->bindValue(1, $userId, PDO::PARAM_INT);
        $stmt->bindValue(2, $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
