"""initial schema

Revision ID: 03e5f186a9d5
Revises:
Create Date: 2026-02-05 13:27:47.649534

"""

from typing import Sequence, Union

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "03e5f186a9d5"
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "games",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("name", sa.String(100), nullable=False),
        sa.Column("slug", sa.String(100), nullable=False, unique=True),
        sa.Column("generation", sa.SmallInteger(), nullable=False),
        sa.Column("region", sa.String(50), nullable=False),
        sa.Column("box_art_url", sa.String(500), nullable=True),
        sa.Column("release_year", sa.SmallInteger(), nullable=True),
    )

    op.create_table(
        "routes",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("name", sa.String(100), nullable=False),
        sa.Column(
            "game_id", sa.Integer(), sa.ForeignKey("games.id"), nullable=False
        ),
        sa.Column("order", sa.SmallInteger(), nullable=False),
    )
    op.create_index("ix_routes_game_id", "routes", ["game_id"])

    op.create_table(
        "pokemon",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "national_dex", sa.SmallInteger(), nullable=False, unique=True
        ),
        sa.Column("name", sa.String(50), nullable=False),
        sa.Column(
            "types", postgresql.ARRAY(sa.String(20)), nullable=False
        ),
        sa.Column("sprite_url", sa.String(500), nullable=True),
    )

    op.create_table(
        "route_encounters",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "route_id", sa.Integer(), sa.ForeignKey("routes.id"), nullable=False
        ),
        sa.Column(
            "pokemon_id",
            sa.Integer(),
            sa.ForeignKey("pokemon.id"),
            nullable=False,
        ),
        sa.Column("encounter_method", sa.String(30), nullable=False),
        sa.Column("encounter_rate", sa.SmallInteger(), nullable=False),
        sa.UniqueConstraint(
            "route_id",
            "pokemon_id",
            "encounter_method",
            name="uq_route_pokemon_method",
        ),
    )
    op.create_index(
        "ix_route_encounters_route_id", "route_encounters", ["route_id"]
    )
    op.create_index(
        "ix_route_encounters_pokemon_id", "route_encounters", ["pokemon_id"]
    )

    op.create_table(
        "nuzlocke_runs",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "game_id", sa.Integer(), sa.ForeignKey("games.id"), nullable=False
        ),
        sa.Column("name", sa.String(100), nullable=False),
        sa.Column("status", sa.String(20), nullable=False),
        sa.Column(
            "rules", postgresql.JSONB(), nullable=False, server_default="{}"
        ),
        sa.Column(
            "started_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "completed_at", sa.DateTime(timezone=True), nullable=True
        ),
    )
    op.create_index(
        "ix_nuzlocke_runs_game_id", "nuzlocke_runs", ["game_id"]
    )
    op.create_index(
        "ix_nuzlocke_runs_status", "nuzlocke_runs", ["status"]
    )

    op.create_table(
        "encounters",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "run_id",
            sa.Integer(),
            sa.ForeignKey("nuzlocke_runs.id"),
            nullable=False,
        ),
        sa.Column(
            "route_id", sa.Integer(), sa.ForeignKey("routes.id"), nullable=False
        ),
        sa.Column(
            "pokemon_id",
            sa.Integer(),
            sa.ForeignKey("pokemon.id"),
            nullable=False,
        ),
        sa.Column("nickname", sa.String(50), nullable=True),
        sa.Column("status", sa.String(20), nullable=False),
        sa.Column("catch_level", sa.SmallInteger(), nullable=True),
        sa.Column("faint_level", sa.SmallInteger(), nullable=True),
        sa.Column(
            "caught_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )
    op.create_index("ix_encounters_run_id", "encounters", ["run_id"])
    op.create_index("ix_encounters_route_id", "encounters", ["route_id"])
    op.create_index("ix_encounters_pokemon_id", "encounters", ["pokemon_id"])


def downgrade() -> None:
    op.drop_table("encounters")
    op.drop_table("nuzlocke_runs")
    op.drop_table("route_encounters")
    op.drop_table("pokemon")
    op.drop_table("routes")
    op.drop_table("games")
