CREATE TABLE if not exists blameless_events
(
    id          SERIAL PRIMARY KEY,
    incident_id integer                NOT NULL,
    event_id    integer                NOT NULL,
    type        character varying(100) NOT NULL,
    data_json   jsonb,
    date        timestamp without time zone,
    created_at  timestamp without time zone,
    updated_at  timestamp without time zone,
    deleted_at  timestamp without time zone
);

CREATE TABLE if not exists blameless_incidents
(
    id                          SERIAL PRIMARY KEY,
    incident_id                 integer                NOT NULL,
    description                 text,
    status                      character varying(100) NOT NULL,
    severity                    text                   NOT NULL,
    is_deleted                  boolean,
    summary                     text,
    type                        character varying(100),
    start_of_incident_impact    timestamp without time zone,
    start_of_customer_impact    timestamp without time zone,
    duration_of_customer_impact integer,
    data_json                   jsonb,
    incident_created_at         timestamp without time zone,
    created_at                  timestamp without time zone,
    updated_at                  timestamp without time zone,
    deleted_at                  timestamp without time zone
);

CREATE TABLE if not exists dbz_heartbeat
(
    id SERIAL PRIMARY KEY,
    ts timestamp with time zone
);

CREATE TABLE if not exists deployment_config_mapping
(
    id                    SERIAL PRIMARY KEY,
    alert_id              character varying(64),
    manifest_id           character varying(255),
    deployment_portal_url character varying(255),
    priority              integer,
    created_at            timestamp without time zone,
    updated_at            timestamp without time zone,
    deleted_at            timestamp without time zone
);

CREATE TABLE if not exists deployment_mapping
(
    id                    SERIAL PRIMARY KEY,
    alert_id              character varying(64),
    deployment_portal_url character varying(255),
    service_name          character varying(255),
    priority              integer,
    created_at            timestamp without time zone,
    updated_at            timestamp without time zone,
    deleted_at            timestamp without time zone
);

CREATE TABLE if not exists diagnose_runbook
(
    id          SERIAL PRIMARY KEY,
    alert_id    character varying(64),
    alert_name  character varying(255),
    description character varying(255),
    is_active   character varying(255),
    created_at  timestamp without time zone,
    updated_at  timestamp without time zone,
    deleted_at  timestamp without time zone
);

CREATE TABLE if not exists grafana_mapping
(
    id           SERIAL PRIMARY KEY,
    alert_id     character varying(64),
    endpoint_url character varying(255),
    priority     integer,
    created_at   timestamp without time zone,
    updated_at   timestamp without time zone,
    deleted_at   timestamp without time zone
);

CREATE TABLE if not exists houston_user
(
    id            SERIAL PRIMARY KEY,
    name          character varying(50),
    slack_user_id character varying(100),
    active        boolean DEFAULT true,
    created_at    timestamp without time zone,
    updated_at    timestamp without time zone,
    deleted_at    timestamp without time zone
);

CREATE TABLE if not exists severity
(
    id             SERIAL PRIMARY KEY,
    name           character varying(50) NOT NULL UNIQUE,
    description    text,
    version        bigint              DEFAULT 0,
    sla            integer,
    slack_user_ids character varying[] DEFAULT '{}'::character varying[],
    created_at     timestamp without time zone,
    updated_at     timestamp without time zone,
    deleted_at     timestamp without time zone
);

CREATE TABLE if not exists incident_status
(
    id                 SERIAL PRIMARY KEY,
    name               character varying(50) NOT NULL UNIQUE,
    description        text,
    is_terminal_status boolean DEFAULT false,
    version            bigint  DEFAULT 0,
    created_at         timestamp without time zone,
    updated_at         timestamp without time zone,
    deleted_at         timestamp without time zone
);

CREATE TABLE if not exists team
(
    id                    SERIAL PRIMARY KEY,
    name                  character varying(50) NOT NULL UNIQUE,
    slack_user_ids        character varying[] DEFAULT '{}'::character varying[],
    active                boolean             DEFAULT false,
    confluence_link       text,
    version               bigint              DEFAULT 0,
    oncall_handle         character varying(50),
    created_at            timestamp without time zone,
    updated_at            timestamp without time zone,
    deleted_at            timestamp without time zone,
    webhook_slack_channel character varying(50),
    manager_handle        character varying(50),
    pse_oncall_handle     character varying
);

CREATE TABLE if not exists incident
(
    id              SERIAL PRIMARY KEY,
    title           text,
    description     text,
    status          integer NOT NULL REFERENCES incident_status (id),
    severity_id     integer NOT NULL REFERENCES severity (id),
    incident_name   text,
    slack_channel   character varying(100),
    detection_time  timestamp without time zone,
    start_time      timestamp without time zone,
    end_time        timestamp without time zone,
    team_id         integer NOT NULL REFERENCES team (id),
    jira_id         character varying(100),
    confluence_id   character varying(100),
    created_by      character varying(100),
    updated_by      character varying(100),
    severity_tat    timestamp without time zone,
    remind_me_at    timestamp without time zone,
    enable_reminder boolean DEFAULT false,
    created_at      timestamp without time zone,
    updated_at      timestamp without time zone,
    deleted_at      timestamp without time zone,
    meta_data       jsonb,
    rca_text        character varying,
    CONSTRAINT fk_status FOREIGN KEY (status) REFERENCES incident_status (id),
    CONSTRAINT fk_severity FOREIGN KEY (severity_id) REFERENCES severity (id),
    CONSTRAINT fk_team FOREIGN KEY (team_id) REFERENCES team (id)
);

CREATE INDEX if not exists incident_team_id_idx ON incident (team_id int4_ops);
CREATE INDEX if not exists incident_severity_id_idx ON incident (severity_id int4_ops);
CREATE INDEX if not exists incident_status_idx ON incident (status int4_ops);
CREATE INDEX if not exists incident_slack_channel_idx ON incident (slack_channel text_ops);
CREATE INDEX if not exists incident_deleted_at_idx ON incident (deleted_at timestamp_ops);
CREATE INDEX if not exists incident_updated_at_idx ON incident (updated_at timestamp_ops);
CREATE INDEX if not exists incident_severity_tat_idx ON incident (severity_tat timestamp_ops);

CREATE TABLE if not exists incident_channel
(
    id                SERIAL PRIMARY KEY,
    slack_channel     character varying(100),
    incident_id       integer NOT NULL REFERENCES incident (id),
    message_timestamp character varying(100),
    version           bigint DEFAULT 0,
    created_at        timestamp without time zone,
    updated_at        timestamp without time zone,
    deleted_at        timestamp without time zone,
    CONSTRAINT fk_incident FOREIGN KEY (incident_id) REFERENCES incident (id)
);

CREATE INDEX if not exists incident_channel_incident_id_idx ON incident_channel (incident_id int4_ops);
CREATE INDEX if not exists incident_channel_slack_channel_idx ON incident_channel (slack_channel text_ops);

CREATE TABLE if not exists incident_role
(
    id          SERIAL PRIMARY KEY,
    incident_id integer NOT NULL REFERENCES incident (id),
    role        character varying(100),
    assigned_to character varying(100),
    assigned_by character varying(100),
    created_at  timestamp without time zone,
    updated_at  timestamp without time zone,
    deleted_at  timestamp without time zone,
    CONSTRAINT fk_incident FOREIGN KEY (incident_id) REFERENCES incident (id)
);

CREATE INDEX if not exists incident_role_incident_id_idx ON incident_role (incident_id int4_ops);

CREATE TABLE if not exists tag
(
    id           SERIAL PRIMARY KEY,
    name         character varying      NOT NULL UNIQUE,
    label        text                   NOT NULL,
    place_holder text,
    action_id    character varying(100) NOT NULL,
    type         character varying(100) NOT NULL,
    created_at   timestamp without time zone,
    updated_at   timestamp without time zone,
    deleted_at   timestamp without time zone
);

CREATE TABLE if not exists incident_tag
(
    id              SERIAL PRIMARY KEY,
    incident_id     integer NOT NULL REFERENCES incident (id),
    tag_id          integer NOT NULL REFERENCES tag (id),
    tag_value_ids   integer[] DEFAULT '{}'::integer[],
    free_text_value text,
    created_at      timestamp without time zone,
    updated_at      timestamp without time zone,
    deleted_at      timestamp without time zone,
    CONSTRAINT fk_incident FOREIGN KEY (incident_id) REFERENCES incident (id),
    CONSTRAINT fk_tag FOREIGN KEY (tag_id) REFERENCES tag (id)
);

CREATE INDEX if not exists incident_tag_incident_id_idx ON incident_tag (incident_id int4_ops);
CREATE INDEX if not exists incident_tag_tag_id_idx ON incident_tag (tag_id int4_ops);

CREATE TABLE if not exists kibana_mapping
(
    id           SERIAL PRIMARY KEY,
    alert_id     character varying(64),
    service_name character varying(255),
    priority     integer,
    created_at   timestamp without time zone,
    updated_at   timestamp without time zone,
    deleted_at   timestamp without time zone
);

CREATE TABLE if not exists role
(
    id         SERIAL PRIMARY KEY,
    name       character varying(100) NOT NULL UNIQUE,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    deleted_at timestamp without time zone
);

CREATE TABLE if not exists shedlock
(
    name         character varying(64) PRIMARY KEY,
    lock_until   timestamp without time zone,
    locked_at    timestamp without time zone,
    locked_by    character varying(255),
    unlocked_by  character varying(64),
    locked_value boolean DEFAULT true
);

CREATE TABLE if not exists tag_value
(
    id         SERIAL PRIMARY KEY,
    tag_id     integer           NOT NULL REFERENCES tag (id),
    value      character varying NOT NULL,
    create_at  timestamp without time zone,
    updated_at timestamp without time zone,
    deleted_at timestamp without time zone,
    CONSTRAINT fk_tag FOREIGN KEY (tag_id) REFERENCES tag (id)
);
CREATE INDEX if not exists tag_value_tag_id_idx ON tag_value (tag_id int4_ops);

CREATE TABLE if not exists team_tag
(
    id         SERIAL PRIMARY KEY,
    team_id    integer NOT NULL REFERENCES team (id),
    tag_id     integer NOT NULL REFERENCES tag (id),
    optional   boolean DEFAULT false,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    deleted_at timestamp without time zone,
    CONSTRAINT fk_team FOREIGN KEY (team_id) REFERENCES team (id),
    CONSTRAINT fk_tag FOREIGN KEY (tag_id) REFERENCES tag (id)
);

CREATE INDEX if not exists team_tag_team_id_idx ON team_tag (team_id int4_ops);
CREATE INDEX if not exists team_tag_tag_id_idx ON team_tag (tag_id int4_ops);