from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "202607110003"
down_revision = "202607110002"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "report_templates",
        sa.Column("template_key", sa.String(length=120), nullable=False),
        sa.Column("name", sa.String(length=180), nullable=False),
        sa.Column("version", sa.String(length=40), nullable=False),
        sa.Column("validation_type", sa.String(length=120), nullable=False),
        sa.Column("reference_path", sa.String(length=500), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_report_templates")),
        sa.UniqueConstraint("template_key", name=op.f("uq_report_templates_template_key")),
    )
    op.create_index(op.f("ix_report_templates_active"), "report_templates", ["active"], unique=False)
    op.create_index(op.f("ix_report_templates_template_key"), "report_templates", ["template_key"], unique=False)
    op.create_index(op.f("ix_report_templates_validation_type"), "report_templates", ["validation_type"], unique=False)

    op.create_table(
        "text_blocks",
        sa.Column("template_id", sa.String(), nullable=False),
        sa.Column("block_key", sa.String(length=160), nullable=False),
        sa.Column("title", sa.String(length=240), nullable=False),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("order_index", sa.Integer(), nullable=False),
        sa.Column("version", sa.String(length=40), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["template_id"], ["report_templates.id"], name=op.f("fk_text_blocks_template_id_report_templates")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_text_blocks")),
    )
    op.create_index(op.f("ix_text_blocks_active"), "text_blocks", ["active"], unique=False)
    op.create_index(op.f("ix_text_blocks_block_key"), "text_blocks", ["block_key"], unique=False)
    op.create_index(op.f("ix_text_blocks_template_id"), "text_blocks", ["template_id"], unique=False)

    op.create_table(
        "report_sections",
        sa.Column("template_id", sa.String(), nullable=False),
        sa.Column("section_key", sa.String(length=160), nullable=False),
        sa.Column("number", sa.String(length=40), nullable=True),
        sa.Column("title", sa.String(length=240), nullable=False),
        sa.Column("order_index", sa.Integer(), nullable=False),
        sa.Column("page_break_before", sa.Boolean(), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["template_id"], ["report_templates.id"], name=op.f("fk_report_sections_template_id_report_templates")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_report_sections")),
    )
    op.create_index(op.f("ix_report_sections_active"), "report_sections", ["active"], unique=False)
    op.create_index(op.f("ix_report_sections_section_key"), "report_sections", ["section_key"], unique=False)
    op.create_index(op.f("ix_report_sections_template_id"), "report_sections", ["template_id"], unique=False)

    op.create_table(
        "checklist_templates",
        sa.Column("template_id", sa.String(), nullable=False),
        sa.Column("checklist_key", sa.String(length=160), nullable=False),
        sa.Column("title", sa.String(length=240), nullable=False),
        sa.Column("columns", sa.JSON(), nullable=False),
        sa.Column("items", sa.JSON(), nullable=False),
        sa.Column("order_index", sa.Integer(), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["template_id"], ["report_templates.id"], name=op.f("fk_checklist_templates_template_id_report_templates")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_checklist_templates")),
    )
    op.create_index(op.f("ix_checklist_templates_active"), "checklist_templates", ["active"], unique=False)
    op.create_index(op.f("ix_checklist_templates_checklist_key"), "checklist_templates", ["checklist_key"], unique=False)
    op.create_index(op.f("ix_checklist_templates_template_id"), "checklist_templates", ["template_id"], unique=False)

    op.create_table(
        "measurement_imports",
        sa.Column("validation_id", sa.String(), nullable=False),
        sa.Column("import_type", sa.String(length=60), nullable=False),
        sa.Column("original_filename", sa.String(length=255), nullable=False),
        sa.Column("storage_path", sa.String(length=500), nullable=False),
        sa.Column("sha256", sa.String(length=64), nullable=False),
        sa.Column("parser_version", sa.String(length=40), nullable=False),
        sa.Column("status", sa.String(length=60), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["validation_id"], ["validations.id"], name=op.f("fk_measurement_imports_validation_id_validations")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_measurement_imports")),
    )
    op.create_index(op.f("ix_measurement_imports_import_type"), "measurement_imports", ["import_type"], unique=False)
    op.create_index(op.f("ix_measurement_imports_sha256"), "measurement_imports", ["sha256"], unique=False)
    op.create_index(op.f("ix_measurement_imports_status"), "measurement_imports", ["status"], unique=False)
    op.create_index(op.f("ix_measurement_imports_validation_id"), "measurement_imports", ["validation_id"], unique=False)

    op.create_table(
        "measurement_import_values",
        sa.Column("import_id", sa.String(), nullable=False),
        sa.Column("test_run", sa.String(length=120), nullable=False),
        sa.Column("field_name", sa.String(length=120), nullable=False),
        sa.Column("raw_value", sa.Text(), nullable=True),
        sa.Column("normalized_value", sa.String(length=180), nullable=True),
        sa.Column("unit", sa.String(length=40), nullable=True),
        sa.Column("source_page", sa.Integer(), nullable=True),
        sa.Column("source_text", sa.Text(), nullable=True),
        sa.Column("confidence", sa.Integer(), nullable=False),
        sa.Column("confirmed", sa.Boolean(), nullable=False),
        sa.Column("corrected_value", sa.String(length=180), nullable=True),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["import_id"], ["measurement_imports.id"], name=op.f("fk_measurement_import_values_import_id_measurement_imports")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_measurement_import_values")),
    )
    op.create_index(op.f("ix_measurement_import_values_confirmed"), "measurement_import_values", ["confirmed"], unique=False)
    op.create_index(op.f("ix_measurement_import_values_field_name"), "measurement_import_values", ["field_name"], unique=False)
    op.create_index(op.f("ix_measurement_import_values_import_id"), "measurement_import_values", ["import_id"], unique=False)
    op.create_index(op.f("ix_measurement_import_values_test_run"), "measurement_import_values", ["test_run"], unique=False)


def downgrade() -> None:
    op.drop_table("measurement_import_values")
    op.drop_table("measurement_imports")
    op.drop_table("checklist_templates")
    op.drop_table("report_sections")
    op.drop_table("text_blocks")
    op.drop_table("report_templates")
