<?php
namespace Bot\Services;

use Bot\Database;
use PDO;

class FreeGoodsService
{
    private PDO $pdo;

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

    /**
     * @return array<int,array<string,mixed>>
     */
    public function listActive(): array
    {
        $stmt = $this->pdo->query(
            "SELECT * FROM free_goods WHERE status = 'active' ORDER BY sort_order DESC, id DESC"
        );
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public function listAll(): array
    {
        $stmt = $this->pdo->query(
            "SELECT * FROM free_goods ORDER BY sort_order DESC, id DESC"
        );
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function create(
        string $title,
        string $description,
        ?string $buttonLabel,
        ?string $buttonUrl,
        ?string $parseMode = null,
        string $status = 'inactive',
        int $sortOrder = 0
    ): int {
        $stmt = $this->pdo->prepare(
            "INSERT INTO free_goods (title, description, button_label, button_url, parse_mode, status, sort_order, created_at)
             VALUES (:title, :description, :button_label, :button_url, :parse_mode, :status, :sort_order, NOW())"
        );
        $stmt->execute([
            ':title'        => $title,
            ':description'  => $description,
            ':button_label' => $buttonLabel,
            ':button_url'   => $buttonUrl,
            ':parse_mode'   => $parseMode,
            ':status'       => $status === 'active' ? 'active' : 'inactive',
            ':sort_order'   => $sortOrder,
        ]);
        return (int) $this->pdo->lastInsertId();
    }

    public function updateField(int $id, string $field, $value): bool
    {
        $allowed = [
            'title', 'description', 'button_label', 'button_url', 'sort_order', 'parse_mode',
        ];
        if (!in_array($field, $allowed, true)) {
            return false;
        }
        $value = $field === 'sort_order' ? (int) $value : $value;
        if ($field === 'parse_mode' && $value === '') {
            $value = null;
        }
        $stmt = $this->pdo->prepare(
            "UPDATE free_goods SET {$field} = :value, updated_at = NOW() WHERE id = :id"
        );
        $stmt->execute([
            ':value' => $value,
            ':id'    => $id,
        ]);
        return $stmt->rowCount() > 0;
    }

    public function setStatus(int $id, string $status): bool
    {
        if (!in_array($status, ['active', 'inactive'], true)) {
            return false;
        }
        $stmt = $this->pdo->prepare(
            "UPDATE free_goods SET status = :status, updated_at = NOW() WHERE id = :id"
        );
        $stmt->execute([
            ':status' => $status,
            ':id'     => $id,
        ]);
        return $stmt->rowCount() > 0;
    }

    public function delete(int $id): bool
    {
        $stmt = $this->pdo->prepare('DELETE FROM free_goods WHERE id = ?');
        $stmt->execute([$id]);
        return $stmt->rowCount() > 0;
    }
}
