"""Loans

Revision ID: 02ed6e775351
Revises: b838606ad8d1
Create Date: 2025-12-20 10:36:30.853896

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    book_status_enum = sa.Enum(
        "active",
        "borrowed",
        "reserved",
        "restoration",
        "written_off",
        name="bookstatus",
    )
    book_status_enum.create(op.get_bind())
    op.create_table(
        "loans",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("book_id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("borrowed_at", sa.DateTime(), nullable=False),
        sa.Column("due_date", sa.DateTime(), nullable=False),
        sa.Column("returned_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["book_id"],
            ["book.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_loans_id"), "loans", ["id"], unique=False)
    op.add_column(
        "book",
        sa.Column("status", book_status_enum, nullable=False, server_default="active"),
    )
    op.drop_index(op.f("ix_roles_name"), table_name="roles")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(op.f("ix_roles_name"), "roles", ["name"], unique=True)
    op.drop_column("book", "status")
    op.drop_index(op.f("ix_loans_id"), table_name="loans")
    op.drop_table("loans")
    book_status_enum = sa.Enum(
        "active",
        "borrowed",
        "reserved",
        "restoration",
        "written_off",
        name="bookstatus",
    )
    book_status_enum.drop(op.get_bind())
    # ### end Alembic commands ###
