<?php
if (!defined('ABSPATH')) exit;

/**
 * CAS DB install / upgrade
 *
 * Upgrade-safe rules:
 * - dbDelta runs when CUSTAUSU_DB_VERSION changes.
 * - dbDelta runs when installed package plan changes.
 * - dbDelta runs when an expected table for the current package is missing.
 * - package downgrade never drops tables automatically.
 */

if (!function_exists('custausu_db_get_installed_version')) {
    function custausu_db_get_installed_version(): string {
        $v = get_option(custausu_db_option_version_key(), '');
        return is_string($v) ? $v : '';
    }
}

if (!function_exists('custausu_db_set_installed_version')) {
    function custausu_db_set_installed_version(string $version): void {
        update_option(custausu_db_option_version_key(), (string)$version, false);
    }
}

if (!function_exists('custausu_db_get_installed_package_plan')) {
    function custausu_db_get_installed_package_plan(): string {
        $v = get_option(custausu_db_option_installed_package_plan_key(), '');
        return is_string($v) ? custausu_db_sanitize_package_plan($v) : '';
    }
}

if (!function_exists('custausu_db_set_installed_package_plan')) {
    function custausu_db_set_installed_package_plan(string $plan): void {
        update_option(
            custausu_db_option_installed_package_plan_key(),
            custausu_db_sanitize_package_plan($plan),
            false
        );
    }
}

if (!function_exists('custausu_db_package_plan_changed')) {
    function custausu_db_package_plan_changed(): bool {
        $installed = custausu_db_get_installed_package_plan();
        $current   = custausu_db_current_package_plan();

        if ($installed === '') {
            return true;
        }

        return $installed !== $current;
    }
}

if (!function_exists('custausu_db_needs_upgrade')) {
    function custausu_db_needs_upgrade(): bool {
        $installed_version = custausu_db_get_installed_version();
        $current_version   = custausu_db_version();

        if ($installed_version === '') {
            return true;
        }

        if (version_compare($installed_version, $current_version, '<')) {
            return true;
        }

        if (custausu_db_package_plan_changed()) {
            return true;
        }

        if (!empty(custausu_db_expected_tables_missing())) {
            return true;
        }

        return false;
    }
}

if (!function_exists('custausu_db_install_or_upgrade')) {
    function custausu_db_install_or_upgrade(): void {
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';

        $current_plan = custausu_db_current_package_plan();
        $schemas = custausu_db_schema_sql();

        foreach ($schemas as $key => $sql) {
            dbDelta($sql);

            if (function_exists('custausu_log')) {
                custausu_log('debug', 'CAS dbDelta executed', [
                    'schema'       => (string)$key,
                    'table'        => custausu_db_table((string)$key),
                    'package_plan' => $current_plan,
                    'db_version'   => custausu_db_version(),
                ], 'debug');
            }
        }

        custausu_db_set_installed_version(custausu_db_version());
        custausu_db_set_installed_package_plan($current_plan);

        if (function_exists('custausu_log')) {
            custausu_log('info', 'CAS DB install/upgrade completed', [
                'db_version'      => custausu_db_version(),
                'package_plan'    => $current_plan,
                'schemas_applied' => array_keys($schemas),
            ], 'info');
        }
    }
}

if (!function_exists('custausu_db_maybe_upgrade')) {
    function custausu_db_maybe_upgrade(): void {
        if (!custausu_db_needs_upgrade()) return;

        custausu_db_install_or_upgrade();

        if (function_exists('custausu_log')) {
            custausu_log('info', 'CAS DB upgraded', [
                'installed_version' => custausu_db_get_installed_version(),
                'current_version'   => custausu_db_version(),
                'installed_package' => custausu_db_get_installed_package_plan(),
                'current_package'   => custausu_db_current_package_plan(),
            ], 'info');
        }
    }
}