"""add part_materials json column to cad_files

Revision ID: ce21c8a67543
Revises: 054
Create Date: 2026-03-09 20:11:52.201187

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = 'ce21c8a67543'
down_revision: Union[str, None] = '054'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('app_config')
    op.drop_index(op.f('ix_audit_log_channel'), table_name='audit_log')
    op.drop_index(op.f('ix_audit_log_notification_ts'), table_name='audit_log', postgresql_where='(notification = true)')
    op.drop_index(op.f('ix_audit_log_target_notification'), table_name='audit_log')
    op.add_column('cad_files', sa.Column('part_materials', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.alter_column('cad_files', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('cad_files_file_hash_key'), 'cad_files', type_='unique')
    op.drop_index(op.f('ix_cad_files_file_hash'), table_name='cad_files')
    op.create_index(op.f('ix_cad_files_file_hash'), 'cad_files', ['file_hash'], unique=True)
    op.drop_index(op.f('uq_dashboard_config_tenant_default'), table_name='dashboard_configs', postgresql_where='(is_tenant_default = true)')
    op.drop_index(op.f('uq_dashboard_config_user'), table_name='dashboard_configs', postgresql_where='(user_id IS NOT NULL)')
    op.create_index(op.f('ix_dashboard_configs_tenant_id'), 'dashboard_configs', ['tenant_id'], unique=False)
    op.create_index(op.f('ix_dashboard_configs_user_id'), 'dashboard_configs', ['user_id'], unique=False)
    op.drop_index(op.f('ix_import_validations_status'), table_name='import_validations')
    op.drop_index(op.f('ix_import_validations_tenant'), table_name='import_validations')
    op.create_index(op.f('ix_import_validations_tenant_id'), 'import_validations', ['tenant_id'], unique=False)
    op.drop_index(op.f('ix_invoice_lines_invoice'), table_name='invoice_lines')
    op.drop_index(op.f('ix_invoices_status'), table_name='invoices')
    op.drop_index(op.f('ix_invoices_tenant'), table_name='invoices')
    op.create_index(op.f('ix_invoices_tenant_id'), 'invoices', ['tenant_id'], unique=False)
    op.drop_constraint(op.f('invoices_tenant_id_fkey'), 'invoices', type_='foreignkey')
    op.create_foreign_key(None, 'invoices', 'tenants', ['tenant_id'], ['id'])
    op.drop_index(op.f('ix_material_aliases_material_id'), table_name='material_aliases')
    op.drop_index(op.f('uq_material_aliases_alias_lower'), table_name='material_aliases')
    op.drop_index(op.f('ix_media_assets_asset_type'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_asset_type_created'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_order_line'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_product'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_tenant'), table_name='media_assets')
    op.create_index(op.f('ix_media_assets_order_line_id'), 'media_assets', ['order_line_id'], unique=False)
    op.create_index(op.f('ix_media_assets_product_id'), 'media_assets', ['product_id'], unique=False)
    op.create_index(op.f('ix_media_assets_tenant_id'), 'media_assets', ['tenant_id'], unique=False)
    op.drop_index(op.f('ix_notification_configs_user'), table_name='notification_configs')
    op.drop_constraint(op.f('uq_notification_config_user_event_channel'), 'notification_configs', type_='unique')
    op.create_index(op.f('ix_notification_configs_user_id'), 'notification_configs', ['user_id'], unique=False)
    op.alter_column('order_items', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.alter_column('order_lines', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_index(op.f('uq_order_lines_render'), table_name='order_lines', postgresql_where='(output_type_id IS NOT NULL)')
    op.drop_index(op.f('uq_order_lines_tracking'), table_name='order_lines', postgresql_where='(output_type_id IS NULL)')
    op.alter_column('orders', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('orders_order_number_key'), 'orders', type_='unique')
    op.drop_index(op.f('ix_orders_order_number'), table_name='orders')
    op.create_index(op.f('ix_orders_order_number'), 'orders', ['order_number'], unique=True)
    op.drop_index(op.f('ix_render_positions_product_id'), table_name='product_render_positions')
    op.drop_index(op.f('uq_render_positions_product_name'), table_name='product_render_positions')
    op.create_index(op.f('ix_product_render_positions_product_id'), 'product_render_positions', ['product_id'], unique=False)
    op.alter_column('products', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_index(op.f('ix_products_category_lagertyp'), table_name='products')
    op.drop_index(op.f('uq_products_produkt_baureihe'), table_name='products', postgresql_where='((produkt_baureihe IS NOT NULL) AND (is_active = true))')
    op.drop_index(op.f('ix_render_templates_active_unique'), table_name='render_templates', postgresql_where='(is_active = true)')
    op.drop_constraint(op.f('templates_category_key_key'), 'templates', type_='unique')
    op.drop_index(op.f('ix_templates_category_key'), table_name='templates')
    op.create_index(op.f('ix_templates_category_key'), 'templates', ['category_key'], unique=True)
    op.alter_column('users', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(op.f('users_email_key'), 'users', type_='unique')
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.drop_index(op.f('ix_workflow_node_results_run'), table_name='workflow_node_results')
    op.create_index(op.f('ix_workflow_node_results_run_id'), 'workflow_node_results', ['run_id'], unique=False)
    op.drop_index(op.f('ix_workflow_runs_order_line'), table_name='workflow_runs')
    op.drop_index(op.f('ix_workflow_runs_status'), table_name='workflow_runs')
    op.create_index(op.f('ix_workflow_runs_order_line_id'), 'workflow_runs', ['order_line_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_workflow_runs_order_line_id'), table_name='workflow_runs')
    op.create_index(op.f('ix_workflow_runs_status'), 'workflow_runs', ['status'], unique=False)
    op.create_index(op.f('ix_workflow_runs_order_line'), 'workflow_runs', ['order_line_id'], unique=False)
    op.drop_index(op.f('ix_workflow_node_results_run_id'), table_name='workflow_node_results')
    op.create_index(op.f('ix_workflow_node_results_run'), 'workflow_node_results', ['run_id'], unique=False)
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=False)
    op.create_unique_constraint(op.f('users_email_key'), 'users', ['email'], postgresql_nulls_not_distinct=False)
    op.alter_column('users', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_index(op.f('ix_templates_category_key'), table_name='templates')
    op.create_index(op.f('ix_templates_category_key'), 'templates', ['category_key'], unique=False)
    op.create_unique_constraint(op.f('templates_category_key_key'), 'templates', ['category_key'], postgresql_nulls_not_distinct=False)
    op.create_index(op.f('ix_render_templates_active_unique'), 'render_templates', ['category_key', 'output_type_id'], unique=True, postgresql_where='(is_active = true)')
    op.create_index(op.f('uq_products_produkt_baureihe'), 'products', [sa.literal_column('lower(produkt_baureihe::text)')], unique=True, postgresql_where='((produkt_baureihe IS NOT NULL) AND (is_active = true))')
    op.create_index(op.f('ix_products_category_lagertyp'), 'products', ['category_key', 'lagertyp'], unique=False)
    op.alter_column('products', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_index(op.f('ix_product_render_positions_product_id'), table_name='product_render_positions')
    op.create_index(op.f('uq_render_positions_product_name'), 'product_render_positions', ['product_id', sa.literal_column('lower(name::text)')], unique=True)
    op.create_index(op.f('ix_render_positions_product_id'), 'product_render_positions', ['product_id'], unique=False)
    op.drop_index(op.f('ix_orders_order_number'), table_name='orders')
    op.create_index(op.f('ix_orders_order_number'), 'orders', ['order_number'], unique=False)
    op.create_unique_constraint(op.f('orders_order_number_key'), 'orders', ['order_number'], postgresql_nulls_not_distinct=False)
    op.alter_column('orders', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.create_index(op.f('uq_order_lines_tracking'), 'order_lines', ['order_id', 'product_id', sa.literal_column("COALESCE(render_position_id, '00000000-0000-0000-0000-000000000000'::uuid)")], unique=True, postgresql_where='(output_type_id IS NULL)')
    op.create_index(op.f('uq_order_lines_render'), 'order_lines', ['order_id', 'product_id', 'output_type_id', sa.literal_column("COALESCE(render_position_id, '00000000-0000-0000-0000-000000000000'::uuid)")], unique=True, postgresql_where='(output_type_id IS NOT NULL)')
    op.alter_column('order_lines', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.alter_column('order_items', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_index(op.f('ix_notification_configs_user_id'), table_name='notification_configs')
    op.create_unique_constraint(op.f('uq_notification_config_user_event_channel'), 'notification_configs', ['user_id', 'event_type', 'channel'], postgresql_nulls_not_distinct=False)
    op.create_index(op.f('ix_notification_configs_user'), 'notification_configs', ['user_id'], unique=False)
    op.drop_index(op.f('ix_media_assets_tenant_id'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_product_id'), table_name='media_assets')
    op.drop_index(op.f('ix_media_assets_order_line_id'), table_name='media_assets')
    op.create_index(op.f('ix_media_assets_tenant'), 'media_assets', ['tenant_id'], unique=False)
    op.create_index(op.f('ix_media_assets_product'), 'media_assets', ['product_id'], unique=False)
    op.create_index(op.f('ix_media_assets_order_line'), 'media_assets', ['order_line_id'], unique=False)
    op.create_index(op.f('ix_media_assets_asset_type_created'), 'media_assets', ['asset_type', 'created_at'], unique=False)
    op.create_index(op.f('ix_media_assets_asset_type'), 'media_assets', ['asset_type'], unique=False)
    op.create_index(op.f('uq_material_aliases_alias_lower'), 'material_aliases', [sa.literal_column('lower(alias::text)')], unique=True)
    op.create_index(op.f('ix_material_aliases_material_id'), 'material_aliases', ['material_id'], unique=False)
    op.drop_constraint(None, 'invoices', type_='foreignkey')
    op.create_foreign_key(op.f('invoices_tenant_id_fkey'), 'invoices', 'tenants', ['tenant_id'], ['id'], ondelete='CASCADE')
    op.drop_index(op.f('ix_invoices_tenant_id'), table_name='invoices')
    op.create_index(op.f('ix_invoices_tenant'), 'invoices', ['tenant_id'], unique=False)
    op.create_index(op.f('ix_invoices_status'), 'invoices', ['status'], unique=False)
    op.create_index(op.f('ix_invoice_lines_invoice'), 'invoice_lines', ['invoice_id'], unique=False)
    op.drop_index(op.f('ix_import_validations_tenant_id'), table_name='import_validations')
    op.create_index(op.f('ix_import_validations_tenant'), 'import_validations', ['tenant_id'], unique=False)
    op.create_index(op.f('ix_import_validations_status'), 'import_validations', ['status'], unique=False)
    op.drop_index(op.f('ix_dashboard_configs_user_id'), table_name='dashboard_configs')
    op.drop_index(op.f('ix_dashboard_configs_tenant_id'), table_name='dashboard_configs')
    op.create_index(op.f('uq_dashboard_config_user'), 'dashboard_configs', ['user_id'], unique=True, postgresql_where='(user_id IS NOT NULL)')
    op.create_index(op.f('uq_dashboard_config_tenant_default'), 'dashboard_configs', ['tenant_id'], unique=True, postgresql_where='(is_tenant_default = true)')
    op.drop_index(op.f('ix_cad_files_file_hash'), table_name='cad_files')
    op.create_index(op.f('ix_cad_files_file_hash'), 'cad_files', ['file_hash'], unique=False)
    op.create_unique_constraint(op.f('cad_files_file_hash_key'), 'cad_files', ['file_hash'], postgresql_nulls_not_distinct=False)
    op.alter_column('cad_files', 'tenant_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_column('cad_files', 'part_materials')
    op.create_index(op.f('ix_audit_log_target_notification'), 'audit_log', ['target_user_id', 'notification', 'read_at'], unique=False)
    op.create_index(op.f('ix_audit_log_notification_ts'), 'audit_log', ['notification', 'timestamp'], unique=False, postgresql_where='(notification = true)')
    op.create_index(op.f('ix_audit_log_channel'), 'audit_log', ['channel'], unique=False)
    op.create_table('app_config',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), autoincrement=False, nullable=False),
    sa.Column('version', sa.INTEGER(), server_default=sa.text('1'), autoincrement=False, nullable=False),
    sa.Column('render', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False),
    sa.Column('storage', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False),
    sa.Column('notifications', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False),
    sa.Column('worker', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False),
    sa.Column('billing', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False),
    sa.Column('updated_at', postgresql.TIMESTAMP(), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('id', name=op.f('app_config_pkey'))
    )
    # ### end Alembic commands ###
