-- Create the sources table.
-- Each user can save custom news sources (URLs) for their syntheses.
-- A unique constraint on (user_id, url) prevents duplicate URLs per user.

CREATE TABLE sources (
    id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id    UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    title      VARCHAR(200) NOT NULL CHECK (char_length(title) BETWEEN 1 AND 200),
    url        VARCHAR(1000) NOT NULL CHECK (char_length(url) <= 1000),
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_sources_user_id ON sources(user_id);
CREATE UNIQUE INDEX idx_sources_user_id_url ON sources(user_id, url);
