"""Add structured standard metadata table.

Revision ID: 0014_add_article_standard_meta
Revises: 0013_add_crawl_job_current_page
Create Date: 2026-05-09
"""
from alembic import op
import sqlalchemy as sa


revision = "0014_add_article_standard_meta"
down_revision = "0013_add_crawl_job_current_page"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "article_standard_meta",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("article_id", sa.BigInteger(), nullable=False),
        sa.Column("standard_no", sa.String(length=100), nullable=True),
        sa.Column("chinese_title", sa.Text(), nullable=True),
        sa.Column("english_title", sa.Text(), nullable=True),
        sa.Column("standard_status", sa.String(length=100), nullable=True),
        sa.Column("ccs_codes", sa.JSON(), nullable=True),
        sa.Column("ics_codes", sa.JSON(), nullable=True),
        sa.Column("publish_date", sa.Date(), nullable=True),
        sa.Column("implementation_date", sa.Date(), nullable=True),
        sa.Column("competent_department", sa.String(length=500), nullable=True),
        sa.Column("technical_committee", sa.String(length=500), nullable=True),
        sa.Column("issuing_body", sa.String(length=500), nullable=True),
        sa.Column("standard_type", sa.String(length=100), nullable=True),
        sa.Column("raw_meta_json", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=True),
        sa.ForeignKeyConstraint(["article_id"], ["article.id"], ondelete="CASCADE"),
        sa.UniqueConstraint("article_id", name="uq_article_standard_meta_article"),
    )
    op.create_index("ix_standard_meta_standard_no", "article_standard_meta", ["standard_no"])
    op.create_index("ix_standard_meta_publish_date", "article_standard_meta", ["publish_date"])
    op.create_index(
        "ix_standard_meta_implementation_date",
        "article_standard_meta",
        ["implementation_date"],
    )


def downgrade() -> None:
    op.drop_index("ix_standard_meta_implementation_date", table_name="article_standard_meta")
    op.drop_index("ix_standard_meta_publish_date", table_name="article_standard_meta")
    op.drop_index("ix_standard_meta_standard_no", table_name="article_standard_meta")
    op.drop_table("article_standard_meta")
