CREATE TABLE IF NOT EXISTS user_email_notify (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    "user" UUID NOT NULL,
    email TEXT NOT NULL,
    subject TEXT NOT NULL,
    template TEXT NOT NULL,
    body_text TEXT,
    body_html TEXT,
    notify_type TEXT NOT NULL,
    target_type TEXT,
    target_id UUID,
    metadata TEXT,
    status TEXT NOT NULL DEFAULT 'queued',
    provider_message_id TEXT,
    error TEXT,
    retry_count INTEGER NOT NULL DEFAULT 0,
    queued_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    sent_at TIMESTAMPTZ,
    delivered_at TIMESTAMPTZ,
    opened_at TIMESTAMPTZ,
    clicked_at TIMESTAMPTZ,
    failed_at TIMESTAMPTZ
);
