<?php

defined('ABSPATH') || exit;

$bookwellab_old_version = \BookWellAB\BookWellAB_Database::get_db_store_option('db_version', '0.0.0');
$bookwellab_db = new \BookWellAB\BookWellAB_Database();

if (version_compare((string) $bookwellab_old_version, '1.0.0', '<')) {
    $bookwellab_db->install_tables();
}

if (version_compare((string) $bookwellab_old_version, '1.0.1', '<')) {
    $bookwellab_db->install_spam_tokens_table();
    \BookWellAB\BookWellAB_Frontend::purge_legacy_spam_transients();
}

if (version_compare((string) $bookwellab_old_version, '1.0.2', '<')) {
    $bookwellab_db->ensure_appointment_customer_index();
}


if (version_compare((string) $bookwellab_old_version, '2.0.0', '<')) {
    $bookwellab_db->add_missing_columns();
    $bookwellab_db->install_payment_debug_logs_table();
    $bookwellab_db->ensure_appearance_theme_setting();
    $bookwellab_db->ensure_manually_payment_settings();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.1', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.2', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.3', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.4', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.5', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.6', '<')) {
    $bookwellab_db->ensure_form_fields_table_and_data();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '2.0.9', '<')) {
    $bookwellab_db->add_missing_columns();
    \BookWellAB\BookWellAB_Customer_Account::ensure_customer_role();
    $bookwellab_db->ensure_customer_panel_support();
    \BookWellAB\BookWellAB_Default_Pages::maybe_create_default_pages();    
    $bookwellab_db->ensure_service_image_id_column();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.0', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.1', '<')) {
    $bookwellab_db->ensure_appearance_form_options();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.2', '<')) {
    $bookwellab_db->ensure_zoom_support();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.3', '<')) {
    $bookwellab_db->ensure_zoom_and_debug_log_support();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.5', '<')) {
    $bookwellab_db->ensure_week_starts_on_setting();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.6', '<')) {
    $bookwellab_db->ensure_form_fields_table_and_data();
    $bookwellab_db->ensure_form_field_width_column();
    $bookwellab_db->maybe_seed_default_form_field_widths();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.7', '<')) {
    // Idempotent schema/settings checks for fresh installs that skipped earlier migration steps.
    $bookwellab_db->ensure_form_fields_table_and_data();
    $bookwellab_db->ensure_form_field_width_column();
    $bookwellab_db->maybe_seed_default_form_field_widths();
    $bookwellab_db->ensure_week_starts_on_setting();
    $bookwellab_db->ensure_zoom_and_debug_log_support();
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

if (version_compare((string) $bookwellab_old_version, '3.0.8', '<')) {
    \BookWellAB\BookWellAB_Frontend::flush_upgrade_caches();
}

\BookWellAB\BookWellAB_Database::update_db_store_option('db_version', BOOKWELLAB_VERSION);
\BookWellAB\BookWellAB_Database::update_db_store_option('updated_date_' . BOOKWELLAB_VERSION, current_time('mysql'));
