<?php

declare(strict_types=1);

namespace Numinix\SeekmodoWordPress\LocalIndex;

use Numinix\SeekmodoWordPress\Bootstrap;
use Numinix\SeekmodoWordPress\Config\Options;

/**
 * Vertical-aware local index configuration (content vs commerce).
 */
final class VerticalConfig
{
    public const OPT_ENABLED          = 'seekmodo_local_index_enabled';
    public const OPT_MODE             = 'seekmodo_local_index_mode';
    public const OPT_CONTENT_ENABLED  = 'seekmodo_local_index_content_enabled';
    public const OPT_COMMERCE_ENABLED = 'seekmodo_local_index_commerce_enabled';
    public const OPT_RELEVANSSI_COMPAT = 'seekmodo_relevanssi_compat_enabled';

    public const MODE_OFF      = 'off';
    public const MODE_FALLBACK = 'fallback';
    public const MODE_PRIMARY  = 'primary';

    public static function registerDefaults(): void
    {
        if (!function_exists('add_option')) {
            return;
        }
        add_option(self::OPT_ENABLED, '1');
        add_option(self::OPT_MODE, self::MODE_FALLBACK);
        add_option(self::OPT_CONTENT_ENABLED, '1');
        add_option(self::OPT_COMMERCE_ENABLED, Bootstrap::wooCommerceActive() ? '1' : '0');
        add_option(self::OPT_RELEVANSSI_COMPAT, '0');
    }

    public static function isEnabled(): bool
    {
        if (function_exists('apply_filters')) {
            $filtered = apply_filters('seekmodo_local_index_enabled', null);
            if ($filtered !== null) {
                return (bool) $filtered;
            }
        }
        $v = get_option(self::OPT_ENABLED, '1');

        return self::truthy($v);
    }

    public static function mode(): string
    {
        $mode = strtolower(trim((string) get_option(self::OPT_MODE, self::MODE_FALLBACK)));
        if (!in_array($mode, [self::MODE_OFF, self::MODE_FALLBACK, self::MODE_PRIMARY], true)) {
            return self::MODE_FALLBACK;
        }

        return $mode;
    }

    public static function contentIndexEnabled(): bool
    {
        if (!self::isEnabled() || self::mode() === self::MODE_OFF) {
            return false;
        }
        $verticals = Options::enabledVerticals();
        if (!isset($verticals['content'])) {
            return false;
        }
        if (function_exists('apply_filters')) {
            $filtered = apply_filters('seekmodo_local_index_content_enabled', null);
            if ($filtered !== null) {
                return (bool) $filtered;
            }
        }

        return self::truthy(get_option(self::OPT_CONTENT_ENABLED, '1'));
    }

    public static function commerceIndexEnabled(): bool
    {
        if (!self::isEnabled() || self::mode() === self::MODE_OFF) {
            return false;
        }
        if (!Bootstrap::wooCommerceActive()) {
            return false;
        }
        $verticals = Options::enabledVerticals();
        if (!isset($verticals['commerce'])) {
            return false;
        }
        if (function_exists('apply_filters')) {
            $filtered = apply_filters('seekmodo_local_index_commerce_enabled', null);
            if ($filtered !== null) {
                return (bool) $filtered;
            }
        }

        return self::truthy(get_option(self::OPT_COMMERCE_ENABLED, '1'));
    }

    public static function relevanssiCompatEnabled(): bool
    {
        if (function_exists('apply_filters')) {
            return (bool) apply_filters('seekmodo_relevanssi_compat_enabled', self::truthy(get_option(self::OPT_RELEVANSSI_COMPAT, '0')));
        }

        return self::truthy(get_option(self::OPT_RELEVANSSI_COMPAT, '0'));
    }

    public static function shouldUseLocalIndex(string $vertical = 'content'): bool
    {
        if (!self::isEnabled() || self::mode() === self::MODE_OFF) {
            return false;
        }
        if ($vertical === 'commerce') {
            return self::commerceIndexEnabled() && Schema::rowCount() > 0;
        }

        return self::contentIndexEnabled() && Schema::rowCount() > 0;
    }

    /**
     * @param mixed $v
     */
    private static function truthy($v): bool
    {
        if (is_bool($v)) {
            return $v;
        }

        return in_array(strtolower((string) $v), ['1', 'true', 'yes', 'on'], true);
    }
}
