"""gamification_schema_fix_v4

Revision ID: e5ad17906e7f
Revises: 429ffa7dd5e1
Create Date: 2026-02-27 06:17:18.106421

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'e5ad17906e7f'
down_revision: Union[str, Sequence[str], None] = '429ffa7dd5e1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """ExpertiseTag és ServiceExpertise bővítése - Kézi kényszerítés."""
    
    # 1. ExpertiseTag bővítése (data séma)
    op.add_column('expertise_tags', sa.Column('name_en', sa.String(length=100), nullable=True), schema='data')
    op.add_column('expertise_tags', sa.Column('is_official', sa.Boolean(), server_default=sa.text('true'), nullable=False), schema='data')
    op.add_column('expertise_tags', sa.Column('suggested_by_id', sa.BigInteger(), nullable=True), schema='data')
    op.add_column('expertise_tags', sa.Column('discovery_points', sa.Integer(), server_default=sa.text('10'), nullable=False), schema='data')
    op.add_column('expertise_tags', sa.Column('search_keywords', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'[]'::jsonb"), nullable=False), schema='data')
    op.add_column('expertise_tags', sa.Column('usage_count', sa.Integer(), server_default=sa.text('0'), nullable=False), schema='data')
    op.add_column('expertise_tags', sa.Column('icon', sa.String(length=50), nullable=True), schema='data')
    op.add_column('expertise_tags', sa.Column('description', sa.Text(), nullable=True), schema='data')
    op.add_column('expertise_tags', sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), schema='data')
    op.add_column('expertise_tags', sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), schema='data')

    # Foreign Key az identity sémába
    op.create_foreign_key('fk_expertise_tags_suggested_by', 'expertise_tags', 'persons', ['suggested_by_id'], ['id'], source_schema='data', referent_schema='identity')

    # 2. ServiceExpertise bővítése (ha a tábla már létezik, csak az új mezők kellenek)
    # Ha az 'id' mező hiányzik, ezt is hozzáadjuk (sorrend miatt fontos lehet)
    try:
        op.add_column('service_expertises', sa.Column('confidence_level', sa.Integer(), server_default=sa.text('0'), nullable=False), schema='data')
        op.add_column('service_expertises', sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), schema='data')
    except Exception:
        pass # Ha már ott lennének, ne álljon meg a folyamat

def downgrade() -> None:
    """Visszaállítás."""
    op.drop_constraint('fk_expertise_tags_suggested_by', 'expertise_tags', schema='data', type_='foreignkey')
    columns_to_drop = [
        'name_en', 'is_official', 'suggested_by_id', 'discovery_points', 
        'search_keywords', 'usage_count', 'icon', 'description', 
        'created_at', 'updated_at'
    ]
    for col in columns_to_drop:
        op.drop_column('expertise_tags', col, schema='data')
    pass
