<?php

namespace routes;

/**
 * Simple pagination helper to attach additional WHERE conditions to paginated queries.
 * It mirrors the usage expected in routes: (new pagination_helper())->where->addCondition(...)
 */
class pagination_helper
{
    public pagination_where $where;

    public function __construct()
    {
        $this->where = new pagination_where();
    }

    /**
     * Render the helper to a SQL snippet string.
     */
    public function toSql(): string
    {
        return $this->where->toSql();
    }
}

/**
 * Condition factory. For now we only need CUSTOM to pass raw SQL, but this can be extended later.
 */
class pagination_condition_where
{
    public static function CUSTOM(string $rawSql): string
    {
        return $rawSql;
    }
}

/**
 * Builder that collects WHERE conditions and can render them as a single SQL snippet.
 */
class pagination_where
{
    /** @var string[] */
    private array $conditions = [];

    public function addCondition(string $condition): self
    {
        $condition = trim($condition);
        if ($condition !== '') {
            $this->conditions[] = $condition;
        }
        return $this;
    }

    /**
     * Return the SQL snippet. If multiple conditions are provided, they are AND'ed together.
     */
    public function toSql(): string
    {
        if (empty($this->conditions)) {
            return '';
        }
        return '(' . implode(' AND ', $this->conditions) . ')';
    }
}
