"""remove packages, add father_name to applications

Revision ID: 1a2b3c4d5e6f
Revises: 44665fb8ec71
Create Date: 2026-08-09

The internet Package/plan concept is removed from the project: the `packages`
table and the `package_id` columns on connection_applications and connections
are dropped, and the application form gains an optional father's name field.
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op


revision: str = '1a2b3c4d5e6f'
down_revision: Union[str, None] = '44665fb8ec71'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        'connection_applications',
        sa.Column('father_name', sa.String(length=150), nullable=True),
    )

    op.drop_index('ix_connection_applications_package_id', table_name='connection_applications')
    op.drop_constraint(
        'fk_connection_applications_package_id_packages',
        'connection_applications',
        type_='foreignkey',
    )
    op.drop_column('connection_applications', 'package_id')

    op.drop_index('ix_connections_package_id', table_name='connections')
    op.drop_constraint('fk_connections_package_id_packages', 'connections', type_='foreignkey')
    op.drop_column('connections', 'package_id')

    op.drop_table('packages')


def downgrade() -> None:
    op.create_table(
        'packages',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('code', sa.String(length=20), nullable=True),
        sa.Column('speed_mbps', sa.Integer(), nullable=False),
        sa.Column('monthly_price', sa.Numeric(precision=10, scale=2), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('is_active', sa.Boolean(), server_default='true', 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.Column('is_deleted', sa.Boolean(), server_default='false', nullable=False),
        sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id', name=op.f('pk_packages')),
        sa.UniqueConstraint('code', name=op.f('uq_packages_code')),
        sa.UniqueConstraint('name', name=op.f('uq_packages_name')),
    )
    op.create_index(op.f('ix_packages_created_at'), 'packages', ['created_at'], unique=False)
    op.create_index(op.f('ix_packages_is_deleted'), 'packages', ['is_deleted'], unique=False)

    op.add_column('connection_applications', sa.Column('package_id', sa.Integer(), nullable=False))
    op.create_foreign_key(
        'fk_connection_applications_package_id_packages',
        'connection_applications',
        'packages',
        ['package_id'],
        ['id'],
        ondelete='RESTRICT',
    )
    op.create_index(
        op.f('ix_connection_applications_package_id'),
        'connection_applications',
        ['package_id'],
        unique=False,
    )

    op.add_column('connections', sa.Column('package_id', sa.Integer(), nullable=False))
    op.create_foreign_key(
        'fk_connections_package_id_packages',
        'connections',
        'packages',
        ['package_id'],
        ['id'],
        ondelete='RESTRICT',
    )
    op.create_index(op.f('ix_connections_package_id'), 'connections', ['package_id'], unique=False)

    op.drop_column('connection_applications', 'father_name')
