CREATE TABLE IF NOT EXISTS telegram_users (
    id BIGSERIAL PRIMARY KEY,
    telegram_id BIGINT NOT NULL UNIQUE,
    username TEXT,
    first_name TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS vpn_users (
    id BIGSERIAL PRIMARY KEY,
    telegram_id BIGINT,
    remnawave_uuid UUID NOT NULL UNIQUE,
    remnawave_username VARCHAR(36) NOT NULL,
    external_squad_uuid UUID,
    internal_squad_uuids UUID[] NOT NULL DEFAULT '{}',
    expire_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_vpn_users_telegram ON vpn_users(telegram_id);
CREATE INDEX IF NOT EXISTS idx_vpn_users_username ON vpn_users(remnawave_username);

CREATE TABLE IF NOT EXISTS admin_wizard (
    admin_telegram_id BIGINT PRIMARY KEY,
    step TEXT NOT NULL,
    data JSONB NOT NULL DEFAULT '{}',
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
