<?php
require_once '../includes/User.php';
require_once '../includes/db_config.php';

if (!User::isLoggedIn() || !User::isAdmin()) {
    header("Location: /allura/login.php");
    exit();
}

$currentUser = User::getCurrentUser();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'update_status' && isset($_POST['message_id']) && isset($_POST['status'])) {
        try {
            $pdo = getDBConnection();
            $stmt = $pdo->prepare("UPDATE contact_messages SET status = ? WHERE id = ?");
            $stmt->execute([$_POST['status'], $_POST['message_id']]);
            $success_message = "Message status updated successfully.";
        } catch (PDOException $e) {
            $error_message = "Error updating message status.";
        }
    }
}

try {
    $pdo = getDBConnection();
    $stmt = $pdo->prepare("SELECT * FROM contact_messages ORDER BY created_at DESC");
    $stmt->execute();
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $error_message = "Error fetching messages.";
    $messages = [];
}

$new_count = 0;
$read_count = 0;
$replied_count = 0;

foreach ($messages as $message) {
    switch ($message['status']) {
        case 'new':
            $new_count++;
            break;
        case 'read':
            $read_count++;
            break;
        case 'replied':
            $replied_count++;
            break;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Messages - Admin Panel</title>
    <?php include '../includes/header.php'; ?>
</head>
<body>
    <div class="admin-contact-container">
        <div class="admin-contact-header">
            <h1>Contact Messages Management</h1>
            <p>Manage customer inquiries and messages</p>
        </div>
        
        <?php if (isset($success_message)): ?>
            <div class="admin-contact-success">
                <i class="fas fa-check-circle"></i>
                <?php echo htmlspecialchars($success_message); ?>
            </div>
        <?php endif; ?>
        
        <?php if (isset($error_message)): ?>
            <div class="admin-contact-error">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo htmlspecialchars($error_message); ?>
            </div>
        <?php endif; ?>
        
        <div class="admin-contact-stats">
            <div class="admin-contact-stat-card">
                <div class="admin-contact-stat-icon">
                    <i class="fas fa-envelope"></i>
                </div>
                <div class="admin-contact-stat-content">
                    <h3><?php echo count($messages); ?></h3>
                    <p>Total Messages</p>
                </div>
            </div>
            
            <div class="admin-contact-stat-card new">
                <div class="admin-contact-stat-icon">
                    <i class="fas fa-exclamation-circle"></i>
                </div>
                <div class="admin-contact-stat-content">
                    <h3><?php echo $new_count; ?></h3>
                    <p>New Messages</p>
                </div>
            </div>
            
            <div class="admin-contact-stat-card read">
                <div class="admin-contact-stat-icon">
                    <i class="fas fa-eye"></i>
                </div>
                <div class="admin-contact-stat-content">
                    <h3><?php echo $read_count; ?></h3>
                    <p>Read Messages</p>
                </div>
            </div>
            
            <div class="admin-contact-stat-card replied">
                <div class="admin-contact-stat-icon">
                    <i class="fas fa-reply"></i>
                </div>
                <div class="admin-contact-stat-content">
                    <h3><?php echo $replied_count; ?></h3>
                    <p>Replied Messages</p>
                </div>
            </div>
        </div>
        
        <div class="admin-contact-messages-container">
            <h2>All Messages</h2>
            
            <?php if (empty($messages)): ?>
                <div class="admin-contact-no-messages">
                    <i class="fas fa-inbox"></i>
                    <p>No messages found.</p>
                </div>
            <?php else: ?>
                <div class="admin-contact-messages-list">
                    <?php foreach ($messages as $message): ?>
                        <div class="admin-contact-message-card status-<?php echo $message['status']; ?>">
                            <div class="admin-contact-message-header">
                                <div class="admin-contact-message-info">
                                    <h3><?php echo htmlspecialchars($message['subject']); ?></h3>
                                    <p class="admin-contact-message-meta">
                                        <strong><?php echo htmlspecialchars($message['name']); ?></strong> 
                                        (<?php echo htmlspecialchars($message['email']); ?>)
                                        <?php if ($message['phone']): ?>
                                            - <?php echo htmlspecialchars($message['phone']); ?>
                                        <?php endif; ?>
                                    </p>
                                    <p class="admin-contact-message-date">
                                        <i class="fas fa-clock"></i>
                                        <?php echo date('M j, Y g:i A', strtotime($message['created_at'])); ?>
                                    </p>
                                </div>
                                
                                <div class="admin-contact-message-actions">
                                    <form method="POST" style="display: inline;">
                                        <input type="hidden" name="action" value="update_status">
                                        <input type="hidden" name="message_id" value="<?php echo $message['id']; ?>">
                                        <select name="status" onchange="this.form.submit()" class="admin-contact-status-select">
                                            <option value="new" <?php echo $message['status'] === 'new' ? 'selected' : ''; ?>>New</option>
                                            <option value="read" <?php echo $message['status'] === 'read' ? 'selected' : ''; ?>>Read</option>
                                            <option value="replied" <?php echo $message['status'] === 'replied' ? 'selected' : ''; ?>>Replied</option>
                                        </select>
                                    </form>
                                </div>
                            </div>
                            
                            <div class="admin-contact-message-content">
                                <p><?php echo nl2br(htmlspecialchars($message['message'])); ?></p>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <?php include '../includes/footer.php'; ?>
</body>
</html>
