<?php
/**
 * Article Builder - Password Protected Entry Point
 * Simple password protection using PHP sessions
 */

// ============================================================================
// CACHE CONTROL - Prevent browser caching of dynamic content
// ============================================================================
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

// Start session
session_start();

// Version number for cache busting on assets
define('APP_VERSION', '1.1.0');

// Fixed password (server-side only, not exposed to client)
define('ACCESS_PASSWORD', 'sasasasa');

// Handle password submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    if ($_POST['password'] === ACCESS_PASSWORD) {
        $_SESSION['authenticated'] = true;
        header('Location: index.php');
        exit;
    } else {
        $error = true;
    }
}

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: index.php');
    exit;
}

// Check if user is authenticated
$isAuthenticated = isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;

// If authenticated, show the main application
if ($isAuthenticated) {
    // Read the index.html content
    $html = file_get_contents('index.html');

    // Add version parameter to CSS and JS files for cache busting
    $version = APP_VERSION;
    $html = str_replace('href="style.css"', 'href="style.css?v=' . $version . '"', $html);
    $html = str_replace('src="prompts-config.js"', 'src="prompts-config.js?v=' . $version . '"', $html);
    $html = str_replace('src="app.js"', 'src="app.js?v=' . $version . '"', $html);

    // Output the modified HTML
    echo $html;
    exit;
}

// If not authenticated, show password modal
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <meta name="description" content="Multi-agent workflow system for creating comprehensive, SEO-optimized articles. AI-powered research, focus validation, and prompt generation.">
    <meta name="author" content="Mauro Sicard">

    <!-- Open Graph / Social Media Meta Tags -->
    <meta property="og:title" content="Article Builder | BRIX Templates">
    <meta property="og:description" content="Multi-agent workflow system for creating comprehensive, SEO-optimized articles. AI-powered research, focus validation, and prompt generation.">
    <meta property="og:type" content="website">
    <meta property="og:url" content="https://apps.brixtemplates.com/article-builder/">
    <meta property="og:image" content="https://apps.brixtemplates.com/article-builder/assets/featured-image.png">
    <meta property="og:image:width" content="1200">
    <meta property="og:image:height" content="630">
    <meta property="og:image:alt" content="Article Builder - AI-powered content creation workflow">

    <!-- Twitter Card Meta Tags -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="Article Builder | BRIX Templates">
    <meta name="twitter:description" content="Multi-agent workflow system for creating comprehensive, SEO-optimized articles. AI-powered research, focus validation, and prompt generation.">
    <meta name="twitter:image" content="https://apps.brixtemplates.com/article-builder/assets/featured-image.png">
    <meta name="twitter:image:alt" content="Article Builder - AI-powered content creation workflow">

    <title>Article Builder - Authentication</title>
    <link rel="icon" type="image/svg+xml" href="assets/favicon.svg">

    <!-- Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Figtree:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Figtree', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .auth-container {
            background: white;
            border-radius: 24px;
            padding: 48px;
            max-width: 440px;
            width: 100%;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            text-align: center;
        }

        .auth-icon {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 24px;
        }

        .auth-icon i {
            font-size: 36px;
            color: white;
        }

        h1 {
            font-size: 28px;
            font-weight: 700;
            color: #1a202c;
            margin-bottom: 12px;
        }

        .subtitle {
            font-size: 15px;
            color: #718096;
            margin-bottom: 32px;
            line-height: 1.6;
        }

        .form-group {
            margin-bottom: 24px;
            text-align: left;
        }

        label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #4a5568;
            margin-bottom: 8px;
        }

        input[type="password"] {
            width: 100%;
            padding: 14px 16px;
            font-size: 15px;
            font-family: 'Figtree', sans-serif;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            transition: all 0.2s ease;
            outline: none;
        }

        input[type="password"]:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .error-message {
            background: #fee;
            border: 1px solid #fcc;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 24px;
            color: #c53030;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .error-message i {
            font-size: 18px;
        }

        .btn-submit {
            width: 100%;
            padding: 14px 24px;
            font-size: 15px;
            font-weight: 600;
            font-family: 'Figtree', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-submit:active {
            transform: translateY(0);
        }

        .footer-text {
            margin-top: 24px;
            font-size: 13px;
            color: #a0aec0;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-icon">
            <i class="bi bi-shield-lock"></i>
        </div>

        <h1>Article Builder</h1>
        <p class="subtitle">Enter your password to access the application</p>

        <?php if (isset($error)): ?>
        <div class="error-message">
            <i class="bi bi-exclamation-circle"></i>
            <span>Incorrect password. Please try again.</span>
        </div>
        <?php endif; ?>

        <form method="POST" action="index.php">
            <div class="form-group">
                <label for="password">Password</label>
                <input
                    type="password"
                    id="password"
                    name="password"
                    placeholder="Enter your password"
                    autofocus
                    required
                >
            </div>

            <button type="submit" class="btn-submit">
                <i class="bi bi-unlock"></i>
                <span>Access Application</span>
            </button>
        </form>

        <p class="footer-text">BRIX Templates Article Builder</p>
    </div>
</body>
</html>
