<?php
// Check authentication - customer must be logged in
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'customer') {
    header('Location: ../index.php');
    exit();
}

// Get customer data
$customerModel = new Customer();
$customer = $customerModel->findByUserId($_SESSION['user_id']);

if (!$customer) {
    // Create customer profile if it doesn't exist
    $userModel = new User();
    $user = $userModel->find($_SESSION['user_id']);
    
    if ($user) {
        $customerData = [
            'user_id' => $_SESSION['user_id'],
            'first_name' => $user['first_name'] ?? '',
            'last_name' => $user['last_name'] ?? '',
            'phone' => $user['phone'] ?? ''
        ];
        $customerId = $customerModel->create($customerData);
        $customer = $customerModel->find($customerId);
    } else {
        $_SESSION['error'] = 'Customer profile not found.';
        header('Location: ../index.php');
        exit();
    }
}

// Get customer statistics
$stats = $customerModel->getStats($customer['id']);

// Get recent orders for this customer
$orderModel = new Order();
$db = Database::getInstance();
$recentOrders_sql = "SELECT * FROM orders WHERE customer_id = ? ORDER BY created_at DESC LIMIT 10";
$recentOrders = $db->fetchAll($recentOrders_sql, [$customer['id']]);

// Get wallet data - FIXED: Use findByUserId instead of getByCustomerId
$walletModel = new Wallet();
$wallet = $walletModel->findByUserId($_SESSION['user_id']);

// If no wallet exists, create one
if (!$wallet) {
    $walletModel->create($_SESSION['user_id']);
    $wallet = $walletModel->findByUserId($_SESSION['user_id']);
}

// Get message count for notifications (if Message model exists)
$unreadCount = 0;
if (class_exists('Message')) {
    try {
        $messageModel = new Message();
        $unreadCount = $messageModel->getUnreadCount($_SESSION['user_id']);
    } catch (Exception $e) {
        $unreadCount = 0;
    }
}

// Set session variables for header
$_SESSION['unread_messages'] = $unreadCount;
$_SESSION['user_name'] = $customer['first_name'] . ' ' . $customer['last_name'];
$_SESSION['user_avatar'] = !empty($customer['avatar']) ? $customer['avatar'] : '../../../assets/dist/img/avatar/01.jpg';

// Get monthly order data for chart (if methods exist)
$monthlyOrders = [];

// Get revenue data for current month
$currentMonthRevenue = 0;
?>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Customer Dashboard - Rent Ivation</title>
    <!-- App favicon -->
    <link rel="shortcut icon" href="../assets/images/favicon.png">
    <!-- Global Styles(used by all pages) -->
    <link href="../assets/plugins_dashboard/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/plugins_dashboard/metisMenu/metisMenu.min.css" rel="stylesheet">
    <link href="../assets/plugins_dashboard/fontawesome/css/all.min.css" rel="stylesheet">
    <!-- Third party Styles(used by this page) -->
    <link href="../assets/plugins_dashboard/toastr/toastr.css" rel="stylesheet">
    <link href="../assets/plugins_dashboard/datatables/dataTables.bootstrap5.min.css" rel="stylesheet">
    <!-- App css -->
    <link href="../assets/dist/css/app.min.css" rel="stylesheet">
    <!-- Start Your Custom Style Now -->
    <link href="../assets/dist/css/style.css" rel="stylesheet">