"""customers initial - customers, addresses, password reset tokens

Revision ID: customers_001
Revises: catalog_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "customers_001"
down_revision = "catalog_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- customers ---
    op.create_table(
        "customers",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False),
        sa.Column("email", sa.String(255), nullable=False, index=True),
        sa.Column("hashed_password", sa.String(255), nullable=False),
        sa.Column("first_name", sa.String(100), nullable=True),
        sa.Column("last_name", sa.String(100), nullable=True),
        sa.Column("phone", sa.String(50), nullable=True),
        sa.Column("customer_number", sa.String(100), nullable=False, index=True),
        sa.Column("preferences", sa.JSON(), nullable=True),
        sa.Column("marketing_consent", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("last_order_date", sa.DateTime(), nullable=True),
        sa.Column("total_orders", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("total_spent", sa.Numeric(10, 2), nullable=True, server_default="0"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("preferred_language", sa.String(5), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )

    # --- customer_addresses ---
    op.create_table(
        "customer_addresses",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False),
        sa.Column("customer_id", sa.Integer(), sa.ForeignKey("customers.id"), nullable=False),
        sa.Column("address_type", sa.String(50), nullable=False),
        sa.Column("first_name", sa.String(100), nullable=False),
        sa.Column("last_name", sa.String(100), nullable=False),
        sa.Column("company", sa.String(200), nullable=True),
        sa.Column("address_line_1", sa.String(255), nullable=False),
        sa.Column("address_line_2", sa.String(255), nullable=True),
        sa.Column("city", sa.String(100), nullable=False),
        sa.Column("postal_code", sa.String(20), nullable=False),
        sa.Column("country_name", sa.String(100), nullable=False),
        sa.Column("country_iso", sa.String(5), nullable=False),
        sa.Column("is_default", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )

    # --- password_reset_tokens ---
    op.create_table(
        "password_reset_tokens",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("customer_id", sa.Integer(), sa.ForeignKey("customers.id", ondelete="CASCADE"), nullable=False),
        sa.Column("token_hash", sa.String(64), nullable=False, index=True),
        sa.Column("expires_at", sa.DateTime(), nullable=False),
        sa.Column("used_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )


def downgrade() -> None:
    op.drop_table("password_reset_tokens")
    op.drop_table("customer_addresses")
    op.drop_table("customers")
