<?php

if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

/**
 * Decides when full ReadyLaunch™ frontend asset bundles should load for login and account.
 *
 * Combines classic template toggles ({@see MeprReadyLaunchCtrl::template_enabled()}) with
 * FSE ReadyLaunch rules so Site Editor templates still receive mp-pro-login / account stacks
 * when design_enable_* toggles are off.
 */
class MeprReadyLaunchAssetsHelper
{
    /**
     * Whether to load the login ReadyLaunch bundle (theme, login.css, etc.).
     *
     * @return boolean
     */
    public static function should_enqueue_login_bundle(): bool
    {
        if (MeprReadyLaunchCtrl::template_enabled('login')) {
            return true;
        }

        return self::fse_frontend_needs_login_assets();
    }

    /**
     * Whether to load the account ReadyLaunch bundle (account.css, account.js, etc.).
     *
     * @return boolean
     */
    public static function should_enqueue_account_bundle(): bool
    {
        if (MeprReadyLaunchCtrl::template_enabled('account')) {
            return true;
        }

        return self::fse_frontend_needs_account_assets();
    }

    /**
     * Whether to omit mp-pro-theme for the login asset stack (FSE ReadyLaunch only).
     *
     * Classic ReadyLaunch login relies on theme.css for guest chrome and shared form base styles.
     * FSE login uses the block theme header/layout; theme.css often conflicts (e.g. .site-header).
     *
     * @return boolean
     */
    public static function should_skip_mp_pro_theme_for_fse_login(): bool
    {
        if (MeprReadyLaunchCtrl::template_enabled('login')) {
            return false;
        }

        return self::fse_frontend_needs_login_assets();
    }

    /**
     * Whether to omit mp-pro-theme for the account asset stack (FSE ReadyLaunch only).
     *
     * Classic ReadyLaunch account relies on theme.css for shared chrome with the PHP layout.
     * FSE account uses the block theme shell; theme.css can skew header/typography like FSE login.
     *
     * @return boolean
     */
    public static function should_skip_mp_pro_theme_for_fse_account(): bool
    {
        if (MeprReadyLaunchCtrl::template_enabled('account')) {
            return false;
        }

        return self::fse_frontend_needs_account_assets();
    }

    /**
     * Whether to omit ReadyLaunch / MemberPress clash styles on the front for MemberPress FSE shells.
     *
     * Detection uses {@see MeprFseTemplatesCtrl::post_has_memberpress_fse_template()} (post _wp_page_template) **or**
     * {@see MeprFseTemplatesCtrl::request_uses_memberpress_fse_cpt_option_template()} (fse_cpt_templates CPT default),
     * because courses often render the FSE shell from CPT options before every post has template meta.
     *
     * Block-theme shells (courses, membership CPTs, etc.) should not load readylaunch/theme.css — it targets the
     * classic PHP layout (.site-header, normalize stack) and conflicts with FSE + plugin shells.
     *
     * When classic ReadyLaunch login or account PHP templates are enabled for those dedicated pages, theme.css stays
     * so guest/account chrome matches the classic stack.
     *
     * @return boolean
     */
    public static function should_dequeue_mp_pro_theme_for_fse_shell(): bool
    {
        if (is_admin()) {
            return false;
        }

        if (! MeprFseHelper::is_fse_mode_active()) {
            return false;
        }

        $uses_memberpress_fse_shell = MeprFseHelper::post_has_memberpress_fse_template()
            || MeprFseHelper::request_uses_memberpress_fse_cpt_option_template();

        if (! $uses_memberpress_fse_shell) {
            return false;
        }

        global $post;

        if (MeprReadyLaunchCtrl::template_enabled('login') && MeprUser::is_login_page($post)) {
            return false;
        }

        if (MeprReadyLaunchCtrl::template_enabled('account') && MeprUser::is_account_page($post)) {
            return false;
        }

        return (bool) MeprHooks::apply_filters('mepr_should_dequeue_mp_pro_theme_for_fse_shell', true);
    }

    /**
     * FSE: login page uses a MemberPress block template while the classic login template toggle is off.
     *
     * @return boolean
     */
    private static function fse_frontend_needs_login_assets(): bool
    {
        if (is_admin()) {
            return false;
        }

        if (! MeprFseHelper::is_fse_mode_active()) {
            return false;
        }

        $options       = MeprOptions::fetch();
        $login_page_id = isset($options->login_page_id) ? (int) $options->login_page_id : 0;
        if ($login_page_id <= 0 || ! is_page($login_page_id)) {
            return false;
        }

        return MeprFseHelper::post_has_memberpress_fse_template($login_page_id);
    }

    /**
     * FSE: account page uses a MemberPress block template while the classic account template toggle is off.
     *
     * @return boolean
     */
    private static function fse_frontend_needs_account_assets(): bool
    {
        if (is_admin()) {
            return false;
        }

        if (! MeprFseHelper::is_fse_mode_active()) {
            return false;
        }

        $options         = MeprOptions::fetch();
        $account_page_id = isset($options->account_page_id) ? (int) $options->account_page_id : 0;
        if ($account_page_id <= 0 || ! is_page($account_page_id)) {
            return false;
        }

        return MeprFseHelper::post_has_memberpress_fse_template($account_page_id);
    }
}
