"""orders 002 - customer order stats table

Revision ID: orders_002
Revises: orders_001
Create Date: 2026-03-07
"""
import sqlalchemy as sa

from alembic import op

revision = "orders_002"
down_revision = "orders_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "customer_order_stats",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("customer_id", sa.Integer(), sa.ForeignKey("customers.id"), nullable=False, index=True),
        sa.Column("total_orders", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("total_spent_cents", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_order_date", sa.DateTime(timezone=True), nullable=True),
        sa.Column("first_order_date", sa.DateTime(timezone=True), 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),
        sa.UniqueConstraint("store_id", "customer_id", name="uq_customer_order_stats_store_customer"),
    )
    op.create_index("idx_customer_order_stats_customer", "customer_order_stats", ["customer_id"])


def downgrade() -> None:
    op.drop_table("customer_order_stats")
