CREATE TABLE shop_invitations
(
    id           BIGINT PRIMARY KEY,
    shop_id      BIGINT      NOT NULL REFERENCES shops (id),
    player_id    BIGINT      NOT NULL,
    status       VARCHAR(20) NOT NULL DEFAULT 'pending',
    invited_by   BIGINT      NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    responded_at TIMESTAMPTZ,

    CONSTRAINT chk_invitation_status CHECK (status IN ('pending', 'accepted', 'rejected', 'cancelled'))
);

CREATE UNIQUE INDEX idx_unique_pending_invitation
    ON shop_invitations (shop_id, player_id)
    WHERE status = 'pending';
CREATE INDEX idx_invitations_shop ON shop_invitations (shop_id);
CREATE INDEX idx_invitations_player ON shop_invitations (player_id) WHERE status = 'pending';
CREATE INDEX idx_invitations_player_status ON shop_invitations (player_id, status, created_at DESC);
CREATE INDEX idx_invitations_shop_status ON shop_invitations (shop_id, status, created_at DESC);