"""fix_org_lifecycle_and_expertise_id
Revision ID: 0473a3146058
Revises: 1d75b3806b43
Create Date: 2026-02-28 00:15:15.823744
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '0473a3146058'
down_revision: Union[str, Sequence[str], None] = '1d75b3806b43'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

def upgrade() -> None:
    # --- 1. Organizations tábla bővítése (Életciklus adatok) ---
    # Megjegyzés: A schema='data' kritikus, mert ott lakik a tábla!
    op.add_column('organizations', sa.Column('legal_owner_id', sa.Integer(), nullable=True), schema='data')
    op.add_column('organizations', sa.Column('first_registered_at', sa.DateTime(timezone=True), nullable=True), schema='data')
    op.add_column('organizations', sa.Column('current_lifecycle_started_at', sa.DateTime(timezone=True), nullable=True), schema='data')
    op.add_column('organizations', sa.Column('last_deactivated_at', sa.DateTime(timezone=True), nullable=True), schema='data')
    op.add_column('organizations', sa.Column('lifecycle_index', sa.Integer(), server_default='0', nullable=False), schema='data')

    # --- 2. Service Expertises tábla bővítése (Hiányzó ID) ---
    # Identity-t használunk, hogy automatikusan generáljon számokat a meglévő sorokhoz is
    op.add_column('service_expertises', sa.Column('id', sa.Integer(), sa.Identity(always=False), nullable=False), schema='data')
    
    # Opcionális: Ha az ID-t elsődleges kulccsá akarod tenni
    # op.create_primary_key('pk_service_expertises', 'service_expertises', ['id'], schema='data')

def downgrade() -> None:
    # Visszaállítási logika (fordított sorrendben)
    op.drop_column('service_expertises', 'id', schema='data')
    op.drop_column('organizations', 'lifecycle_index', schema='data')
    op.drop_column('organizations', 'last_deactivated_at', schema='data')
    op.drop_column('organizations', 'current_lifecycle_started_at', schema='data')
    op.drop_column('organizations', 'first_registered_at', schema='data')
    op.drop_column('organizations', 'legal_owner_id', schema='data')