from yoyo import step

__depends__ = {"20240211_00_yto88-discord-channel-table-and-discord-posts-revisions"}

steps = [
    step("""
    CREATE TYPE unapproved_link_status AS ENUM ('pending', 'approved', 'rejected');
    CREATE TABLE unapproved_link_requests (
        id int PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
        from_service text NOT NULL,
        from_id text NOT NULL,
        to_service text NOT NULL,
        to_id text NOT NULL,
        reason text,
        requester_id int NOT NULL REFERENCES account (id),
        status unapproved_link_status NOT NULL DEFAULT 'pending',

        FOREIGN KEY (from_service, from_id) REFERENCES lookup (service, id),
        FOREIGN KEY (to_service, to_id) REFERENCES lookup (service, id),
        UNIQUE (from_service, from_id, to_service, to_id)
    );

    CREATE INDEX unapproved_link_requests_status_id_idx ON unapproved_link_requests (status, id);
    """),
    step("""
    UPDATE lookup SET relation_id = nextval('lookup_relation_id_seq');
    CREATE INDEX lookup_public_id_idx ON lookup (public_id);
    CREATE INDEX lookup_relation_id_idx ON lookup (relation_id);
    """)
]
