<?php

/**
 * @since 2.0.0
 * @package B1_Accounting
 * @subpackage B1_Accounting/includes
 * @author Site.pro <apskaita@site.pro>
 * @link https://www.site.pro
 */
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-b1-accounting.php';
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-b1-accounting-logger.php';
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-b1-accounting-validation-logger.php';

class B1_Accounting_Deactivator extends B1_Accounting
{

    public static function deactivate()
    {
        static::drop_tables();
        static::delete_options();
        wp_clear_scheduled_hook('admin_post_b1_sync_orders');
        wp_clear_scheduled_hook('admin_post_b1_sync_items');
        wp_clear_scheduled_hook('admin_post_b1_clear_logs');
    }

    public static function drop_tables()
    {
        global $wpdb;
        $table = "{$wpdb->prefix}posts";
        $columnName = "b1_reference_id";
        $wpdb->query($wpdb->prepare("ALTER TABLE %i DROP COLUMN %i",$table,$columnName));// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        $wpdb->query(B1_Accounting_Logger::get_drop_sql());
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        $wpdb->query(B1_Accounting_Validation_Logger::get_drop_sql());
    }

    public static function delete_options()
    {
        delete_option('b1-accounting');
    }

}
