<?php

if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Shortcode for Custom Login Page with Automatic Redirect
 * Features: Nonce Security, Premium Layout, Mobile Responsive
 */
add_shortcode('afdp_login', 'afdp_login_shortcode');

function afdp_login_shortcode() {
    // --- 1. REDIRECT FEATURE ---
    // If user is already logged in, move them to the account page immediately
    if (is_user_logged_in()) {
        $account_url = home_url('/afdp-account/');
        echo "<script>window.location.href='" . esc_url($account_url) . "';</script>";
        return '<div style="text-align:center; padding:100px; font-family:sans-serif; color:#64748b;">Redirecting to your account...</div>';
    }

    $login_error = '';
    
    // --- 2. LOGIN HANDLER ---
    if (isset($_POST['afdp_login_submit'])) {
        if (!isset($_POST['afdp_login_nonce']) || !wp_verify_nonce($_POST['afdp_login_nonce'], 'afdp_login_action')) {
            $login_error = "Security check failed. Please refresh and try again.";
        } else {
            $redirect_to = isset($_GET['redirect_to']) ? esc_url($_GET['redirect_to']) : home_url('/afdp-account/');

            $creds = [
                'user_login'    => sanitize_text_field($_POST['afdp_email']),
                'user_password' => $_POST['afdp_password'], 
                'remember'      => isset($_POST['afdp_remember']),
            ];

            $user = wp_signon($creds, false);

            if (is_wp_error($user)) {
                $login_error = "Invalid email or password. Please try again.";
            } else {
                wp_safe_redirect($redirect_to);
                exit;
            }
        }
    }

    ob_start();
    ?>

    <style>
        /* Maintain exact layout features and styling */
        .afdp-auth-wrapper { padding: 50px 0; background: #f8fafc; }
        .afdp-auth-card { 
            max-width: 900px; margin: 0 auto; background: #fff; 
            border-radius: 24px; overflow: hidden; 
            box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
            display: flex;
        }
        .afdp-auth-info-side { 
            background: linear-gradient(135deg, #FF385C 0%, #E31C5F 100%); 
            padding: 50px; flex: 0 0 40%; color: #fff;
            display: flex; flex-direction: column; justify-content: center;
        }
        .afdp-auth-form-side { padding: 50px; flex: 1; }
        .afdp-auth-icon { font-size: 40px; color: #fff; }
        .afdp-auth-title { font-size: 28px; font-weight: 800; color: #0f172a; }
        .afdp-form-label { font-weight: 700; color: #64748b; font-size: 14px; margin-bottom: 8px; display: block; }
        .afdp-form-control { 
            width: 100%; padding: 14px 18px; border: 2px solid #e2e8f0; 
            border-radius: 12px; font-size: 16px; outline: none; transition: 0.3s;
        }
        .afdp-form-control:focus { border-color: #FF385C; }
        .afdp-auth-btn { 
            width: 100%; padding: 16px; background: #0f172a; color: #fff; 
            border: none; border-radius: 12px; font-weight: 800; font-size: 16px; 
            cursor: pointer; transition: 0.3s; 
        }
        .afdp-auth-btn:hover { background: #000; transform: translateY(-2px); }
        .afdp-link-red { color: #FF385C; text-decoration: none; font-weight: 700; }
        
        @media (max-width: 768px) {
            .afdp-auth-card { flex-direction: column; }
            .afdp-auth-info-side { display: none; }
        }
    </style>

    <div class="afdp-auth-wrapper">
        <div class="afdp-auth-card">
            <div class="afdp-auth-info-side">
                <div class="text-center w-100" style="text-align:center; margin-bottom: 20px;">
                    <span class="dashicons dashicons-rest" style="font-size:50px; width:50px; height:50px;"></span>
                </div>
                <h3 style="color:#fff; font-size: 24px; font-weight: 800;">Welcome Back!</h3>
                <p style="color:rgba(255,255,255,0.9); margin-top: 10px;">Log in to access your saved addresses and track live orders.</p>
                <ul style="margin-top: 30px; list-style: none; padding: 0; color:#fff; font-weight: 600;">
                    <li style="margin-bottom: 15px;"><span class="dashicons dashicons-history" style="margin-right: 10px;"></span> View Order History</li>
                    <li style="margin-bottom: 15px;"><span class="dashicons dashicons-heart" style="margin-right: 10px;"></span> Fast Checkout</li>
                    <li style="margin-bottom: 15px;"><span class="dashicons dashicons-star-filled" style="margin-right: 10px;"></span> Member-only Deals</li>
                </ul>
            </div>
            
            <div class="afdp-auth-form-side">
                <h2 class="afdp-auth-title mb-2">Login</h2>
                <p style="color: #64748b; margin-bottom: 30px;">Good to see you again! Please enter your details.</p>
                
                <?php if ($login_error): ?>
                    <div style="color: #721c24; background-color: #f8d7da; padding: 15px; border-radius: 10px; margin-bottom: 20px; font-weight: 600;">
                        <span class="dashicons dashicons-warning" style="margin-right:5px;"></span> <?php echo esc_html($login_error); ?>
                    </div>
                <?php endif; ?>

                <form method="post">
                    <?php wp_nonce_field('afdp_login_action', 'afdp_login_nonce'); ?>

                    <div style="margin-bottom: 20px;">
                        <label class="afdp-form-label">Username or Email</label>
                        <input type="text" name="afdp_email" class="afdp-form-control" placeholder="Enter your email" required>
                    </div>

                    <div style="margin-bottom: 20px;">
                        <div style="display: flex; justify-content: space-between; align-items: center;">
                            <label class="afdp-form-label">Password</label>
                            <a href="<?php echo esc_url(wp_lostpassword_url()); ?>" class="afdp-link-red" style="font-size: 12px;">Forgot Password?</a>
                        </div>
                        <input type="password" name="afdp_password" class="afdp-form-control" placeholder="••••••••" required>
                    </div>

                    <div style="margin-bottom: 25px;">
                        <label style="display: flex; align-items: center; color: #64748b; font-size: 14px; cursor: pointer;">
                            <input type="checkbox" name="afdp_remember" style="margin-right: 10px;"> Keep me logged in
                        </label>
                    </div>

                    <button type="submit" name="afdp_login_submit" class="afdp-auth-btn">Sign In</button>
                </form>

                <div style="text-align: center; margin-top: 40px; border-top: 1px solid #f1f5f9; padding-top: 30px;">
                    <p style="color: #64748b; margin-bottom: 5px;">Don't have an account yet?</p> 
                    <a href="<?php echo esc_url(home_url('/afdp-registration')); ?>" class="afdp-link-red" style="font-size: 18px; font-weight: 800;">Create Account Now</a>
                </div>
            </div>
        </div>
    </div>

    <?php
    return ob_get_clean();
}