<?php

declare(strict_types=1);

namespace apexl\Io\modules\tabulator\Enums;

use apexl\Io\exceptions\TabulatorFilterConditionCannotBeConvertedToWhereException;
use apexl\Vault\enums\WhereCondition;
use JsonSerializable;

enum TabulatorFilterCondition: string implements JsonSerializable
{
    case BEATING_HEART = 'beating_heart';
    case BETWEEN = 'between';
    case CREATOR = 'creator';
    case EXACT = '=';
    case GREATER_THAN_OR_EQUAL = '>=';
    case LESS_THAN_OR_EQUAL = '<=';
    case LIKE = 'like';
    case NOT_EQUAL = '!=';
    case IN = 'in';

    public function jsonSerialize(): string
    {
        return $this->value;
    }

    /**
     * @throws TabulatorFilterConditionCannotBeConvertedToWhereException
     */
    public function toWhereCondition(): WhereCondition
    {
        return match ($this) {
            self::EXACT, self::CREATOR => WhereCondition::EQUALS,
            self::LIKE => WhereCondition::LIKE,
            self::BETWEEN => WhereCondition::BETWEEN,
            self::GREATER_THAN_OR_EQUAL => WhereCondition::GREATER_THAN_OR_EQUAL,
            self::LESS_THAN_OR_EQUAL => WhereCondition::LESS_THAN_OR_EQUAL,
            self::NOT_EQUAL => WhereCondition::NOT_EQUALS,
            self::IN => WhereCondition::IN,
            default => throw new TabulatorFilterConditionCannotBeConvertedToWhereException()
        };
    }
}
