<?php
/**
 * Activation, deactivation and the admin warnings that matter.
 *
 * @package SkyServers\Retragere
 */

declare(strict_types=1);

namespace SkyServers\Retragere\Wp;

/**
 * Creates the public page on activation so the shop is compliant immediately,
 * without the owner having to know what a shortcode is.
 */
final class Activation
{
    public static function activate(): void
    {
        PostType::register();

        $settings = Settings::all();
        $pageId   = (int) $settings['page_id'];

        if ($pageId <= 0 || 'publish' !== get_post_status($pageId)) {
            $pageId = wp_insert_post(
                [
                    'post_type'    => 'page',
                    'post_status'  => 'publish',
                    'post_title'   => 'Retragere din contract',
                    'post_content' => '[' . Shortcode::TAG . ']',
                ]
            );

            if (!is_wp_error($pageId)) {
                $settings['page_id'] = (int) $pageId;
                update_option(Settings::OPTION, $settings);
            }
        }

        Retention::schedule();
    }

    public static function deactivate(): void
    {
        wp_clear_scheduled_hook(Mailer::HOOK_RETRY);
        Retention::unschedule();
        // Cleared by name rather than through the class that schedules it.
        // That class is absent from the free build, so calling it there was a
        // fatal error on deactivation; and a site that ran the paid build and
        // later replaced it with the free one would otherwise leave the event
        // behind in wp_cron with nothing left to handle it.
        wp_clear_scheduled_hook('ssrc_refund_reminders');
    }

    public static function notices(): void
    {
        if (!current_user_can('manage_options')) {
            return;
        }

        if (!Mailer::isConfigured()) {
            printf(
                '<div class="notice notice-warning"><p>%s</p></div>',
                esc_html__(
                    'Contract withdrawal: this site does not appear to use an authenticated SMTP server. The acknowledgement is required by law, and mail sent through the server\'s default function frequently lands in spam. Installing an SMTP plugin is recommended.',
                    'skyservers-retragere-contract'
                )
            );
        }

        $missing = Settings::missingIdentification();
        if ([] !== $missing) {
            printf(
                '<div class="notice notice-warning"><p>%s</p></div>',
                sprintf(
                    /* translators: %s: comma separated list of missing fields. */
                    esc_html__(
                        'Contract withdrawal: the confirmation sent to the customer has to identify the trader, and the following details are missing from the settings: %s. Without them the acknowledgement falls short of the legal requirements.',
                        'skyservers-retragere-contract'
                    ),
                    esc_html(implode(', ', $missing))
                )
            );
        }

        $pageId = (int) Settings::get('page_id');
        if ($pageId <= 0 || 'publish' !== get_post_status($pageId)) {
            printf(
                '<div class="notice notice-error"><p>%s</p></div>',
                esc_html__(
                    'Contract withdrawal: the public page is missing. Create a page containing the [skyservers_retragere] shortcode and add a visible link to it in the menu or the footer.',
                    'skyservers-retragere-contract'
                )
            );
        }
    }
}
