<?php
require_once '../config/database.php';
require_once '../includes/admin_auth.php';

$adminAuth = new AdminAuth();
if (!$adminAuth->isLoggedIn()) {
    exit('Unauthorized');
}

$investment_id = (int)($_GET['investment_id'] ?? 0);
if (!$investment_id) exit('Invalid ID');

$db = Database::getInstance()->getConnection();

// Ensure table exists
$db->exec("CREATE TABLE IF NOT EXISTS investment_notes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    investment_id INT NOT NULL,
    admin_id INT NOT NULL,
    note TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

$stmt = $db->prepare("SELECT n.*, a.username FROM investment_notes n LEFT JOIN admin_users a ON n.admin_id = a.id WHERE n.investment_id = ? ORDER BY n.created_at DESC");
$stmt->execute([$investment_id]);
$notes = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (empty($notes)) {
    echo '<p class="text-gray-500 mb-4">No notes yet.</p>';
} else {
    foreach ($notes as $note) {
        echo '<div class="bg-gray-50 p-3 rounded-lg mb-3">';
        echo '<div class="flex justify-between text-xs text-gray-500 mb-1">';
        echo '<span>' . htmlspecialchars($note['username']) . '</span>';
        echo '<span>' . date('M d, Y H:i', strtotime($note['created_at'])) . '</span>';
        echo '</div>';
        echo '<p class="text-gray-700">' . nl2br(htmlspecialchars($note['note'])) . '</p>';
        echo '</div>';
    }
}
?>