<?php
session_start();
require_once '../includes/product-card.php';
require_once '../config/database.php';
require_once '../models/user.php';

$page_title = 'السلة';
$base_path = '../';

$database = new Database();
$conn = $database->getConnection();
$user_model = new User($conn);

$cart_items = [];
$total = 0;
$discount = 0;
$applied_coupon = null;

if (isset($_SESSION['user_id'])) {
    $stmt = $conn->prepare("
        SELECT c.*, p.name, p.price, p.image
        FROM cart c
        JOIN products p ON c.product_id = p.id
        WHERE c.user_id = ?
    ");
    $stmt->execute([$_SESSION['user_id']]);
    $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($cart_items as $item) {
        $total += $item['price'] * $item['quantity'];
    }
    
    if (isset($_SESSION['applied_coupon'])) {
        $applied_coupon = $_SESSION['applied_coupon'];
        $discount = $applied_coupon['discount'];
    }
}

$final_total = $total - $discount;

try {
    $stmt = $conn->prepare("SELECT setting_value FROM settings WHERE setting_key = 'store_name'");
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $site_name = $result ? $result['setting_value'] : 'Roz Skin';
    $logo_text = $site_name;
} catch (PDOException $e) {
    $site_name = 'Roz Skin';
    $logo_text = 'Roz Skin';
}

$cart_count = count($cart_items);
?>
