<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Admin;

use APD\Container;
use APD\Contracts\ServiceProviderInterface;
use APD\Meta\ProductConfigMeta;
use APD\Meta\TemplateMeta;
use APD\Meta\UserDesignMeta;
use APD\PostTypes\ProductConfigPostType;
use APD\PostTypes\TemplatePostType;
use APD\PostTypes\ThemePostType;
use APD\PostTypes\UserDesignPostType;
use APD\Support\AssetCatalog;
use APD\Support\CommercialRuntimeConfig;
use APD\Support\ModelUploads;
use APD\Support\QADiagnostics;
use APD\Support\SvgUploads;

defined('ABSPATH') || exit;

class AdminServiceProvider implements ServiceProviderInterface
{
    public function __construct(protected Container $container)
    {
    }

    public function register() : void
    {
        $this->container->set(CommercialRuntimeConfig::class, fn() => new CommercialRuntimeConfig());
        $this->container->set(SupportPage::class, fn() => new SupportPage($this->container->get(CommercialRuntimeConfig::class)));
        $this->container->set(AdminMenu::class, fn() => new AdminMenu($this->container->get(SupportPage::class)));
        $this->container->set(AdminAssets::class, fn() => new AdminAssets());
        $this->container->set(AdminSettings::class, fn() => new AdminSettings());
        $this->container->set(ProductConfigEditor::class, fn() => new ProductConfigEditor());
        $this->container->set(ThemePage::class, fn() => new ThemePage());
        $this->container->set(ToolsController::class, fn() => new ToolsController());
        $this->container->set(DesignerStudioPage::class, fn() => new DesignerStudioPage());
        $this->container->set(AssetCatalog::class, fn() => new AssetCatalog());
        $this->container->set(ModelUploads::class, fn() => new ModelUploads());
        $this->container->set(SvgUploads::class, fn() => new SvgUploads());
        $this->container->set(CatalogCrudController::class, fn() => new CatalogCrudController());
        $this->container->set(GoogleFontsCatalogController::class, fn() => new GoogleFontsCatalogController());
        $this->container->set(OrderProductionPage::class, fn() => new OrderProductionPage());
        $this->container->set(QADiagnostics::class, fn() => new QADiagnostics());
        $this->container->set(QADiagnosticsPage::class, fn() => new QADiagnosticsPage());
        $this->container->set(TranslationsPage::class, fn() => new TranslationsPage());
        $this->container->set(PluginLifecycle::class, fn() => new PluginLifecycle());
    }

    public function boot() : void
    {
        add_action('init', [new ThemePostType(), 'register'], 1);
        add_action('init', [new ProductConfigPostType(), 'register']);
        add_action('init', [new ProductConfigMeta(), 'register']);
        add_action('init', [new TemplatePostType(), 'register']);
        add_action('init', [new TemplateMeta(), 'register']);
        add_action('init', [new UserDesignPostType(), 'register']);
        add_action('init', [new UserDesignMeta(), 'register']);
        $this->container->get(ModelUploads::class)->register();
        $this->container->get(SvgUploads::class)->register();

        if (! is_admin()) {
            return;
        }

        add_action('admin_init', [$this->container->get(AdminSettings::class), 'register']);
        add_action('admin_init', [$this->container->get(PluginLifecycle::class), 'maybeRedirectAfterActivation'], 1);
        add_filter('plugin_action_links_' . plugin_basename(ASUKA_PRODUCT_DESIGNER_FILE), [$this->container->get(PluginLifecycle::class), 'actionLinks']);
        add_filter('plugin_row_meta', [$this->container->get(PluginLifecycle::class), 'rowMeta'], 10, 2);
        add_action('admin_init', [$this->container->get(AssetCatalog::class), 'register']);
        add_action('admin_init', [$this->container->get(CatalogCrudController::class), 'handleQueryAction']);
        add_action('admin_menu', [$this->container->get(AdminMenu::class), 'register']);
        add_action('admin_enqueue_scripts', [$this->container->get(AdminAssets::class), 'enqueue']);
        add_filter('admin_body_class', [$this->container->get(AdminAssets::class), 'adminBodyClass']);
        add_action('admin_notices', [$this->container->get(ProductConfigEditor::class), 'renderAdminNotices']);
        add_action('admin_notices', [$this->container->get(DesignerStudioPage::class), 'renderAdminNotices']);
        add_action('add_meta_boxes', [$this->container->get(ProductConfigEditor::class), 'registerMetaBoxes']);
        add_action('add_meta_boxes_' . ThemePostType::TYPE, [$this->container->get(ThemePage::class), 'registerMetaBoxes']);
        add_action('save_post_' . ProductConfigPostType::TYPE, [$this->container->get(ProductConfigEditor::class), 'save']);
        add_action('save_post_' . TemplatePostType::TYPE, [$this->container->get(ProductConfigEditor::class), 'save']);
        add_action('save_post_' . ThemePostType::TYPE, [$this->container->get(ThemePage::class), 'savePost'], 10, 3);
        add_filter('manage_edit-' . ProductConfigPostType::TYPE . '_columns', [$this->container->get(ProductConfigEditor::class), 'columns']);
        add_action('manage_' . ProductConfigPostType::TYPE . '_posts_custom_column', [$this->container->get(ProductConfigEditor::class), 'renderColumn'], 10, 2);
        add_filter('manage_edit-' . ThemePostType::TYPE . '_columns', [$this->container->get(ThemePage::class), 'columns']);
        add_action('manage_' . ThemePostType::TYPE . '_posts_custom_column', [$this->container->get(ThemePage::class), 'renderColumn'], 10, 2);
        add_filter('post_row_actions', [$this->container->get(ThemePage::class), 'rowActions'], 10, 2);
        add_action('wp_trash_post', [$this->container->get(ThemePage::class), 'guardTrash']);
        add_action('admin_post_apd_export_config', [$this->container->get(ToolsController::class), 'exportConfig']);
        add_action('admin_post_apd_export_product_package', [$this->container->get(ToolsController::class), 'exportProductPackage']);
        add_action('admin_post_apd_inspect_product_package', [$this->container->get(ToolsController::class), 'inspectProductPackage']);
        add_action('admin_post_apd_import_product_package', [$this->container->get(ToolsController::class), 'importProductPackage']);
        add_action('admin_post_apd_discard_product_package', [$this->container->get(ToolsController::class), 'discardProductPackage']);
        add_action('admin_post_apd_save_catalog_item', [$this->container->get(CatalogCrudController::class), 'save']);
        add_action('admin_post_apd_delete_catalog_item', [$this->container->get(CatalogCrudController::class), 'delete']);
        add_action('admin_post_apd_duplicate_catalog_item', [$this->container->get(CatalogCrudController::class), 'duplicate']);
        add_action('admin_post_apd_move_catalog_item', [$this->container->get(CatalogCrudController::class), 'move']);
        add_action('admin_post_apd_import_catalog_items', [$this->container->get(CatalogCrudController::class), 'import']);
        add_action('admin_post_apd_import_catalog_media', [$this->container->get(CatalogCrudController::class), 'importMedia']);
        add_action('admin_post_apd_export_catalog_items', [$this->container->get(CatalogCrudController::class), 'export']);
        add_action('wp_ajax_apd_google_fonts_catalog', [$this->container->get(GoogleFontsCatalogController::class), 'fetch']);
        add_action('admin_post_apd_import_config', [$this->container->get(ToolsController::class), 'importConfig']);
        add_action('admin_post_apd_save_studio_config', [$this->container->get(DesignerStudioPage::class), 'save']);
        add_action('admin_post_apd_export_production_pack', [$this->container->get(OrderProductionPage::class), 'export']);
        add_action('admin_post_apd_update_production_status', [$this->container->get(OrderProductionPage::class), 'updateStatus']);
        add_action('admin_post_apd_save_production_notes', [$this->container->get(OrderProductionPage::class), 'updateNotes']);
        add_action('admin_post_apd_save_qa_report', [$this->container->get(QADiagnosticsPage::class), 'save']);
        add_action('admin_post_apd_reset_qa_report', [$this->container->get(QADiagnosticsPage::class), 'reset']);
        add_action('admin_post_apd_export_qa_report', [$this->container->get(QADiagnosticsPage::class), 'export']);
        add_action('admin_post_apd_create_js_translation_locale', [$this->container->get(TranslationsPage::class), 'createLocale']);
        add_action('admin_post_apd_save_js_translations', [$this->container->get(TranslationsPage::class), 'save']);
        add_action('admin_post_apd_delete_js_translation_locale', [$this->container->get(TranslationsPage::class), 'deleteLocale']);
        add_action('admin_post_apd_export_js_translation_locale', [$this->container->get(TranslationsPage::class), 'exportLocale']);
        add_action('admin_post_apd_import_js_translation_locale', [$this->container->get(TranslationsPage::class), 'importLocale']);
        add_action('admin_post_apd_save_plugin_translations', [$this->container->get(TranslationsPage::class), 'savePluginTranslations']);
        add_action('admin_post_apd_export_plugin_translation_locale', [$this->container->get(TranslationsPage::class), 'exportPluginLocale']);
        add_action('admin_post_apd_import_plugin_translation_locale', [$this->container->get(TranslationsPage::class), 'importPluginLocale']);
        add_action('admin_post_apd_open_support_center', [$this->container->get(SupportPage::class), 'open']);
    }
}
