<?php

declare(strict_types=1);

namespace App\Framework\Database\Criteria\Expression;

use App\Framework\Database\Criteria\Criterion;

/**
 * Simple property comparison expression
 */
final readonly class SimpleExpression implements Criterion
{
    public function __construct(
        public string $property,
        public mixed  $value,
        public string $operator = '='
    ) {
    }

    public function toSql(): string
    {
        if ($this->value === null) {
            return match($this->operator) {
                '=' => "{$this->property} IS NULL",
                '!=' => "{$this->property} IS NOT NULL",
                default => throw new \InvalidArgumentException("Cannot use operator {$this->operator} with NULL value")
            };
        }

        return "{$this->property} {$this->operator} ?";
    }

    public function getParameters(): array
    {
        return $this->value === null ? [] : [$this->value];
    }
}
