<?php

namespace EasyLinkAutomations\Providers;

if (!defined('ABSPATH')) {
    exit;
}

use EasyLinkAutomations\Config;
use EasyLinkAutomations\Hooks\Hooks;
use EasyLinkAutomations\Installer;

/**
 * Handles plugin activation, deactivation, and uninstallation.
 *
 * Migrations run on activation and are cleaned up on uninstall.
 * Scheduled events are cleared on deactivation.
 */
final class InstallerProvider
{
    private string $activateHook;

    private string $deactivateHook;

    private static string $uninstallHook;

    public function __construct()
    {
        $mainFile = Config::get('MAIN_FILE');

        register_activation_hook($mainFile, [$this, 'onActivate']);
        register_deactivation_hook($mainFile, [$this, 'onDeactivate']);

        $this->activateHook   = Config::withPrefix('activate');
        $this->deactivateHook = Config::withPrefix('deactivate');
        self::$uninstallHook  = Config::withPrefix('uninstall');

        Hooks::addAction($this->deactivateHook, [$this, 'deactivate']);
        register_uninstall_hook($mainFile, [self::class, 'onUninstall']);
    }

    public function register(): void
    {
        $installer = new Installer(
            [
                'php'        => Config::REQUIRED_PHP_VERSION,
                'wp'         => Config::REQUIRED_WP_VERSION,
                'version'    => Config::VERSION,
                'oldVersion' => Config::getOption('version', '0.0'),
                'multisite'  => true,
                'basename'   => Config::get('BASENAME'),
            ],
            [
                'activate'  => $this->activateHook,
                'uninstall' => self::$uninstallHook,
            ],
            [
                'migration' => self::migration(),
                'drop'      => self::drop(),
            ]
        );

        $installer->register();
    }

    public function deactivate(): void
    {
        // Clear any scheduled cron events
        wp_clear_scheduled_hook(Config::withPrefix('cron_cleanup'));
        flush_rewrite_rules();
    }

    public function onActivate(bool $networkWide): void
    {
        Hooks::doAction($this->activateHook, $networkWide);
    }

    public function onDeactivate(bool $networkWide): void
    {
        Hooks::doAction($this->deactivateHook, $networkWide);
    }

    public static function onUninstall(bool $networkWide = false): void
    {
        Hooks::doAction(self::$uninstallHook, $networkWide);
    }

    /**
     * Migration classes to run on activation / upgrade.
     */
    public static function migration(): array
    {
        return [
            'path'       => Config::get('BASEDIR')
                . DIRECTORY_SEPARATOR . 'db'
                . DIRECTORY_SEPARATOR . 'Migrations'
                . DIRECTORY_SEPARATOR,
            'migrations' => [
                'EasyLinkAutomationsAutomationsTableMigration',
                'EasyLinkAutomationsAutomationActionsTableMigration',
                'EasyLinkAutomationsConnectionsTableMigration',
                'EasyLinkAutomationsExecutionLogsTableMigration',
                'EasyLinkAutomationsPluginOptions',
            ],
        ];
    }

    /**
     * Tables to drop on uninstall (reverse order of creation).
     */
    public static function drop(): array
    {
        return [
            'path'       => Config::get('BASEDIR')
                . DIRECTORY_SEPARATOR . 'db'
                . DIRECTORY_SEPARATOR . 'Migrations'
                . DIRECTORY_SEPARATOR,
            'migrations' => [
                'EasyLinkAutomationsExecutionLogsTableMigration',
                'EasyLinkAutomationsAutomationActionsTableMigration',
                'EasyLinkAutomationsConnectionsTableMigration',
                'EasyLinkAutomationsAutomationsTableMigration',
                'EasyLinkAutomationsPluginOptions',
            ],
        ];
    }
}
