<?php

namespace NeurogenesisTheme\Helpers;

defined('ABSPATH') || exit;

/**
 * Eligibility and persistence for the admin onboarding notice.
 *
 * Dismiss / snooze state is stored in the single `thinko_options` array
 * (theme review §5 / §12).
 */
class OnboardingNotice
{
    /**
     * Design mode: bypass NeurogenesisTheme page, import, and dismiss gates.
     * Enable with: add_filter( 'neurogenesis_theme_onboarding_debug', '__return_true' );
     */
    public static function is_debug(): bool
    {
        return (bool) apply_filters('neurogenesis_theme_onboarding_debug', false);
    }

    public static function should_render(): bool
    {
        if (!is_user_logged_in() || !current_user_can('edit_posts')) {
            return false;
        }

        if (self::is_debug()) {
            return true;
        }

        if (self::is_dismissed()) {
            return false;
        }

        if (self::is_snoozed()) {
            return false;
        }

        if (isset($_GET['page']) && NEUROGENESIS_THEME_PAGE_ID === $_GET['page']) {
            return false;
        }

        if (null !== ContentLayouts::imported_layout_slug()) {
            return false;
        }

        return (bool) apply_filters('neurogenesis_theme_onboarding_should_show', true);
    }

    /**
     * @return array{onboarding_dismissed?: bool, onboarding_snoozed_until?: int}
     */
    public static function get_state(): array
    {
        return [
            'onboarding_dismissed'     => (bool) ThemeOptions::get(ThemeOptions::ONBOARDING_DISMISSED, false),
            'onboarding_snoozed_until' => (int) ThemeOptions::get(ThemeOptions::ONBOARDING_SNOOZED_UNTIL, 0),
        ];
    }

    /**
     * @param array{onboarding_dismissed?: bool, onboarding_snoozed_until?: int} $state
     */
    public static function update_state(array $state): void
    {
        $values = [];

        if (array_key_exists('onboarding_dismissed', $state)) {
            $values[ThemeOptions::ONBOARDING_DISMISSED] = !empty($state['onboarding_dismissed']);
        }
        if (array_key_exists('onboarding_snoozed_until', $state)) {
            $values[ThemeOptions::ONBOARDING_SNOOZED_UNTIL] = (int) $state['onboarding_snoozed_until'];
        }

        ThemeOptions::set_many($values);
    }

    public static function is_dismissed(): bool
    {
        return !empty(self::get_state()['onboarding_dismissed']);
    }

    public static function is_snoozed(): bool
    {
        $until = (int) (self::get_state()['onboarding_snoozed_until'] ?? 0);

        return $until > 0 && time() < $until;
    }

    public static function dismiss(): void
    {
        self::update_state(
            [
                'onboarding_dismissed'     => true,
                'onboarding_snoozed_until' => 0,
            ]
        );
    }

    /**
     * @param int $days Number of days to snooze the notice.
     */
    public static function snooze(int $days = 7): void
    {
        $days = max(1, $days);

        self::update_state(
            [
                'onboarding_dismissed'     => false,
                'onboarding_snoozed_until' => time() + ($days * DAY_IN_SECONDS),
            ]
        );
    }
}
