<?php

declare(strict_types=1);

namespace App\Framework\Database\Criteria\Projection;

use App\Framework\Database\Criteria\Projection;

/**
 * Aggregate function projection (COUNT, SUM, AVG, etc.)
 */
final class AggregateProjection implements Projection
{
    public function __construct(
        public readonly string $function,
        public readonly string $property,
        public readonly ?string $alias = null
    ) {
    }

    public function toSql(): string
    {
        $sql = "{$this->function}({$this->property})";

        return $this->alias ? "{$sql} AS {$this->alias}" : $sql;
    }

    public function getAliases(): array
    {
        return $this->alias ? [$this->alias => "{$this->function}({$this->property})"] : [];
    }
}
