import asyncio
from decimal import Decimal
from datetime import datetime, timedelta
from app.database import async_session, init_db
from app.models import User, Wallet, Bet, BetCategory, BetStatus, BetVisibility
from app.utils.security import get_password_hash


async def seed():
    print("Initializing database...")
    await init_db()

    async with async_session() as db:
        print("Creating test users...")
        users = [
            User(
                email="alice@example.com",
                username="alice",
                password_hash=get_password_hash("password123"),
                display_name="Alice Smith"
            ),
            User(
                email="bob@example.com",
                username="bob",
                password_hash=get_password_hash("password123"),
                display_name="Bob Jones"
            ),
            User(
                email="charlie@example.com",
                username="charlie",
                password_hash=get_password_hash("password123"),
                display_name="Charlie Brown"
            ),
        ]

        for user in users:
            db.add(user)
        await db.flush()

        print("Creating wallets...")
        for user in users:
            wallet = Wallet(
                user_id=user.id,
                balance=Decimal("1000.00"),
                escrow=Decimal("0.00")
            )
            db.add(wallet)

        print("Creating sample bets...")
        bets = [
            Bet(
                creator_id=users[0].id,
                title="Super Bowl LVIII Winner",
                description="Bet on who will win Super Bowl LVIII",
                category=BetCategory.SPORTS,
                event_name="Super Bowl LVIII",
                event_date=datetime.utcnow() + timedelta(days=30),
                creator_position="Kansas City Chiefs win",
                opponent_position="San Francisco 49ers win",
                stake_amount=Decimal("100.00"),
                creator_odds=1.5,
                opponent_odds=2.0,
                visibility=BetVisibility.PUBLIC,
                expires_at=datetime.utcnow() + timedelta(days=7)
            ),
            Bet(
                creator_id=users[1].id,
                title="League of Legends Worlds 2024",
                description="Who will win the LoL World Championship?",
                category=BetCategory.ESPORTS,
                event_name="LoL Worlds 2024",
                creator_position="T1 wins",
                opponent_position="Any other team wins",
                stake_amount=Decimal("50.00"),
                creator_odds=1.8,
                opponent_odds=1.8,
                visibility=BetVisibility.PUBLIC,
                expires_at=datetime.utcnow() + timedelta(days=14)
            ),
            Bet(
                creator_id=users[2].id,
                title="Oscar Best Picture 2024",
                description="Which film will win Best Picture at the 2024 Oscars?",
                category=BetCategory.ENTERTAINMENT,
                event_name="96th Academy Awards",
                event_date=datetime.utcnow() + timedelta(days=60),
                creator_position="Oppenheimer wins",
                opponent_position="Any other film wins",
                stake_amount=Decimal("25.00"),
                creator_odds=1.3,
                opponent_odds=3.0,
                visibility=BetVisibility.PUBLIC,
                expires_at=datetime.utcnow() + timedelta(days=30)
            ),
            Bet(
                creator_id=users[0].id,
                title="Bitcoin Price Prediction",
                description="Will Bitcoin reach $100,000 by end of Q1 2024?",
                category=BetCategory.CUSTOM,
                event_name="Bitcoin Price Target",
                event_date=datetime.utcnow() + timedelta(days=90),
                creator_position="Bitcoin will reach $100k",
                opponent_position="Bitcoin won't reach $100k",
                stake_amount=Decimal("75.00"),
                creator_odds=2.5,
                opponent_odds=1.5,
                visibility=BetVisibility.PUBLIC,
                expires_at=datetime.utcnow() + timedelta(days=20)
            ),
        ]

        for bet in bets:
            db.add(bet)

        await db.commit()
        print("✅ Seed data created successfully!")
        print("\nTest users:")
        print("  - alice@example.com / password123")
        print("  - bob@example.com / password123")
        print("  - charlie@example.com / password123")
        print("\nEach user has $1000 starting balance")


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