<?php
require_once("php/db_connect.php");

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$shop_id = isset($_GET["shop_id"]) ? (int) $_GET["shop_id"] : 0;

$shopStmt = $conn->prepare("
    SELECT id, shop_name, shop_type, main_services
    FROM users
    WHERE id = ?
");
$shopStmt->bind_param("i", $shop_id);
$shopStmt->execute();
$shopResult = $shopStmt->get_result();
$shop = $shopResult->fetch_assoc();

if (!$shop) {
    die("Shop not found.");
}

$serviceStmt = $conn->prepare("
    SELECT id, service_name, description, category, service_type, service_image, estimated_days
    FROM services
    WHERE owner_shop_id = ? AND status = 'active'
    ORDER BY created_at DESC
");
$serviceStmt->bind_param("i", $shop_id);
$serviceStmt->execute();
$shopServices = $serviceStmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($shop["shop_name"]); ?></title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <section class="shop-profile-section">
        <div class="shop-profile-card">
            <h1><?php echo htmlspecialchars($shop["shop_name"]); ?></h1>
            <p>Shop Type: <?php echo htmlspecialchars($shop["shop_type"]); ?></p>
            <p>Main Services: <?php echo htmlspecialchars($shop["main_services"]); ?></p>
        </div>

        <div class="shop-services-list">
            <h2>Services Offered</h2>

            <div class="services-grid">
                <?php if ($shopServices && $shopServices->num_rows > 0): ?>
                    <?php while ($service = $shopServices->fetch_assoc()): ?>
                        <div class="service-card">
                            <div class="service-image-wrapper">
                                <?php if (!empty($service["service_image"])): ?>
                                    <img 
                                        src="uploads/services/<?php echo htmlspecialchars($service["service_image"]); ?>" 
                                        alt="<?php echo htmlspecialchars($service["service_name"]); ?>"
                                        class="service-image"
                                    >
                                <?php else: ?>
                                    <div class="service-image-placeholder">No Image</div>
                                <?php endif; ?>
                            </div>

                            <div class="service-card-body">
                                <span class="service-category">
                                    <?php echo htmlspecialchars($service["category"]); ?>
                                </span>

                                <h3><?php echo htmlspecialchars($service["service_name"]); ?></h3>
                                <p><?php echo htmlspecialchars($service["service_type"]); ?></p>
                                <p><?php echo htmlspecialchars($service["description"]); ?></p>
                                <p>Estimated Days: <?php echo (int)$service["estimated_days"]; ?></p>

                                <a href="login.php" class="view-price-btn">Log in to view price</a>
                            </div>
                        </div>
                    <?php endwhile; ?>
                <?php else: ?>
                    <p>No active services found for this shop.</p>
                <?php endif; ?>
            </div>
        </div>
    </section>
</body>
</html>