<?php

namespace RinnovoBooking\Core;

use RinnovoBooking\Core\RinnovoBookingService;

class RinnovoBookingInit {
    private static $RinnovoBookingService;

    public function __construct() {
        self::$RinnovoBookingService = new RinnovoBookingService();

        add_action('upgrader_process_complete', [$this, 'RinnovoBookingUpgrade'],10, 2);
    }
    

    /**
     * Attivazione del plugin.
     * @return void
     */
    public static function RinnovoBookingActivate() {
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        require_once plugin_dir_path(__DIR__) . 'config/tableCreate.php';
        
        self::$RinnovoBookingService = new RinnovoBookingService();
        self::$RinnovoBookingService->scriviLog('sistema', 'Plugin attivato');
    }

    /**
     * Disattivazione del plugin.
     * @return void
     */
    public static function RinnovoBookingDeactivate() {
        self::$RinnovoBookingService = new RinnovoBookingService();
        self::$RinnovoBookingService->scriviLog('sistema', 'Disattivazione plugin');
        self::RinnovoBookingRemoveCron();
    }


    /**
     * Disinstallazione del plugin.
     * @return void
     */
    public static function RinnovoBookingUninstall() {
        self::$RinnovoBookingService = new RinnovoBookingService();
        self::RinnovoBookingRemoveCron();

        // Elimina le tabelle del database
        $tables = [
            RINNOVOBOOKINGPREFIX,
            RINNOVOBOOKINGPREFIX . '_bookings',
            RINNOVOBOOKINGPREFIX . '_log',
            RINNOVOBOOKINGPREFIX . '_sedi',
            RINNOVOBOOKINGPREFIX . '_slots_except',
            RINNOVOBOOKINGPREFIX . '_time_slots',
            RINNOVOBOOKINGPREFIX . '_utenti'
        ];

        global $wpdb;
        foreach ($tables as $table) {
            $wpdb->query("DROP TABLE IF EXISTS `$table`");
        }

        self::$RinnovoBookingService->scriviLog('sistema', 'Disinstallazione plugin');
    }


    /**
     * Rimuove il cron job del plugin.
     * @return void
     */
    public static function RinnovoBookingRemoveCron() {
        wp_clear_scheduled_hook('puc_cron_check_updates-' . RINNOVOBOOKINGPREFIX);
    }


    /**
     * Aggiornamento del plugin.
     * @param mixed $upgrader_object
     * @param mixed $options
     * @return void
     */
    public function RinnovoBookingUpgrade($upgrader_object, $options) {
        $current_plugin_path_name = plugin_basename(__FILE__);
        if ($options['action'] === 'update' && $options['type'] === 'plugin') {
            foreach ($options['plugins'] as $each_plugin) {
                if ($each_plugin === $current_plugin_path_name) {
                    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
                    require_once plugin_dir_path(__FILE__) . 'include/tableCreate.php';

                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_bookings';
                    $miaSql = "ALTER TABLE $table_name ADD COLUMN sede_provenienza text NULL";
                    $wpdb->query($miaSql);
                }
            }
        }

        $this->RinnovoBookingService->scriviLog('sistema', 'Aggiornamento plugin');
    }
}
