<?php

/**
 * @since 2.0.0
 * @package B1_Accounting
 * @subpackage B1_Accounting/includes
 * @author Site.pro <apskaita@site.pro>
 * @link https://www.site.pro
 */
class B1_Accounting_Activator extends B1_Accounting
{

    /**
     * @since 2.0.0
     */
    public static function activate()
    {
        static::create_tables();
        static::init_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 create_tables()
    {
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        global $wpdb;
        $postsTableName = "{$wpdb->prefix}posts";
        $columnName = "b1_reference_id";

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        $wpdb->query($wpdb->prepare("ALTER TABLE %i ADD COLUMN %i INT NULL DEFAULT NULL",$postsTableName,$columnName));// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange

        $sql = B1_Accounting_Logger::get_init_sql();
        dbDelta( $sql );
        update_option("b1_database_version", 1); // log table
    }

    public static function init_options()
    {
        add_option('b1-accounting', static::get_mappings_defaults());

    }


}
