ALTER TABLE users
    ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'active',
    ADD COLUMN IF NOT EXISTS invited_at TIMESTAMPTZ NULL,
    ADD COLUMN IF NOT EXISTS disabled_at TIMESTAMPTZ NULL;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'users_status_valid_chk'
    ) THEN
        ALTER TABLE users
            ADD CONSTRAINT users_status_valid_chk
            CHECK (status IN ('active', 'disabled', 'invited'));
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_users_status ON users(status);
