-- Safe domains table: approved domains for link previews and external link warnings
CREATE TABLE IF NOT EXISTS safe_domains (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    domain TEXT NOT NULL UNIQUE,
    category TEXT NOT NULL DEFAULT 'general',  -- general, news, social, government, education, etc.
    is_approved BOOLEAN NOT NULL DEFAULT true,  -- true = safe, false = explicitly blocked
    notes TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_safe_domains_domain ON safe_domains (domain);
CREATE INDEX IF NOT EXISTS idx_safe_domains_approved ON safe_domains (is_approved);

-- Seed with common safe domains
INSERT INTO safe_domains (domain, category, is_approved, notes) VALUES
    -- News
    ('npr.org', 'news', true, 'National Public Radio'),
    ('apnews.com', 'news', true, 'Associated Press'),
    ('bringmethenews.com', 'news', true, 'Bring Me The News - Minnesota'),
    ('reuters.com', 'news', true, 'Reuters'),
    ('bbc.com', 'news', true, 'BBC'),
    ('bbc.co.uk', 'news', true, 'BBC UK'),
    ('nytimes.com', 'news', true, 'New York Times'),
    ('washingtonpost.com', 'news', true, 'Washington Post'),
    ('theguardian.com', 'news', true, 'The Guardian'),
    ('startribune.com', 'news', true, 'Star Tribune - Minnesota'),
    ('mprnews.org', 'news', true, 'MPR News - Minnesota'),
    ('kstp.com', 'news', true, 'KSTP - Minnesota'),
    ('kare11.com', 'news', true, 'KARE 11 - Minnesota'),
    ('fox9.com', 'news', true, 'Fox 9 - Minnesota'),
    ('wcco.com', 'news', true, 'WCCO - Minnesota'),
    -- Social / Tech
    ('wikipedia.org', 'education', true, 'Wikipedia'),
    ('github.com', 'tech', true, 'GitHub'),
    ('youtube.com', 'social', true, 'YouTube'),
    ('youtu.be', 'social', true, 'YouTube short links'),
    ('vimeo.com', 'social', true, 'Vimeo'),
    ('spotify.com', 'social', true, 'Spotify'),
    ('open.spotify.com', 'social', true, 'Spotify Open'),
    ('soundcloud.com', 'social', true, 'SoundCloud'),
    ('twitch.tv', 'social', true, 'Twitch'),
    ('reddit.com', 'social', true, 'Reddit'),
    ('imgur.com', 'social', true, 'Imgur'),
    -- Government
    ('gov', 'government', true, 'US Government domains'),
    ('state.mn.us', 'government', true, 'Minnesota State'),
    ('minneapolismn.gov', 'government', true, 'City of Minneapolis'),
    ('stpaul.gov', 'government', true, 'City of St. Paul'),
    -- Sojorn
    ('sojorn.net', 'internal', true, 'Sojorn'),
    ('gosojorn.com', 'internal', true, 'Sojorn legacy')
ON CONFLICT (domain) DO NOTHING;
