<?php

namespace Formatrica\Admin;

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

use Formatrica\Capability\Feature_Gate;
use Formatrica\Setup\Service_Locator;
use Formatrica\Setup\Service_Provider;
use Formatrica\Admin\Export_Controller;
use Formatrica\Admin\Duplicate_Controller;
use Formatrica\Admin\Delete_Controller;
use Formatrica\Admin\Activate_Controller;

final class Admin_Service_Provider implements Service_Provider
{
    public function register(Service_Locator $services): void
    {
        if (!is_admin()) {
            return;
        }

        $gate = $services->has('feature_gate') && $services->get('feature_gate') instanceof Feature_Gate
            ? $services->get('feature_gate')
            : null;

        $menu = new Menu($gate);
        add_action('admin_menu', [$menu, 'register_menu']);

        $notices = new Notices();
        $notices->boot();

        $exporter = new Export_Controller();
        add_action('admin_post_formatrica_export', [$exporter, 'handle']);

        $duplicator = new Duplicate_Controller();
        add_action('admin_post_formatrica_duplicate', [$duplicator, 'handle']);

        $deleter = new Delete_Controller();
        add_action('admin_post_formatrica_delete', [$deleter, 'handle']);
        add_action('admin_post_formatrica_delete_permanent', [$deleter, 'handle']);

        $activator = new Activate_Controller();
        add_action('admin_post_formatrica_activate', [$activator, 'handle']);

        // Builder quick-action deep-links (Pro forms.quick_actions): seed the
        // new-page/post editor with the form's shortcode block.
        (new Quick_Action_Prefill())->register();

        // Submissions admin surface: CSV export + single delete.
        $submission_export = new Submission_Export_Controller();
        add_action('admin_post_formatrica_export_submissions', [$submission_export, 'handle_export']);
        add_action('admin_post_formatrica_delete_submission', [$submission_export, 'handle_delete']);
        add_action('admin_post_formatrica_mark_submission', [$submission_export, 'handle_mark']);

        // Submission PDF download (Pro documents.pdf; also gated inside the
        // handler). The wporg package strips app/Documents + this controller,
        // hence the class guard.
        if (class_exists(Submission_Pdf_Controller::class)) {
            $submission_pdf = new Submission_Pdf_Controller();
            add_action('admin_post_formatrica_submission_pdf', [$submission_pdf, 'handle']);
        }

        // Payment return/cancel pages (Pro payments.basic). Public (nopriv): the
        // visitor returns from the gateway possibly logged out; authenticated by
        // the per-order HMAC status_token inside the handler. The wporg package
        // strips app/Payments, hence the class guard.
        if (class_exists(\Formatrica\Payments\Payment_Page_Controller::class)) {
            $payment_page = new \Formatrica\Payments\Payment_Page_Controller();
            add_action('admin_post_formatrica_payment_return', [$payment_page, 'handle_return']);
            add_action('admin_post_nopriv_formatrica_payment_return', [$payment_page, 'handle_return']);
            add_action('admin_post_formatrica_payment_cancel', [$payment_page, 'handle_cancel']);
            add_action('admin_post_nopriv_formatrica_payment_cancel', [$payment_page, 'handle_cancel']);
        }

        $services->set(Menu::class, $menu);
        $services->set(Notices::class, $notices);
        $services->set(Export_Controller::class, $exporter);
        $services->set(Duplicate_Controller::class, $duplicator);
        $services->set(Delete_Controller::class, $deleter);
        $services->set(Activate_Controller::class, $activator);
    }
}
