<?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;

defined('ABSPATH') || exit;

final class ChangelogPage
{
    private const PUBLIC_URL = 'https://designer.asukadev.com/changelog/#wordpress';

    public function render(): void
    {
        if (! current_user_can('manage_asuka_designer')) {
            wp_die(esc_html__('You are not allowed to access this page.', 'asuka-product-designer'));
        }

        $overview = AdminOverview::snapshot();
        $theme = sanitize_html_class((string) ($overview['settings']['admin_theme'] ?? 'light'));
        $releases = $this->releases();
        ?>
        <div class="wrap apd-admin-shell apd-admin-theme-<?php echo esc_attr($theme); ?> apd-changelog-page">
            <header class="apd-workspace-header">
                <div>
                    <span class="apd-dashboard-eyebrow"><?php esc_html_e('Product updates', 'asuka-product-designer'); ?></span>
                    <h1><?php esc_html_e('Changelog', 'asuka-product-designer'); ?></h1>
                    <p><?php esc_html_e('See what changed in the WordPress and WooCommerce integration, with the newest release first.', 'asuka-product-designer'); ?></p>
                </div>
                <a class="button button-primary" href="<?php echo esc_url(self::PUBLIC_URL); ?>" target="_blank" rel="noreferrer">
                    <?php esc_html_e('Open public changelog', 'asuka-product-designer'); ?>
                </a>
            </header>

            <section class="apd-changelog-summary" aria-label="<?php esc_attr_e('Installed version', 'asuka-product-designer'); ?>">
                <div>
                    <span><?php esc_html_e('Installed version', 'asuka-product-designer'); ?></span>
                    <strong><?php echo esc_html(ASUKA_PRODUCT_DESIGNER_VERSION); ?></strong>
                </div>
                <p><?php esc_html_e('Release notes are bundled with the installed plugin. Opening this page does not contact an external service or send store data.', 'asuka-product-designer'); ?></p>
            </section>

            <section class="apd-changelog-feed" aria-label="<?php esc_attr_e('WordPress releases', 'asuka-product-designer'); ?>">
                <?php if ($releases === []) : ?>
                    <div class="apd-changelog-empty">
                        <span class="dashicons dashicons-update-alt" aria-hidden="true"></span>
                        <div>
                            <h2><?php esc_html_e('Release notes are temporarily unavailable.', 'asuka-product-designer'); ?></h2>
                            <p><?php esc_html_e('The installed plugin continues to work normally. Open the public changelog to view the latest notes.', 'asuka-product-designer'); ?></p>
                        </div>
                    </div>
                <?php else : ?>
                    <?php foreach ($releases as $release) : ?>
                        <article class="apd-changelog-entry">
                            <aside>
                                <span><?php echo esc_html($release['channelLabel']); ?></span>
                                <strong><?php echo esc_html($release['version']); ?></strong>
                                <time datetime="<?php echo esc_attr($release['date']); ?>"><?php echo esc_html($release['date']); ?></time>
                            </aside>
                            <div>
                                <h2><a href="<?php echo esc_url($release['url']); ?>" target="_blank" rel="noreferrer"><?php echo esc_html($release['title']); ?></a></h2>
                                <?php if ($release['excerpt'] !== '') : ?>
                                    <p class="apd-changelog-entry__excerpt"><?php echo esc_html($release['excerpt']); ?></p>
                                <?php endif; ?>
                                <div class="apd-changelog-entry__notes"><?php echo wp_kses_post($release['content']); ?></div>
                            </div>
                        </article>
                    <?php endforeach; ?>
                <?php endif; ?>
            </section>
        </div>
        <?php
    }

    private function releases(): array
    {
        return [[
            'version' => ASUKA_PRODUCT_DESIGNER_VERSION,
            'channelLabel' => __('Stable', 'asuka-product-designer'),
            'date' => '2026-08-20',
            'title' => __('WordPress.org compliance and storefront hardening', 'asuka-product-designer'),
            'excerpt' => __('This maintenance release improves REST authorization, package storage, privacy disclosures, and WordPress coding-standard compliance.', 'asuka-product-designer'),
            'content' => '<ul>'
                . '<li>' . esc_html__('Restricted product configuration endpoints to authorized administrators or publicly bound products.', 'asuka-product-designer') . '</li>'
                . '<li>' . esc_html__('Moved imported portable-package media into a dedicated plugin uploads directory.', 'asuka-product-designer') . '</li>'
                . '<li>' . esc_html__('Removed the automatic external request from the administration changelog.', 'asuka-product-designer') . '</li>'
                . '<li>' . esc_html__('Moved production worksheet styles and behavior into enqueued assets.', 'asuka-product-designer') . '</li>'
                . '</ul>',
            'url' => self::PUBLIC_URL,
        ]];
    }
}
