<?php

if (! defined('ABSPATH')) {
    exit;
} final class PufferGo_Polylang_Setup
{
    public const DONE_OPTION = 'puffergo_polylang_setup_done';

    public const NOTICE_TRANSIENT = 'puffergo_polylang_init_done';

    public const DEFAULT_LANG_OPTION = 'puffergo_polylang_default_lang';

    public static function init(): void
    {
        add_action('admin_init', array(__CLASS__, 'maybe_bootstrap'));
        add_action('admin_notices', array(__CLASS__, 'maybe_render_notice'));
    }

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

if (! PufferGo_Polylang::is_language_admin_ready()) {
            return;
        }

if (get_option(self::DONE_OPTION)) {
            $actual_locale = PufferGo_Polylang::locale_for(PufferGo_Polylang::default_language());

            if ('' !== $actual_locale) {
                $stored = get_option(self::DEFAULT_LANG_OPTION);

                if (! is_array($stored) || ($stored['locale'] ?? '') !== $actual_locale) {
                    update_option(self::DEFAULT_LANG_OPTION, array('locale' => $actual_locale));
                }
            }

return;
        } self::ensure_default_language();
        $default = PufferGo_Polylang::default_language();

        if ('' === $default) {
            if (function_exists('puffergo_log')) {
                puffergo_log('Polylang setup: no default language after add; will retry next load.');
            }

return;
        } $tagged_ok = self::tag_existing_content($default);

        if (! $tagged_ok) {
            if (function_exists('puffergo_log')) {
                puffergo_log('Polylang setup: some posts could not be tagged; will retry next load.');
            }

return;
        } update_option(self::DEFAULT_LANG_OPTION, array('locale' => PufferGo_Polylang::locale_for($default)));
        update_option(self::DONE_OPTION, 1);
        set_transient(self::NOTICE_TRANSIENT, 1, MINUTE_IN_SECONDS);
    }

    private static function ensure_default_language(): void
    {
        if (! empty(PufferGo_Polylang::languages())) {
            return;
        } $configured = get_option(self::DEFAULT_LANG_OPTION);
        $locale = is_array($configured) ? ($configured['locale'] ?? '') : '';

        if ('' === $locale) {
            return;
        } PufferGo_Polylang::add_language(array('locale' => $locale));
    }

    private static function tag_existing_content(string $default): bool
    {
        $all_ok = true;

        foreach (PufferGo_Polylang::untagged_localizable_post_ids() as $post_id) {
            PufferGo_Polylang::set_post_language($post_id, $default);

            if ('' === PufferGo_Polylang::get_post_language($post_id)) {
                $all_ok = false;
            }
        }

return $all_ok;
    }

    public static function maybe_render_notice(): void
    {
        if (! delete_transient(self::NOTICE_TRANSIENT)) {
            return;
        } echo '<div class="notice notice-success is-dismissible"><p>';
        echo esc_html__('PufferGo set up Polylang: a default language is configured and your PufferGo pages and items are tagged in it. Add more languages in Polylang → Languages to start translating.', 'puffergo');
        echo '</p></div>';
    }
}
