<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include 'db.php';

$id = $_GET['id'] ?? 0;

// Fetch company (now also fetch description)
$stmt = $pdo->prepare("
  SELECT c.*, cat.name AS category
  FROM companies c
  LEFT JOIN categories cat ON c.category_id = cat.id
  WHERE c.id=?
");
$stmt->execute([$id]);
$company = $stmt->fetch();

if (!$company) { die("Company not found. <a href='index.php'>Back to home</a>"); }

// Add new review
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SESSION['user_id'])) {
    $rating = $_POST['rating'];
    $comment = $_POST['review_text'];
    $pdo->prepare('INSERT INTO reviews (user_id, company_id, rating, comment) VALUES (?,?,?,?)')
        ->execute([$_SESSION['user_id'], $id, $rating, $comment]);
    header("Location: review.php?id=$id");
    exit;
}

// Fetch reviews
$stmt = $pdo->prepare("
  SELECT r.*, u.username, u.profile_image AS user_image 
  FROM reviews r
  LEFT JOIN users u ON r.user_id = u.id
  WHERE r.company_id=?
  ORDER BY r.id DESC
");
$stmt->execute([$id]);
$reviews = $stmt->fetchAll();

// Stats
$stmt = $pdo->prepare("SELECT ROUND(AVG(rating),1) AS avg_rating, MAX(rating) AS highest_rating, COUNT(*) AS total_reviews FROM reviews WHERE company_id=?");
$stmt->execute([$id]);
$stats = $stmt->fetch();

// Latest reviews
$latestReviews = $pdo->query("
  SELECT r.comment, r.rating, u.username, c.name AS company_name
  FROM reviews r
  JOIN users u ON r.user_id = u.id
  JOIN companies c ON r.company_id = c.id
  ORDER BY r.id DESC
  LIMIT 5
")->fetchAll();

// Top rated companies
$topCompanies = $pdo->query("
  SELECT c.id, c.name, c.image, ROUND(AVG(r.rating),1) AS avg_rating
  FROM companies c
  JOIN reviews r ON c.id = r.company_id
  GROUP BY c.id
  HAVING COUNT(r.id) >= 1
  ORDER BY avg_rating DESC
  LIMIT 5
")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($company['name']) ?> - Review Stream</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Sans-serif fonts -->
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Raleway&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Slab&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif&display=swap" rel="stylesheet">


<!-- Favicon -->
<link rel="icon" href="/favicon.png" type="image/png" />



<style>
  body {
    margin: 0;
    padding: 0; /* removed huge padding */
    font-family: Arial, sans-serif;
    background: #f5f5f5;
	padding-bottom: 200px !important;
  }
  .container { max-width: 1200px; margin: auto; padding: 20px; }
  .content-layout { display: flex; gap: 20px; flex-wrap: wrap; }
  .main-content { flex: 3; min-width: 250px; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); }
  .sidebar { flex: 1; min-width: 200px; display: flex; flex-direction: column; gap: 15px; }
  .sidebar-card { background: #fff; padding: 12px; border-radius: 8px; box-shadow: 0 1px 4px rgba(0,0,0,0.08); }
  .star-box {
    display: inline-block;
    width: 22px;
    height: 22px;
    margin-right: 2px;
    background-color: #d3d3d3;
    color: white;
    text-align: center;
    line-height: 22px;
    font-size: 14px;
    border-radius: 3px;
  }
  .star-box.filled {
    background-color: #00b67a;
  }
  .about-company {
    background: #fafafa;
    margin-top: 25px;
    padding: 15px;
    border-left: 4px solid #00b67a;
    border-radius: 6px;
  }
  .about-company h3 {
    margin-top: 0;
    color: #333;
  }
  .topbar {
    background: #007BFF;
    color: white;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .topbar .menu a {
    color: white;
    margin-left: 10px;
    text-decoration: none;
  }
  footer {
    text-align: center;
    margin-top: 40px;
    padding: 15px;
    font-size: 14px;
    color: #666;
  }
  .footer-menu a {
    margin: 0 8px;
    color: #666;
    text-decoration: none;
  }
  .review-box {
    background: #f9f9f9;
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 6px;
  }
  
  .review-box1 p{
	  font-family: 'Poppins', sans-serif;
	  line-height: 8px !important;
	  font-size: 13px !important;
  }
  
  .review-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #ccc;
}

  .review-user-img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 10px;
}

  
  
  .fixed-footer {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      background: #222;
      text-align: center;
      padding: 10px 0;
      color: #fff;
      z-index: 1000;
    }

   .fixed-footer p{
		font-family: 'Poppins', sans-serif !important;
		font-size: 13px !important;
	}

    .footer-menu a {
      margin: 0 10px;
      text-decoration: none;
      color: #fff;
	  font-family: 'Poppins', sans-serif !important;
	  font-size: 13px !important;
    }

    .footer-menu a:hover {
      text-decoration: underline;
    }
	
	
	
	 /* Collapsible menu */
  .menu { display: none; flex-direction: column; }
  .menu.active { display: flex; }
  .menu a { padding: 8px 0; text-decoration: none; color: #333; }
  .menu-toggle { font-size: 1.5em; cursor: pointer; }
  @media(min-width: 768px){
    .menu { display: flex !important; flex-direction: row; }
    .menu a { margin-left: 15px; }
    .menu-toggle { display: none; }
  }
  
  
  .topbar { 
  display:flex; 
  justify-content:space-between; 
  align-items:center; 
  padding:10px 20px; 
  background:#007BFF; 
  color:white; 
  }
 
  .logo {
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #a2d4f5, #fefb72); /* light blue to lemon */
  border-radius: 50%;
  width: 36px;
  height: 36px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  margin-right: 8px;
}
</style>
</head>
<body>


<div class="topbar">
 <div class="logo"><div class="logo-icon"><i class="fas fa-shield-alt" style="color: skyblue; font-size: 25px; margin-left: 6px !important; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);"></i>
</div> REVIEW STREAM
  </div>
  <div class="menu-toggle" onclick="document.querySelector('.menu').classList.toggle('active')">
    <i class="fas fa-bars"></i>
  </div>
  
  
  <div class="menu">
    <a href="index.php">Home</a>
    <a href="companies.php">Companies</a>
    <a href="blog.php">Newsroom</a>
    <?php if(isset($_SESSION['user_id'])): ?>
      <a href="logout.php">Logout</a>
    <?php else: ?>
      <a href="login.php">Login</a>
    <?php endif; ?>
    <button class="dark-mode-toggle" onclick="document.body.classList.toggle('dark-mode')">🌓</button>
  </div>
</div>



<div class="container">
  <div class="content-layout">
    <div class="main-content">
      <div class="card">
        <h2><?= htmlspecialchars($company['name']) ?></h2>
        <div class="review-box1" style="display:flex;gap:10px;align-items:center;">
          <img src="images/companies/<?= htmlspecialchars($company['image']) ?>" alt="<?= htmlspecialchars($company['name']) ?>" style="width:100px;height:100px;object-fit:contain;">
          <div>
            <p><a href="<?= htmlspecialchars($company['website']) ?>" target="_blank"><?= htmlspecialchars($company['website']) ?></a></p>
            <p><?= htmlspecialchars($company['category']) ?></p>
            <p><?= $stats['total_reviews'] ?> Reviews â€¢ AVG: <?= $stats['avg_rating'] ?: 0 ?>/5</p>
            <p>
              <?php for($i=1;$i<=5;$i++): ?>
                <span class="star-box <?= $i<=$stats['highest_rating'] ? 'filled' : '' ?>">â˜…</span>
              <?php endfor; ?>
              (<?= $stats['highest_rating'] ?: 0 ?>/5)
            </p>
          </div>
        </div>

        <?php if(isset($_SESSION['user_id'])): ?>
        <h3>Add Your Review</h3>
        <form method="post">
          <label>Rating:</label>
          <select name="rating" required>
            <option value="">Select...</option>
            <?php for($i=1;$i<=5;$i++): ?>
              <option value="<?= $i ?>"><?= $i ?></option>
            <?php endfor; ?>
          </select>
          <label>Review Text:</label>
          <textarea name="review_text" required></textarea>
          <button type="submit">Submit Review</button>
        </form>
        <?php else: ?>
          <p><a href="login.php">Login</a> to add your review.</p>
        <?php endif; ?>

        <h3>User Reviews</h3>
        <?php if($reviews): ?>
          <?php foreach($reviews as $r): ?>
           <div class="review-box" style="display:flex;gap:10px;align-items:flex-start;">
              <img src="<?= $r['user_image'] ? 'images/user/' . htmlspecialchars($r['user_image']) : 'images/user/default-avatar.png' ?>" alt="User Image" class="review-user-img"/>
            <div>
    <p><strong><?= htmlspecialchars($r['username']) ?></strong> rated 
      <?php for($i=1;$i<=5;$i++): ?>
        <span class="star-box <?= $i<=$r['rating'] ? 'filled' : 'empty' ?>">â˜…</span>
      <?php endfor; ?>
      (<?= $r['rating'] ?>/5)
    </p>
    <p><?= nl2br(htmlspecialchars($r['comment'])) ?></p>
  </div>
</div>
          <?php endforeach; ?>
        <?php else: ?>
          <p>No reviews yet.</p>
        <?php endif; ?>

        <?php if(!empty($company['description'])): ?>
          <div class="about-company">
            <h3>About <?= htmlspecialchars($company['name']) ?></h3>
            <p><?= nl2br(htmlspecialchars($company['description'])) ?></p>
          </div>
        <?php endif; ?>
      </div>
    </div>
   

   <div class="sidebar">
     <div class="sidebar-card">
  <h4>Latest Reviews</h4>
  <?php foreach($latestReviews as $rev): ?>
    <p><strong><?= htmlspecialchars($rev['username']) ?></strong> on <em><?= htmlspecialchars($rev['company_name']) ?></em>:</p>
    <div>
      <?php for($i=1;$i<=5;$i++): ?>
        <span class="star-box <?= $i<=$rev['rating'] ? 'filled' : 'empty' ?>">â˜…</span>
      <?php endfor; ?>
      (<?= $rev['rating'] ?>/5)
    </div>
    <p style="margin-top:2px;"><?= htmlspecialchars(substr($rev['comment'],0,50)) ?>...</p>
    <hr>
  <?php endforeach; ?>
</div>

      <div class="sidebar-card">
        <h4>Top Rated Companies</h4>
        <?php foreach($topCompanies as $top): ?>
          <div style="display:flex;align-items:center;gap:8px;margin-bottom:5px;">
            <img src="images/companies/<?= htmlspecialchars($top['image']) ?>" alt="<?= htmlspecialchars($top['name']) ?>" style="width:30px;height:30px;object-fit:contain;">
            <span><?= htmlspecialchars($top['name']) ?> (<?= $top['avg_rating'] ?>/5)</span>
          </div>
        <?php endforeach; ?>
      </div>
    </div>
	
	
  </div>
</div>

<?php include 'footer.php'; ?>
</body>
</html>
