<?php

declare(strict_types=1);

namespace KupShop\CatalogBundle\Enum;

enum OrderDirection
{
    case ASC;
    case DESC;

    public function uppercase(): string
    {
        return $this->name;
    }

    public function lowercase(): string
    {
        return strtolower($this->name);
    }

    public static function from(int|string $value): self
    {
        return match (is_string($value) ? strtoupper($value) : $value) {
            'DESC', 2 => self::DESC,
            'ASC', 1 => self::ASC,
            default => throw new \InvalidArgumentException('Invalid order direction'),
        };
    }

    public static function tryFrom(int|string $value): ?self
    {
        if (is_string($value) && is_numeric($value)) {
            $value = (int) $value;
        }

        try {
            return self::from($value);
        } catch (\InvalidArgumentException) {
            return null;
        }
    }
}
