import asyncio
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from sqlalchemy import select
from infrastructure.database.session import get_db_session
from modules.emojis.models import CustomEmoji


async def seed_emojis():
    print("🎨 Seeding Custom Emojis...")
    json_path = Path(__file__).resolve().parent.parent / "modules" / "emojis" / "seed_data" / "curated_emojis.json"
    with open(json_path, "r", encoding="utf-8") as f:
        data = json.load(f)

    async with get_db_session() as session:
        count = 0
        for item in data["emojis"]:
            stmt = select(CustomEmoji).where(CustomEmoji.custom_emoji_id == item["custom_emoji_id"])
            res = await session.execute(stmt)
            existing = res.scalar_one_or_none()
            if not existing:
                emoji = CustomEmoji(
                    custom_emoji_id=item["custom_emoji_id"],
                    name=item["name"],
                    fallback_emoji=item["emoji"],
                    category=item["category"],
                    is_animated=item.get("is_animated", True),
                )
                session.add(emoji)
                count += 1
        await session.commit()
    print(f"✅ Successfully seeded {count} custom emojis into database.")


if __name__ == "__main__":
    asyncio.run(seed_emojis())
