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

class ConsentLog extends Model
{
    public function create(array $data): int
    {
        $stmt = $this->db->prepare('INSERT INTO consent_logs (contact_id, channel, status, source, proof_note, ip_address, user_agent, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
        $stmt->execute([
            $data['contact_id'] ?? null,
            $data['channel'] ?? 'email',
            $data['status'] ?? 'opt_in',
            $data['source'] ?? 'manual',
            $data['proof_note'] ?? null,
            $data['ip_address'] ?? null,
            $data['user_agent'] ?? null,
            $data['created_by'] ?? null,
        ]);
        return (int)$this->db->lastInsertId();
    }

    public function forContact(int $contactId): array
    {
        $stmt = $this->db->prepare('SELECT * FROM consent_logs WHERE contact_id = ? ORDER BY created_at DESC');
        $stmt->execute([$contactId]);
        return $stmt->fetchAll();
    }
}
