<?php
session_start();
include('../../../includes/config.php');

if (!isset($_SESSION['user_id'])) {
    header("Location: ../../../index.php");
    exit();
}

$user_id = $_SESSION['user_id'];

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid message ID.");
}

$message_id = (int)$_GET['id'];

// Fetch the original message (ensure user is receiver)
$stmt = $conn->prepare("
    SELECT m.*, u.first_name, u.last_name 
    FROM messages m 
    JOIN users u ON m.sender_id = u.id 
    WHERE m.id = ? AND m.receiver_id = ?
");
if (!$stmt) {
    die("Prepare failed: " . $conn->error);
}
$stmt->bind_param("ii", $message_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$original = $result->fetch_assoc();
$stmt->close();

if (!$original) {
    die("Message not found or you do not have permission to reply.");
}

// Fix ghost thread_id: assign thread_id to original if missing
if (empty($original['thread_id'])) {
    $thread_id = $original['id'];
    $update = $conn->prepare("UPDATE messages SET thread_id = ? WHERE id = ?");
    $update->bind_param("ii", $thread_id, $thread_id);
    $update->execute();
    $update->close();
} else {
    $thread_id = $original['thread_id'];
}

$reply_to_id = $original['sender_id'];
$original_subject = $original['subject'];
$reply_subject = (stripos($original_subject, 'Re:') === 0) ? $original_subject : 'Re: ' . $original_subject;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Reply to Message</title>
    <link rel="stylesheet" href="../../../css/messaging.css" />
    <style>
        .container {
            max-width: 600px;
            margin: 30px auto;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        label {
            font-weight: bold;
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 1em;
        }
        button, .btn {
            padding: 10px 18px;
            border: none;
            background-color: #007bff;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            font-size: 1em;
            margin-right: 10px;
        }
        .btn:hover, button:hover {
            background-color: #0056b3;
        }
        .btn.cancel {
            background-color: #6c757d;
        }
        .btn.cancel:hover {
            background-color: #5a6268;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>Reply to <?= htmlspecialchars($original['first_name'] . ' ' . $original['last_name']) ?></h2>

    <form action="send_message.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="receiver_id" value="<?= $reply_to_id ?>">
        <input type="hidden" name="parent_id" value="<?= $message_id ?>">
        <input type="hidden" name="thread_id" value="<?= $thread_id ?>">
        <input type="hidden" name="redirect" value="view.php?id=<?= $message_id ?>">

        <div>
            <label for="subject">Subject:</label>
            <input type="text" id="subject" name="subject" value="<?= htmlspecialchars($reply_subject) ?>" required>
        </div>

        <div>
            <label for="body">Message:</label>
            <textarea id="body" name="body" rows="8" required></textarea>
        </div>

        <div>
            <label for="attachment">Attach File (Optional):</label>
            <input type="file" id="attachment" name="attachment" />
        </div>

        <button type="submit">Send Reply</button>
        <button type="button" class="btn cancel" onclick="window.location.href='view.php?id=<?= $message_id ?>'">Cancel</button>
    </form>
</div>
</body>
</html>
