CREATE TABLE IF NOT EXISTS channels (
    id UUID NOT NULL PRIMARY KEY,
    team_id UUID NOT NULL REFERENCES teams(id),
    name TEXT NOT NULL,
    enable_by_default BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS email_channels (
    id UUID NOT NULL PRIMARY KEY REFERENCES channels(id) ON DELETE CASCADE,
    recipient TEXT NOT NULL DEFAULT '',
    verification_code TEXT NOT NULL DEFAULT '',
    verification_code_guesses INT NOT NULL DEFAULT 0,
    verified BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS slack_channels (
    id UUID NOT NULL PRIMARY KEY REFERENCES channels(id) ON DELETE CASCADE,
    oauth_state TEXT NOT NULL DEFAULT '',
    access_token TEXT NOT NULL DEFAULT '',
    conversation_id TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS channel_selections (
    project_id UUID NOT NULL REFERENCES projects(id),
    channel_id UUID NOT NULL REFERENCES channels(id),
    PRIMARY KEY (project_id, channel_id)
);
CREATE INDEX ON channel_selections (project_id);
CREATE INDEX ON channel_selections (channel_id);
