<?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-md-6 mb-4">
                    <div class="card h-100">
                        <div class="card-body text-center">
                            <!-- Logo -->
                            <div class="mb-3">
                                        <?php if (!empty($company['logo'])): ?>
                                            <img src="<?= e(getImageUrl($company['logo'])) ?>" alt="<?= e($company['company_name']) ?>" class="img-fluid" style="max-width: 150px; max-height: 150px; 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: 150px; height: 150px; border-radius: 8px; border: 1px solid #dee2e6;">
                                        <i class="fas fa-building fa-4x text-muted"></i>
                                    </div>
                                <?php endif; ?>
                            </div>
                            
                            <!-- Firma Adı -->
                            <h5 class="card-title mb-2"><?= e($company['company_name']) ?></h5>
                            
                            <!-- Kategoriler -->
                            <?php if (!empty($company['categories'])): ?>
                                <div class="mb-3">
                                    <?php foreach ($company['categories'] as $cat): ?>
                                        <a href="<?= SITE_URL ?>/<?= e($cat['slug']) ?>" class="badge bg-primary me-1 mb-1"><?= e($cat['name']) ?></a>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                            
                            <!-- Puanlama -->
                            <?php if ($company['avg_rating']): ?>
                                <div class="mb-3">
                                    <?= displayStars($company['avg_rating']) ?>
                                    <div class="mt-1">
                                        <strong><?= number_format($company['avg_rating'], 1) ?>/5</strong>
                                        <span class="text-muted ms-1">(<?= $company['review_count'] ?> yorum)</span>
                                    </div>
                                </div>
                            <?php else: ?>
                                <div class="mb-3 text-muted">
                                    <small>Henüz yorum yapılmamış</small>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="card-footer text-center">
                            <a href="<?= SITE_URL ?>/firma/<?= $company['id'] ?>" class="btn btn-primary btn-lg w-100">
                                <i class="fas fa-info-circle me-2"></i>Detayları Gör
                            </a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

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

