"""Initial migration

Revision ID: 630b0c367dcb
Revises:
Create Date: 2025-11-18 13:19:37.183397

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "630b0c367dcb"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Upgrade schema."""
    # Drop existing tables to start with a clean slate
    # Drop conversion_rooms first due to foreign key dependency
    op.execute("DROP TABLE IF EXISTS conversion_rooms CASCADE")
    op.execute("DROP TABLE IF EXISTS conversions CASCADE")

    print("dropped existing conversion tables")

    # ### commands auto generated by Alembic - please adjust! ###
    # Create conversions table
    op.create_table(
        "conversions",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("reservation_id", sa.Integer(), nullable=True),
        sa.Column("customer_id", sa.Integer(), nullable=True),
        sa.Column("hashed_customer_id", sa.Integer(), nullable=True),
        sa.Column("hotel_id", sa.String(), nullable=True),
        sa.Column("pms_reservation_id", sa.String(), nullable=True),
        sa.Column("reservation_number", sa.String(), nullable=True),
        sa.Column("reservation_date", sa.Date(), nullable=True),
        sa.Column("creation_time", sa.DateTime(timezone=True), nullable=True),
        sa.Column("reservation_type", sa.String(), nullable=True),
        sa.Column("booking_channel", sa.String(), nullable=True),
        sa.Column("guest_first_name", sa.String(), nullable=True),
        sa.Column("guest_last_name", sa.String(), nullable=True),
        sa.Column("guest_email", sa.String(), nullable=True),
        sa.Column("guest_country_code", sa.String(), nullable=True),
        sa.Column("advertising_medium", sa.String(), nullable=True),
        sa.Column("advertising_partner", sa.String(), nullable=True),
        sa.Column("advertising_campagne", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
        ),
        sa.ForeignKeyConstraint(
            ["hashed_customer_id"],
            ["hashed_customers.id"],
        ),
        sa.ForeignKeyConstraint(
            ["reservation_id"],
            ["reservations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_conversions_advertising_campagne"),
        "conversions",
        ["advertising_campagne"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_advertising_medium"),
        "conversions",
        ["advertising_medium"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_advertising_partner"),
        "conversions",
        ["advertising_partner"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_customer_id"), "conversions", ["customer_id"], unique=False
    )
    op.create_index(
        op.f("ix_conversions_guest_email"), "conversions", ["guest_email"], unique=False
    )
    op.create_index(
        op.f("ix_conversions_guest_first_name"),
        "conversions",
        ["guest_first_name"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_guest_last_name"),
        "conversions",
        ["guest_last_name"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_hashed_customer_id"),
        "conversions",
        ["hashed_customer_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_hotel_id"), "conversions", ["hotel_id"], unique=False
    )
    op.create_index(
        op.f("ix_conversions_pms_reservation_id"),
        "conversions",
        ["pms_reservation_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversions_reservation_id"),
        "conversions",
        ["reservation_id"],
        unique=False,
    )

    # Create conversion_rooms table
    op.create_table(
        "conversion_rooms",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("conversion_id", sa.Integer(), nullable=False),
        sa.Column("pms_hotel_reservation_id", sa.String(), nullable=True),
        sa.Column("arrival_date", sa.Date(), nullable=True),
        sa.Column("departure_date", sa.Date(), nullable=True),
        sa.Column("room_status", sa.String(), nullable=True),
        sa.Column("room_type", sa.String(), nullable=True),
        sa.Column("room_number", sa.String(), nullable=True),
        sa.Column("num_adults", sa.Integer(), nullable=True),
        sa.Column("rate_plan_code", sa.String(), nullable=True),
        sa.Column("connected_room_type", sa.String(), nullable=True),
        sa.Column("daily_sales", sa.JSON(), nullable=True),
        sa.Column("total_revenue", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["conversion_id"],
            ["alpinebits.conversions.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_conversion_rooms_arrival_date"),
        "conversion_rooms",
        ["arrival_date"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversion_rooms_conversion_id"),
        "conversion_rooms",
        ["conversion_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversion_rooms_departure_date"),
        "conversion_rooms",
        ["departure_date"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversion_rooms_pms_hotel_reservation_id"),
        "conversion_rooms",
        ["pms_hotel_reservation_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_conversion_rooms_room_number"),
        "conversion_rooms",
        ["room_number"],
        unique=False,
    )
    op.create_index(
        op.f("ix_acked_requests_username"), "acked_requests", ["username"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "conversions",
        sa.Column("revenue_fb", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("arrival_date", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("room_number", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("revenue_logis", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("room_type", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("num_adults", sa.INTEGER(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("revenue_spa", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("departure_date", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("revenue_board", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("room_status", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("sale_date", sa.DATE(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("revenue_other", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("revenue_total", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "conversions",
        sa.Column("rate_plan_code", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.drop_index(op.f("ix_conversions_guest_last_name"), table_name="conversions")
    op.drop_index(op.f("ix_conversions_guest_first_name"), table_name="conversions")
    op.drop_index(op.f("ix_conversions_guest_email"), table_name="conversions")
    op.create_index(
        op.f("ix_conversions_sale_date"), "conversions", ["sale_date"], unique=False
    )
    op.drop_column("conversions", "updated_at")
    op.drop_column("conversions", "guest_country_code")
    op.drop_column("conversions", "guest_email")
    op.drop_column("conversions", "guest_last_name")
    op.drop_column("conversions", "guest_first_name")
    op.drop_index(op.f("ix_acked_requests_username"), table_name="acked_requests")
    op.drop_index(
        op.f("ix_conversion_rooms_room_number"), table_name="conversion_rooms"
    )
    op.drop_index(
        op.f("ix_conversion_rooms_pms_hotel_reservation_id"),
        table_name="conversion_rooms",
    )
    op.drop_index(
        op.f("ix_conversion_rooms_departure_date"), table_name="conversion_rooms"
    )
    op.drop_index(
        op.f("ix_conversion_rooms_conversion_id"), table_name="conversion_rooms"
    )
    op.drop_index(
        op.f("ix_conversion_rooms_arrival_date"), table_name="conversion_rooms"
    )
    op.drop_table("conversion_rooms")
    # ### end Alembic commands ###
