<?php

declare(strict_types=1);

namespace Numinix\SeekmodoWordPress\Mode;

use Numinix\SeekmodoWordPress\Config\Options;

final class ModeFsm
{
    public const MODE_OFF     = 'off';
    public const MODE_SHADOW  = 'shadow';
    public const MODE_ACTIVE  = 'active';
    public const MODE_ENFORCE = 'enforce';

    public const ALL_MODES = [
        self::MODE_OFF,
        self::MODE_SHADOW,
        self::MODE_ACTIVE,
        self::MODE_ENFORCE,
    ];

    public static function current(): string
    {
        if (defined('SEEKMODO_WP_DISABLE') && SEEKMODO_WP_DISABLE) {
            return self::MODE_OFF;
        }
        $mode = (string) get_option(Options::OPT_MODE, self::MODE_OFF);
        if (!in_array($mode, self::ALL_MODES, true)) {
            return self::MODE_OFF;
        }

        return $mode;
    }

    public static function set(string $mode): bool
    {
        if (!in_array($mode, self::ALL_MODES, true)) {
            return false;
        }

        return update_option(Options::OPT_MODE, $mode);
    }
}
