-- Add newsletter sent tracking table
CREATE TABLE IF NOT EXISTS newsletter_sent_log (
    id SERIAL PRIMARY KEY,
    newsletter_type VARCHAR(50) NOT NULL, -- weekly|match_reminder|match_result|blog_release
    subject VARCHAR(500),
    content_ids TEXT, -- JSON array of article/match IDs included
    recipients_count INT DEFAULT 0,
    sent_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_newsletter_sent_log_type ON newsletter_sent_log(newsletter_type);
CREATE INDEX IF NOT EXISTS idx_newsletter_sent_log_sent_at ON newsletter_sent_log(sent_at);

-- Add match notification tracking to prevent duplicate alerts
CREATE TABLE IF NOT EXISTS match_notifications (
    id SERIAL PRIMARY KEY,
    match_id VARCHAR(255) NOT NULL, -- External FACR match ID
    notification_type VARCHAR(50) NOT NULL, -- reminder_48h|reminder_day|result
    sent_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    recipients_count INT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    UNIQUE(match_id, notification_type)
);

CREATE INDEX IF NOT EXISTS idx_match_notifications_match_id ON match_notifications(match_id);
CREATE INDEX IF NOT EXISTS idx_match_notifications_sent_at ON match_notifications(sent_at);

-- Add blog notification tracking to prevent duplicate alerts
CREATE TABLE IF NOT EXISTS blog_notifications (
    id SERIAL PRIMARY KEY,
    article_id INT NOT NULL,
    sent_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    recipients_count INT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    UNIQUE(article_id)
);

CREATE INDEX IF NOT EXISTS idx_blog_notifications_article_id ON blog_notifications(article_id);
CREATE INDEX IF NOT EXISTS idx_blog_notifications_sent_at ON blog_notifications(sent_at);
