"""Match Outlook child foreign keys to the parent mail identifier.

Revision ID: d9e3f4a5b6c7
Revises: c8d2e3f4a5b6
Create Date: 2026-08-14
"""

from alembic import op
import sqlalchemy as sa


revision = "d9e3f4a5b6c7"
down_revision = "c8d2e3f4a5b6"
branch_labels = None
depends_on = None


def upgrade():
    op.drop_constraint(
        "outlook_attachments_ibfk_1", "outlook_attachments", type_="foreignkey"
    )
    op.drop_constraint(
        "outlook_mail_interpretations_ibfk_1",
        "outlook_mail_interpretations",
        type_="foreignkey",
    )
    op.alter_column(
        "outlook_mails",
        "id",
        existing_type=sa.String(length=100),
        type_=sa.String(length=500),
        existing_nullable=False,
    )
    op.alter_column(
        "outlook_attachments",
        "mail_id",
        existing_type=sa.String(length=100),
        type_=sa.String(length=500),
        existing_nullable=False,
    )
    op.alter_column(
        "outlook_mail_interpretations",
        "mail_id",
        existing_type=sa.String(length=100),
        type_=sa.String(length=500),
        existing_nullable=False,
    )
    op.create_foreign_key(
        "outlook_attachments_ibfk_1",
        "outlook_attachments",
        "outlook_mails",
        ["mail_id"],
        ["id"],
    )
    op.create_foreign_key(
        "outlook_mail_interpretations_ibfk_1",
        "outlook_mail_interpretations",
        "outlook_mails",
        ["mail_id"],
        ["id"],
    )


def downgrade():
    op.drop_constraint(
        "outlook_mail_interpretations_ibfk_1",
        "outlook_mail_interpretations",
        type_="foreignkey",
    )
    op.drop_constraint(
        "outlook_attachments_ibfk_1", "outlook_attachments", type_="foreignkey"
    )
    op.alter_column(
        "outlook_mail_interpretations",
        "mail_id",
        existing_type=sa.String(length=500),
        type_=sa.String(length=100),
        existing_nullable=False,
    )
    op.alter_column(
        "outlook_attachments",
        "mail_id",
        existing_type=sa.String(length=500),
        type_=sa.String(length=100),
        existing_nullable=False,
    )
    op.alter_column(
        "outlook_mails",
        "id",
        existing_type=sa.String(length=500),
        type_=sa.String(length=100),
        existing_nullable=False,
    )
    op.create_foreign_key(
        "outlook_mail_interpretations_ibfk_1",
        "outlook_mail_interpretations",
        "outlook_mails",
        ["mail_id"],
        ["id"],
    )
    op.create_foreign_key(
        "outlook_attachments_ibfk_1",
        "outlook_attachments",
        "outlook_mails",
        ["mail_id"],
        ["id"],
    )
