<?php
namespace Bot\Services;

use Bot\Database;
use PDO;

class PaymentMethodService
{
    private PDO $pdo;

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

    /**
     * @return array<int,array<string,mixed>>
     */
    public function getActive(?string $direction = null): array
    {
        $sql = 'SELECT id, name, instructions, contact_details, direction, details_prompt FROM payment_methods WHERE is_active = 1';
        $params = [];
        if ($direction !== null) {
            $sql .= ' AND direction = ?';
            $params[] = $direction === 'sell' ? 'sell' : 'buy';
        }
        $sql .= ' ORDER BY priority DESC, name ASC';
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function getAll(): array
    {
        $stmt = $this->pdo->query(
            'SELECT id, name, instructions, contact_details, direction, details_prompt, is_active, priority, created_at
             FROM payment_methods ORDER BY priority DESC, name ASC'
        );
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getById(int $id): ?array
    {
        $stmt = $this->pdo->prepare('SELECT * FROM payment_methods WHERE id = ?');
        $stmt->execute([$id]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ?: null;
    }

    public function create(
        string $name,
        string $instructions,
        string $contactDetails = '',
        string $direction = 'buy',
        string $detailsPrompt = '',
        int $priority = 0
    ): int
    {
        $stmt = $this->pdo->prepare(
            'INSERT INTO payment_methods (name, instructions, contact_details, direction, details_prompt, priority, created_at)
             VALUES (?, ?, ?, ?, ?, ?, NOW())'
        );
        $stmt->execute([
            $name,
            $instructions,
            $contactDetails,
            $direction === 'sell' ? 'sell' : 'buy',
            $detailsPrompt !== '' ? $detailsPrompt : null,
            $priority,
        ]);
        return (int) $this->pdo->lastInsertId();
    }

    public function update(
        int $id,
        string $instructions,
        ?string $contactDetails = null,
        ?string $direction = null,
        ?string $detailsPrompt = null
    ): void
    {
        $setFragments = ['instructions = ?'];
        $params = [$instructions];
        if ($contactDetails !== null) {
            $setFragments[] = 'contact_details = ?';
            $params[] = $contactDetails;
        }
        if ($direction !== null) {
            $setFragments[] = 'direction = ?';
            $params[] = $direction === 'sell' ? 'sell' : 'buy';
        }
        if ($detailsPrompt !== null) {
            $setFragments[] = 'details_prompt = ?';
            $params[] = $detailsPrompt !== '' ? $detailsPrompt : null;
        }
        $params[] = $id;
        $stmt = $this->pdo->prepare(
            'UPDATE payment_methods SET ' . implode(', ', $setFragments) . ', updated_at = NOW() WHERE id = ?'
        );
        $stmt->execute($params);
    }

    public function setActive(int $id, bool $active): void
    {
        $stmt = $this->pdo->prepare('UPDATE payment_methods SET is_active = ?, updated_at = NOW() WHERE id = ?');
        $stmt->execute([$active ? 1 : 0, $id]);
    }

    public function delete(int $id): void
    {
        $stmt = $this->pdo->prepare('DELETE FROM payment_methods WHERE id = ?');
        $stmt->execute([$id]);
    }
}
