<?php
require_once __DIR__ . '/../Core.php';

class ContactNote extends Model
{
    public function forContact(int $contactId): array
    {
        $stmt = $this->db->prepare('SELECT n.*, u.name AS created_by_name FROM contact_notes n LEFT JOIN users u ON u.id = n.created_by WHERE n.contact_id = ? ORDER BY n.id DESC');
        $stmt->execute([$contactId]);
        return $stmt->fetchAll();
    }

    public function create(array $data): int
    {
        $stmt = $this->db->prepare('INSERT INTO contact_notes (contact_id, note_body, visibility, created_by) VALUES (?, ?, ?, ?)');
        $stmt->execute([
            (int)$data['contact_id'],
            trim((string)($data['note_body'] ?? '')),
            $data['visibility'] ?? 'internal',
            current_user()['id'] ?? null,
        ]);
        return (int)$this->db->lastInsertId();
    }
}
