import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[4] / 'base_platform'))
from app.db.session import SessionLocal
from .tax_type_tags import TAX_TYPE_TAGS
from .industry_tags import INDUSTRY_TAGS
from .knowledge_asset_tags import KNOWLEDGE_ASSET_TAGS
from .validity_tags import VALIDITY_TAGS
from .regional_tags import REGIONAL_TAGS
from .risk_model_tags import RISK_MODEL_TAGS
from .optimization_tags import OPTIMIZATION_TAGS
from .scenario_tags import SCENARIO_TAGS
from .tag_helpers import seed_tag_categories, create_tag_recursive

def seed_taxonomy(tenant_id: int):
    db = SessionLocal()
    try:
        print(f'Seeding tax taxonomy for tenant {tenant_id}...')
        categories = seed_tag_categories(db, tenant_id)
        mapping = {'tax_type': TAX_TYPE_TAGS, 'industry': INDUSTRY_TAGS, 'knowledge_asset': KNOWLEDGE_ASSET_TAGS, 'validity': VALIDITY_TAGS, 'regional': REGIONAL_TAGS, 'risk_model': RISK_MODEL_TAGS, 'optimization': OPTIMIZATION_TAGS, 'scenario': SCENARIO_TAGS}
        root_count = 0
        for code, tags in mapping.items():
            category = categories.get(code)
            if category:
                for tag_data in tags:
                    create_tag_recursive(db, tag_data, category.id, tenant_id)
                    root_count += 1
        db.commit()
        print(f'✓ Tax taxonomy seeded: {root_count} root tags')
    except Exception as e:
        db.rollback()
        print(f'✗ Failed: {e}')
        raise
    finally:
        db.close()