<?php

namespace Datalumo;

use Datalumo\Admin\AjaxHandler;
use Datalumo\Admin\SettingsPage;
use Datalumo\Embed\WidgetEmbed;
use Datalumo\Search\SearchInterceptor;
use Datalumo\Sync\BulkSync;
use Datalumo\Sync\ContentSync;
use Datalumo\Update\PluginUpdater;

class Datalumo
{
    private static ?Datalumo $instance = null;

    public static function instance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    private function __construct()
    {
        $this->boot();
    }

    private function boot(): void
    {
        add_action('plugins_loaded', [$this, 'loadTextdomain']);
        add_action('init', [$this, 'init']);

        $this->migrateOptions();

        if (is_admin()) {
            new SettingsPage();
            new AjaxHandler();
        }

        new ContentSync();
        new BulkSync();
        new WidgetEmbed();
        new SearchInterceptor();
        new PluginUpdater();

        register_deactivation_hook(DATALUMO_FILE, [$this, 'deactivate']);
    }

    public function init(): void
    {
        $this->registerAdminNotices();
    }

    public function loadTextdomain(): void
    {
        load_plugin_textdomain(
            'datalumo',
            false,
            dirname(plugin_basename(DATALUMO_FILE)) . '/languages/'
        );
    }

    public function deactivate(): void
    {
        if (function_exists('as_unschedule_all_actions')) {
            as_unschedule_all_actions('datalumo_sync_entry');
            as_unschedule_all_actions('datalumo_process_sync_batch');
            as_unschedule_all_actions('datalumo_delete_sync_entries');
        }
    }

    private function registerAdminNotices(): void
    {
        if (! is_admin()) {
            return;
        }

        add_action('admin_notices', function () {
            if (! current_user_can('manage_options')) {
                return;
            }

            $screen = get_current_screen();

            if (! $screen || ! in_array($screen->id, ['plugins', 'settings_page_datalumo'], true)) {
                return;
            }

            $apiKey = get_option('datalumo_api_key');

            if (empty($apiKey)) {
                $settingsUrl = admin_url('options-general.php?page=datalumo');
                echo '<div class="notice notice-warning"><p>';
                echo wp_kses_post(sprintf(
                    __('Datalumo is not configured. <a href="%s">Go to settings</a> to connect your site.', 'datalumo'),
                    esc_url($settingsUrl)
                ));
                echo '</p></div>';
            }
        });
    }

    private function migrateOptions(): void
    {
        $migrations = [
            'datalumo_chatbot_id'    => 'datalumo_chatbot_integration_id',
            'datalumo_search_box_id' => 'datalumo_search_box_integration_id',
        ];

        foreach ($migrations as $old => $new) {
            $value = get_option($old);

            if ($value !== false && get_option($new) === false) {
                update_option($new, $value);
                delete_option($old);
            }
        }
    }
}
