<?php

namespace Sibs\Stargate\Utils;

use Sibs\Stargate\Config\SibsConfig;
use Sibs\Stargate\Constants\Languages;
use Sibs\Stargate\Constants\Plugin;
use Sibs\Stargate\Models\SibsGeneralModels;

defined('ABSPATH') || exit;

class SibsVersion
{
    /**
     * Verify if is a new instalation.
     *
     * @return bool
     */
    public static function is_new_instalation()
    {
        return empty(SibsConfig::getPluginOption('db_version'));
    }

    /**
     * Verify if it's install an upgrade.
     *
     * @return bool
     */
    public static function is_upgrading()
    {
        if (self::is_new_instalation()) {
            return false;
        }

        return version_compare(SibsConfig::getPluginOption('db_version'), constant('SIBS_VERSION'), 'lt');
    }

    /**
     * Verify if it's install the same version.
     *
     * @return bool
     */
    public static function is_same_version()
    {
        if (self::is_new_instalation()) {
            return false;
        }

        return version_compare(SibsConfig::getPluginOption('db_version'), constant('SIBS_VERSION'), 'eq');
    }

    /**
     * Verify if it's install an downgrade.
     *
     * @return bool
     */
    public static function is_dowgrading()
    {
        if (self::is_new_instalation()) {
            return false;
        }

        return version_compare(SibsConfig::getPluginOption('db_version'), constant('SIBS_VERSION'), 'gt');
    }

    /**
     * Need to migrate DB.
     *
     * @return bool
     */
    public static function need_to_migrate_db()
    {
        return self::is_new_instalation() || self::is_upgrading();
    }

    /**
     * Need to seed DB.
     *
     * @param string $reference_version
     *
     * @return bool
     */
    public static function need_to_seed_db($reference_version)
    {
        if (self::is_new_instalation()) {
            return false;
        }

        return version_compare(SibsConfig::getPluginOption('db_version'), $reference_version, 'lt')
            && version_compare(constant('SIBS_VERSION'), $reference_version, 'ge');
    }

    /**
     * Fix Sibs version.
     *
     * If has Plugin version but not DB version, this will fix it
     * If has DB version but not Plugin version, this will fix it
     */
    public static function fix_version_options()
    {
        if (!empty(SibsConfig::getPluginOption('plugin_version')) && empty(SibsConfig::getPluginOption('db_version'))) {
            SibsConfig::setPluginOption('db_version', SibsConfig::getPluginOption('plugin_version'));
        }

        if (!empty(SibsConfig::getPluginOption('db_version')) && empty(SibsConfig::getPluginOption('plugin_version'))) {
            SibsConfig::setPluginOption('plugin_version', SibsConfig::getPluginOption('db_version'));
        }
    }

    /**
     * Update Sibs version.
     */
    public static function update_sibs_version()
    {
        if (self::is_new_instalation() || self::is_upgrading()) {
            SibsConfig::setPluginOption('plugin_version', constant('SIBS_VERSION'));
            SibsConfig::setPluginOption('db_version', constant('SIBS_VERSION'));
        } elseif (self::is_dowgrading()) {
            SibsConfig::setPluginOption('plugin_version', constant('SIBS_VERSION'));
        }
    }

    /**
     * Default Language.
     */
    public static function sibs_default_language()
    {
        if (self::is_new_instalation() || self::is_upgrading() || !empty(SibsConfig::getPluginOption('sibs_default_language'))) {
            SibsConfig::setPluginOption('sibs_default_language', Languages::default());
        }
    }

    /**
     * SIBS: Is Order from SIBS.
     *
     * @return bool
     */
    public static function sibs_is_order_from_sibs(\WC_Order $wc_order)
    {
        $transaction = SibsGeneralModels::get_transaction($wc_order);

        return isset($transaction['payment_id']) ? self::sibs_is_payment_from_sibs($transaction['payment_id']) : false;
    }

    /**
     * SIBS: Is Payment Method from SIBS.
     *
     * @return bool
     */
    private static function sibs_is_payment_from_sibs(string $payment_method)
    {
        if (str_contains($payment_method, Plugin::PLUGIN_PREFIX)) {
            return true;
        }

        return false;
    }
}
