"""GSI with range key - query users by status with sorting."""

from pydynox import DynamoDBClient, GlobalSecondaryIndex, Model, ModelConfig, set_default_client
from pydynox.attributes import NumberAttribute, StringAttribute

# Setup client
client = DynamoDBClient(endpoint_url="http://localhost:8000")
set_default_client(client)


class User(Model):
    """User model with status GSI that has a range key."""

    model_config = ModelConfig(table="users_status")

    pk = StringAttribute(hash_key=True)
    sk = StringAttribute(range_key=True)
    email = StringAttribute()
    status = StringAttribute()
    name = StringAttribute()
    age = NumberAttribute()

    # GSI with range key for sorting
    status_index = GlobalSecondaryIndex(
        index_name="status-index",
        hash_key="status",
        range_key="pk",  # Sort by pk within each status
    )


# Create table with GSI
if not client.table_exists("users_status"):
    client.create_table(
        "users_status",
        hash_key=("pk", "S"),
        range_key=("sk", "S"),
        global_secondary_indexes=[
            {
                "index_name": "status-index",
                "hash_key": ("status", "S"),
                "range_key": ("pk", "S"),
                "projection": "ALL",
            }
        ],
    )

# Create users with different statuses
User(
    pk="USER#1", sk="PROFILE", email="john@example.com", status="active", name="John", age=30
).save()
User(
    pk="USER#2", sk="PROFILE", email="jane@example.com", status="active", name="Jane", age=25
).save()
User(
    pk="USER#3", sk="PROFILE", email="bob@example.com", status="inactive", name="Bob", age=35
).save()
User(
    pk="ADMIN#1", sk="PROFILE", email="admin@example.com", status="active", name="Admin", age=40
).save()

# Query all active users
print("All active users:")
for user in User.status_index.query(status="active"):
    print(f"  {user.name} (pk={user.pk})")

# Query active users with pk starting with "USER#"
print("\nActive users (not admins):")
for user in User.status_index.query(
    status="active",
    range_key_condition=User.pk.begins_with("USER#"),
):
    print(f"  {user.name} (pk={user.pk})")

# Query in descending order
print("\nActive users (descending by pk):")
for user in User.status_index.query(status="active", scan_index_forward=False):
    print(f"  {user.name} (pk={user.pk})")
