"""Phase B3 generate draft invoices from fee structures

Revision ID: 20260523_phase_b3_billing_generation
Revises: 20260522_phase_b1_b2_billing
Create Date: 2026-05-23
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260523_phase_b3_billing_generation"
down_revision = "20260522_phase_b1_b2_billing"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "billing_invoice_generation_batches",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("tenant_id", sa.Integer(), nullable=False),
        sa.Column("branch_id", sa.Integer(), nullable=True),
        sa.Column("billing_period_from", sa.Date(), nullable=False),
        sa.Column("billing_period_to", sa.Date(), nullable=False),
        sa.Column("frequency", sa.String(length=20), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="DRAFT_CREATED"),
        sa.Column("selected_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("created_invoice_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("skipped_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("error_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("remarks", sa.Text(), nullable=True),
        sa.Column("generated_by_user_id", sa.Integer(), nullable=True),
        sa.Column("generated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["generated_by_user_id"], ["users.id"]),
    )
    op.create_index("ix_billing_invoice_generation_batches_tenant_id", "billing_invoice_generation_batches", ["tenant_id"])
    op.create_index("ix_billing_invoice_generation_batches_branch_id", "billing_invoice_generation_batches", ["branch_id"])
    op.create_index("ix_billing_invoice_generation_batches_billing_period_from", "billing_invoice_generation_batches", ["billing_period_from"])
    op.create_index("ix_billing_invoice_generation_batches_billing_period_to", "billing_invoice_generation_batches", ["billing_period_to"])
    op.create_index("ix_billing_invoice_generation_batches_frequency", "billing_invoice_generation_batches", ["frequency"])
    op.create_index("ix_billing_invoice_generation_batches_status", "billing_invoice_generation_batches", ["status"])

    with op.batch_alter_table("billing_invoices") as batch_op:
        batch_op.add_column(sa.Column("generation_batch_id", sa.Integer(), nullable=True))
        batch_op.create_foreign_key(
            "fk_billing_invoices_generation_batch_id",
            "billing_invoice_generation_batches",
            ["generation_batch_id"],
            ["id"],
            ondelete="SET NULL",
        )
        batch_op.create_index("ix_billing_invoices_generation_batch_id", ["generation_batch_id"])


def downgrade() -> None:
    with op.batch_alter_table("billing_invoices") as batch_op:
        batch_op.drop_index("ix_billing_invoices_generation_batch_id")
        batch_op.drop_constraint("fk_billing_invoices_generation_batch_id", type_="foreignkey")
        batch_op.drop_column("generation_batch_id")

    op.drop_index("ix_billing_invoice_generation_batches_status", table_name="billing_invoice_generation_batches")
    op.drop_index("ix_billing_invoice_generation_batches_frequency", table_name="billing_invoice_generation_batches")
    op.drop_index("ix_billing_invoice_generation_batches_billing_period_to", table_name="billing_invoice_generation_batches")
    op.drop_index("ix_billing_invoice_generation_batches_billing_period_from", table_name="billing_invoice_generation_batches")
    op.drop_index("ix_billing_invoice_generation_batches_branch_id", table_name="billing_invoice_generation_batches")
    op.drop_index("ix_billing_invoice_generation_batches_tenant_id", table_name="billing_invoice_generation_batches")
    op.drop_table("billing_invoice_generation_batches")
