<?php
require_once 'admin/config/db.php';

$slug = isset($_GET['slug']) ? trim($_GET['slug']) : '';
if (!$slug) {
    header("Location: portfolio.php");
    exit;
}

// Fetch the project
$stmt = $pdo->prepare("
    SELECT p.*, c.name as category_name, c.slug as category_slug, s.name as subcategory_name 
    FROM portfolio_items p 
    LEFT JOIN portfolio_categories c ON p.category_id = c.id 
    LEFT JOIN portfolio_subcategories s ON p.subcategory_id = s.id
    WHERE p.slug = ? AND p.status = 'published'
");
$stmt->execute([$slug]);
$project = $stmt->fetch();

if (!$project) {
    header("Location: portfolio.php");
    exit;
}

// Fetch similar projects
$similar_stmt = $pdo->prepare("SELECT * FROM portfolio_items WHERE category_id = ? AND id != ? AND status = 'published' LIMIT 3");
$similar_stmt->execute([$project['category_id'], $project['id']]);
$similar_projects = $similar_stmt->fetchAll();

$pageTitle = htmlspecialchars($project['title']) . " | MK Construction & Builder Portfolio";
include 'includes/header.php';
?>

    <div class="page-header" style="background-image: linear-gradient(rgba(0,0,0,0.8), rgba(0,0,0,0.8)), url('<?= htmlspecialchars($project['image_path']) ?>'); background-size: cover; padding: 150px 0;">
        <div class="container text-center">
            <h1 style="color: white; font-size: 3.5rem;"><?= htmlspecialchars($project['title']) ?></h1>
            <p style="color: var(--primary-color); font-weight: 600; text-transform: uppercase;"><?= htmlspecialchars($project['category_name']) ?> Project</p>
        </div>
    </div>

    <section class="section-padding">
        <div class="container">
            <div class="project-details-layout" style="display: grid; grid-template-columns: 2fr 1fr; gap: 50px;">
                <div class="project-main">
                    <img src="<?= htmlspecialchars($project['image_path']) ?>" alt="" style="width: 100%; border-radius: 15px; margin-bottom: 30px; box-shadow: var(--shadow-lg);">
                    <h2 style="font-family: var(--font-serif); margin-bottom: 20px;">Project Overview</h2>
                    <div style="line-height: 1.8; color: #4b5563; font-size: 1.1rem;">
                        <?= $project['description'] ?>
                    </div>
                </div>
                
                <aside class="project-sidebar">
                    <div class="info-widget" style="background: #f8fafc; padding: 35px; border-radius: 12px; border-left: 5px solid var(--primary-color);">
                        <h3 style="margin-bottom: 25px; font-size: 1.4rem;">Project Info</h3>
                        <div class="info-item mb-4">
                            <h5 style="color: var(--text-muted); font-size: 0.85rem; text-transform: uppercase;">Client Name</h5>
                            <p style="font-weight: 700; font-size: 1.1rem; color: var(--dark-nav);"><?= htmlspecialchars($project['client_name'] ?? 'Private Client') ?></p>
                        </div>
                        <div class="info-item mb-4">
                            <h5 style="color: var(--text-muted); font-size: 0.85rem; text-transform: uppercase;">Category</h5>
                            <p style="font-weight: 700; font-size: 1.1rem; color: var(--dark-nav);"><?= htmlspecialchars($project['category_name']) ?></p>
                        </div>
                        <div class="info-item">
                            <h5 style="color: var(--text-muted); font-size: 0.85rem; text-transform: uppercase;">Date Completed</h5>
                            <p style="font-weight: 700; font-size: 1.1rem; color: var(--dark-nav);"><?= date('F Y', strtotime($project['created_at'])) ?></p>
                        </div>
                        <a href="get-a-free-quote.php" class="btn btn-primary w-100 mt-4">Start Your Project</a>
                    </div>
                </aside>
            </div>
        </div>
    </section>

<?php include 'includes/footer.php'; ?>
