"""Create currencies table.

Revision ID: 0007_currencies
Revises: 0006_add_addresses
Create Date: 2026-07-04
"""

from __future__ import annotations

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = "0007_currencies"
down_revision: Union[str, None] = "0006_add_addresses"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "currencies",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("code", sa.String(3), nullable=False, unique=True),
        sa.Column("name", sa.String(50), nullable=False),
        sa.Column("symbol", sa.String(5), nullable=False),
        sa.Column("is_default", sa.Boolean, nullable=False, server_default="false"),
        sa.Column("tenant_id", postgresql.UUID(as_uuid=True), nullable=False, index=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_currencies_tenant_code", "currencies", ["tenant_id", "code"])
    op.create_index("ix_currencies_tenant_default", "currencies", ["tenant_id", "is_default"])


def downgrade() -> None:
    op.drop_index("ix_currencies_tenant_default", table_name="currencies")
    op.drop_index("ix_currencies_tenant_code", table_name="currencies")
    op.drop_table("currencies")
