<?php

namespace Sibs\Stargate\Models;

if (!defined('ABSPATH')) {
    exit; // Prevent direct access
}

class WordpressOptions extends SibsModels
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        parent::__construct('options', 'option_id', false);
    }

    /**
     * Get Table Name.
     */
    public function getTableName(): string
    {
        return $this->getModulePrefix().$this->table_name;
    }

    /**
     * Get Config Name.
     */
    public function getConfigName(): string
    {
        return '';
    }

    /**
     * Create Model.
     */
    public function create(): bool
    {
        return true;
    }

    /**
     * Migrate Model.
     */
    public function migrate(string $installedVersion): bool
    {
        return true;
    }

    /**
     * Check.
     */
    public function check(bool $suppressLog = false): bool
    {
        $this->setSuppressLog($suppressLog);

        return true;
    }

    /**
     * Get By Option Name.
     *
     * @param int  $option_name
     * @param bool $all
     *
     * @return null|array|object
     */
    public function getByOptionName($option_name, $all = false)
    {
        if ($all) {
            return $this->get_all([
                'option_name' => $option_name,
            ], 'id', 'DESC');
        }

        return $this->get_by('option_name', $option_name);
    }

    /**
     * Get Database Version.
     *
     * @return string
     */
    public function getDatabaseVersion()
    {
        // Query to get the MySQL version
        $db_version = $this->db->get_var('SELECT VERSION() AS version');

        if (!$db_version) {
            return 'Database version could not be retrieved.';
        }

        // Check if it contains "MariaDB"
        if (false !== stripos($db_version, 'mariadb')) {
            return $db_version;
        }

        // Assume it's MySQL if "MariaDB" is not found
        return $db_version;
    }

    /**
     * Get All Plugin Options.
     *
     * @return false|int
     */
    public function deleteAllPluginOptions()
    {
        return $this->delete_all(['option_name' => '%sibs%']);
    }
}
