"""initial schema

Revision ID: 20260601_0001
Revises:
Create Date: 2026-06-01
"""

from alembic import op
import sqlalchemy as sa

revision = "20260601_0001"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "roles",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("code", sa.String(64), nullable=False),
        sa.Column("name", sa.String(64), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_roles_code", "roles", ["code"], unique=True)

    op.create_table(
        "admin_users",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("username", sa.String(64), nullable=False),
        sa.Column("password_hash", sa.String(255), nullable=False),
        sa.Column("role_code", sa.String(64), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_admin_users_username", "admin_users", ["username"], unique=True)
    op.create_index("ix_admin_users_role_code", "admin_users", ["role_code"])

    op.create_table(
        "learners",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("phone", sa.String(32), nullable=False),
        sa.Column("current_name", sa.String(64), nullable=False),
        sa.Column("id_type", sa.String(32), nullable=True),
        sa.Column("id_no_encrypted", sa.String(256), nullable=True),
        sa.Column("id_no_masked", sa.String(64), nullable=True),
        sa.Column("student_no", sa.String(64), nullable=True),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("remark", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_learners_phone", "learners", ["phone"], unique=True)
    op.create_index("ix_learners_current_name", "learners", ["current_name"])

    op.create_table(
        "learner_name_histories",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("learner_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(64), nullable=False),
        sa.Column("source", sa.String(64), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_learner_name_histories_learner_id", "learner_name_histories", ["learner_id"])

    op.create_table(
        "learner_phone_histories",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("learner_id", sa.Integer(), nullable=False),
        sa.Column("old_phone", sa.String(32), nullable=False),
        sa.Column("new_phone", sa.String(32), nullable=False),
        sa.Column("reason", sa.String(255), nullable=False),
        sa.Column("changed_by", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_learner_phone_histories_learner_id", "learner_phone_histories", ["learner_id"])

    op.create_table(
        "project_courses",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("code", sa.String(16), nullable=False),
        sa.Column("name", sa.String(128), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_project_courses_code", "project_courses", ["code"], unique=True)

    op.create_table(
        "certificates",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("learner_id", sa.Integer(), nullable=False),
        sa.Column("project_code", sa.String(16), nullable=False),
        sa.Column("certificate_no", sa.String(64), nullable=False),
        sa.Column("certificate_name", sa.String(128), nullable=False),
        sa.Column("class_name", sa.String(128), nullable=True),
        sa.Column("course_name", sa.String(128), nullable=True),
        sa.Column("stage_name", sa.String(128), nullable=True),
        sa.Column("issue_date", sa.Date(), nullable=False),
        sa.Column("issuer_name", sa.String(128), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("pdf_status", sa.String(32), nullable=False),
        sa.Column("pdf_file_path", sa.String(512), nullable=True),
        sa.Column("public_token_id", sa.Integer(), nullable=True),
        sa.Column("qr_token_id", sa.Integer(), nullable=True),
        sa.Column("remark", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_certificates_learner_id", "certificates", ["learner_id"])
    op.create_index("ix_certificates_project_code", "certificates", ["project_code"])
    op.create_index("ix_certificates_certificate_no", "certificates", ["certificate_no"], unique=True)

    op.create_table(
        "certificate_access_tokens",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("certificate_id", sa.Integer(), nullable=False),
        sa.Column("token_hash", sa.String(128), nullable=False),
        sa.Column("token_value", sa.String(128), nullable=False),
        sa.Column("token_type", sa.String(32), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("created_reason", sa.String(128), nullable=False),
        sa.Column("revoked_at", sa.DateTime(), nullable=True),
        sa.Column("last_access_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_certificate_access_tokens_certificate_id", "certificate_access_tokens", ["certificate_id"])
    op.create_index("ix_certificate_access_tokens_token_hash", "certificate_access_tokens", ["token_hash"], unique=True)
    op.create_index("ix_certificate_access_tokens_token_value", "certificate_access_tokens", ["token_value"], unique=True)

    op.create_table(
        "import_batches",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("filename", sa.String(255), nullable=False),
        sa.Column("file_path", sa.String(512), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("total_rows", sa.Integer(), nullable=False),
        sa.Column("valid_rows", sa.Integer(), nullable=False),
        sa.Column("failed_rows", sa.Integer(), nullable=False),
        sa.Column("conflict_rows", sa.Integer(), nullable=False),
        sa.Column("error_report_path", sa.String(512), nullable=True),
        sa.Column("created_by", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_import_batches_created_by", "import_batches", ["created_by"])

    op.create_table(
        "import_batch_rows",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("batch_id", sa.Integer(), nullable=False),
        sa.Column("row_no", sa.Integer(), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("raw_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_import_batch_rows_batch_id", "import_batch_rows", ["batch_id"])

    op.create_table(
        "operation_logs",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("admin_user_id", sa.Integer(), nullable=True),
        sa.Column("action", sa.String(64), nullable=False),
        sa.Column("object_type", sa.String(64), nullable=False),
        sa.Column("object_id", sa.String(64), nullable=True),
        sa.Column("ip_address", sa.String(64), nullable=True),
        sa.Column("detail_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_operation_logs_admin_user_id", "operation_logs", ["admin_user_id"])
    op.create_index("ix_operation_logs_action", "operation_logs", ["action"])
    op.create_index("ix_operation_logs_object_type", "operation_logs", ["object_type"])


def downgrade() -> None:
    op.drop_table("operation_logs")
    op.drop_table("import_batch_rows")
    op.drop_table("import_batches")
    op.drop_table("certificate_access_tokens")
    op.drop_table("certificates")
    op.drop_table("project_courses")
    op.drop_table("learner_phone_histories")
    op.drop_table("learner_name_histories")
    op.drop_table("learners")
    op.drop_table("admin_users")
    op.drop_table("roles")
