"""add_tax_rate_cost_and_letzshop_settings

Revision ID: c9e22eadf533
Revises: e1f2a3b4c5d6
Create Date: 2025-12-20 21:13:30.709696

Adds:
- tax_rate_percent to products and marketplace_products (NOT NULL, default 17)
- cost_cents to products (for profit calculation)
- Letzshop feed settings to vendors (tax_rate, boost_sort, delivery_method, preorder_days)
"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    # === MARKETPLACE PRODUCTS: Add tax_rate_percent ===
    with op.batch_alter_table("marketplace_products", schema=None) as batch_op:
        batch_op.add_column(sa.Column("tax_rate_percent", sa.Integer(), nullable=False, server_default="17"))

    # === PRODUCTS: Add tax_rate_percent and cost_cents, rename supplier_cost_cents ===
    with op.batch_alter_table("products", schema=None) as batch_op:
        batch_op.add_column(sa.Column("tax_rate_percent", sa.Integer(), nullable=False, server_default="17"))
        batch_op.add_column(sa.Column("cost_cents", sa.Integer(), nullable=True))
        # Drop old supplier_cost_cents column (data migrated to cost_cents if needed)
        try:
            batch_op.drop_column("supplier_cost_cents")
        except Exception:
            pass  # Column may not exist

    # === VENDORS: Add Letzshop feed settings ===
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        batch_op.add_column(sa.Column("letzshop_default_tax_rate", sa.Integer(), nullable=False, server_default="17"))
        batch_op.add_column(sa.Column("letzshop_boost_sort", sa.String(length=10), nullable=True, server_default="5.0"))
        batch_op.add_column(sa.Column("letzshop_delivery_method", sa.String(length=100), nullable=True, server_default="package_delivery"))
        batch_op.add_column(sa.Column("letzshop_preorder_days", sa.Integer(), nullable=True, server_default="1"))


def downgrade() -> None:
    # === VENDORS: Remove Letzshop feed settings ===
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        batch_op.drop_column("letzshop_preorder_days")
        batch_op.drop_column("letzshop_delivery_method")
        batch_op.drop_column("letzshop_boost_sort")
        batch_op.drop_column("letzshop_default_tax_rate")

    # === PRODUCTS: Remove tax_rate_percent and cost_cents ===
    with op.batch_alter_table("products", schema=None) as batch_op:
        batch_op.drop_column("cost_cents")
        batch_op.drop_column("tax_rate_percent")
        batch_op.add_column(sa.Column("supplier_cost_cents", sa.Integer(), nullable=True))

    # === MARKETPLACE PRODUCTS: Remove tax_rate_percent ===
    with op.batch_alter_table("marketplace_products", schema=None) as batch_op:
        batch_op.drop_column("tax_rate_percent")
