<?php
/**
 * Template Name: HVAC Community Login
 *
 * This is the custom template for the community login page.
 *
 * @package HVAC_Community_Events
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

get_header(); ?>

<style>
/* Override theme constraints for login page */
#primary, #main, .content-area, .site-main {
    max-width: none !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
}

/* Hide default page title */
.entry-header {
    display: none !important;
}

/* Ensure full-width wrapper */
.hvac-community-login-wrapper {
    width: 100vw !important;
    margin-left: calc(-50vw + 50%) !important;
    padding: 60px 20px !important;
    min-height: 70vh !important;
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%) !important;
}
</style>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php 
        // Ensure the Login_Handler class is available before instantiation
        if (class_exists('\HVAC_Community_Events\Community\Login_Handler')) {
            // Create instance and render login form
            $login_handler = new \HVAC_Community_Events\Community\Login_Handler();
            echo $login_handler->render_login_form(array());
        } else {
            // Fallback: Try to load the class file if not loaded
            $login_handler_file = HVAC_PLUGIN_DIR . 'includes/community/class-login-handler.php';
            if (file_exists($login_handler_file)) {
                require_once $login_handler_file;
                
                // Try again after loading
                if (class_exists('\HVAC_Community_Events\Community\Login_Handler')) {
                    $login_handler = new \HVAC_Community_Events\Community\Login_Handler();
                    echo $login_handler->render_login_form(array());
                } else {
                    // Final fallback: Display basic WordPress login form
                    echo '<div class="hvac-login-fallback">';
                    echo '<h2>Trainer Login</h2>';
                    wp_login_form(array(
                        'echo' => true,
                        'redirect' => home_url('/trainer/dashboard/'),
                        'form_id' => 'hvac_fallback_loginform',
                        'label_username' => 'Username or Email Address',
                        'label_password' => 'Password',
                        'label_remember' => 'Remember Me',
                        'label_log_in' => 'Log In',
                        'id_username' => 'user_login',
                        'id_password' => 'user_pass',
                        'id_remember' => 'rememberme',
                        'id_submit' => 'wp-submit',
                        'remember' => true,
                        'value_username' => '',
                        'value_remember' => false
                    ));
                    echo '</div>';
                }
            } else {
                // Emergency fallback: Basic HTML form
                echo '<div class="hvac-emergency-login">';
                echo '<h2>Trainer Login</h2>';
                echo '<form name="loginform" id="loginform" action="' . esc_url(site_url('wp-login.php', 'login_post')) . '" method="post">';
                echo '<p><label for="user_login">Username or Email Address<br>';
                echo '<input type="text" name="log" id="user_login" class="input" value="" size="20" /></label></p>';
                echo '<p><label for="user_pass">Password<br>';
                echo '<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" /></label></p>';
                echo '<p class="forgetmenot"><label for="rememberme"><input name="rememberme" type="checkbox" id="rememberme" value="forever" /> Remember Me</label></p>';
                echo '<input type="hidden" name="redirect_to" value="' . esc_url(home_url('/trainer/dashboard/')) . '" />';
                echo '<p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="Log In" /></p>';
                echo '</form>';
                echo '</div>';
            }
        }
        ?>
    </main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>