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

$slug = $_GET['slug'] ?? '';

if (empty($slug)) {
    header("HTTP/1.0 404 Not Found");
    include '404.php';
    exit;
}

// Kategoriyi bul
$stmt = $pdo->prepare("SELECT * FROM company_categories WHERE slug = ? AND status = 'active'");
$stmt->execute([$slug]);
$category = $stmt->fetch();

if (!$category) {
    header("HTTP/1.0 404 Not Found");
    include '404.php';
    exit;
}

// SEO bilgileri
$page_title = $category['meta_title'] ?: $category['name'];
$meta_description = $category['meta_description'] ?: $category['description'];
$meta_keywords = $category['meta_keywords'];

// Bu kategorideki firmaları al
$stmt = $pdo->prepare("
    SELECT c.*, 
           (SELECT AVG(rating) FROM reviews WHERE company_id = c.id AND status = 'approved') as avg_rating,
           (SELECT COUNT(*) FROM reviews WHERE company_id = c.id AND status = 'approved') as review_count
    FROM companies c
    INNER JOIN company_category_relations ccr ON c.id = ccr.company_id
    WHERE ccr.category_id = ? AND c.status = 'active'
    ORDER BY c.views DESC
");
$stmt->execute([$category['id']]);
$companies = $stmt->fetchAll();

// Her firma için kategorileri al
foreach ($companies as &$company) {
    $stmt = $pdo->prepare("
        SELECT cc.* FROM company_categories cc
        INNER JOIN company_category_relations ccr ON cc.id = ccr.category_id
        WHERE ccr.company_id = ?
    ");
    $stmt->execute([$company['id']]);
    $company['categories'] = $stmt->fetchAll();
}
unset($company);

include 'includes/header.php';
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-12 mb-4">
            <?php if ($category['icon']): ?>
                <img src="<?= e(getImageUrl($category['icon'])) ?>" alt="<?= e($category['name']) ?>" class="mb-3"
                    style="max-width: 150px;">
            <?php endif; ?>
            <h1>
                <?= e($category['name']) ?>
            </h1>
            <?php if ($category['description']): ?>
                <p class="lead">
                    <?= e($category['description']) ?>
                </p>
            <?php endif; ?>
            <p class="text-muted">
                <?= count($companies) ?> firma bulundu
            </p>
        </div>
    </div>

    <?php if (empty($companies)): ?>
        <div class="alert alert-info">
            <i class="fas fa-info-circle me-2"></i>Bu kategoride henüz firma bulunmamaktadır.
        </div>
    <?php else: ?>
        <div class="row">
            <?php foreach ($companies as $company): ?>
                <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                    <div class="card h-100 shadow-sm hover-shadow transition">
                        <div class="card-body text-center p-3">
                            <a href="<?= SITE_URL ?>/firma/<?= e($company['slug'] ?? '') ?>"
                                class="text-decoration-none text-dark">
                                <!-- Logo -->
                                <div class="mb-2">
                                    <?php if (!empty($company['logo'])): ?>
                                        <img src="<?= e(getImageUrl($company['logo'])) ?>" alt="<?= e($company['company_name']) ?>"
                                            class="img-fluid"
                                            style="width: 100px; height: 100px; object-fit: contain; border-radius: 8px; border: 1px solid #dee2e6;">
                                    <?php else: ?>
                                        <div class="d-inline-flex align-items-center justify-content-center bg-light"
                                            style="width: 100px; height: 100px; border-radius: 8px; border: 1px solid #dee2e6;">
                                            <i class="fas fa-building fa-3x text-muted"></i>
                                        </div>
                                    <?php endif; ?>
                                </div>

                                <!-- Firma Adı -->
                                <h6 class="card-title mb-2 text-truncate" title="<?= e($company['company_name']) ?>">
                                    <?= e($company['company_name']) ?>
                                </h6>
                            </a>

                            <!-- Kategoriler -->
                            <?php if (!empty($company['categories'])): ?>
                                <div class="mb-2" style="height: 25px; overflow: hidden;">
                                    <?php foreach ($company['categories'] as $cat): ?>
                                        <span class="badge bg-light text-dark border me-1" style="font-size: 0.7rem;">
                                            <?= e($cat['name']) ?>
                                        </span>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>

                            <!-- Puanlama -->
                            <?php if ($company['avg_rating']): ?>
                                <div class="mb-2 small">
                                    <span class="text-warning"><i class="fas fa-star"></i></span>
                                    <strong>
                                        <?= number_format($company['avg_rating'], 1) ?>
                                    </strong>
                                    <span class="text-muted">(
                                        <?= $company['review_count'] ?>)
                                    </span>
                                </div>
                            <?php else: ?>
                                <div class="mb-2 text-muted small">
                                    <span>-</span>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

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