# python
# File: db/debug.py

import random
from models.child import Child
from models.task import Task
from models.reward import Reward
from models.image import Image
from db.db import child_db, task_db, reward_db, image_db

def populate_debug_data(clear_existing=True, seed=42):
    """
    Populate DBs with dummy data:
    - 2 children
    - 8 tasks
    - 4 rewards
    Each child gets 3 unique tasks and 2 unique rewards (unique within that child).
    Returns a dict with inserted records.
    """
    random.seed(seed)

    # Optionally clear existing data
    if clear_existing:
        try:
            child_db.truncate()
            task_db.truncate()
            reward_db.truncate()
            image_db.truncate()
        except Exception:
            # fallback: remove all docs by id if truncate isn't available
            from tinydb import Query
            for doc in child_db.all():
                child_db.remove(Query().id == doc.get('id'))
            for doc in task_db.all():
                task_db.remove(Query().id == doc.get('id'))
            for doc in reward_db.all():
                reward_db.remove(Query().id == doc.get('id'))
            for doc in image_db.all():
                image_db.remove(Query().id == doc.get('id'))

    # Create 8 tasks
    task_defs = [
        ("Make Bed", 2, True, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Brush Teeth", 1, True, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Do Homework", 5, True, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Clean Room", 3, True),
        ("Practice Piano", 4, True),
        ("Feed Pet", 1, True, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Take Out Trash", 2, True),
        ("Set Table", 1, True),
        ("Misc Task 1", 1, False),
        ("Misc Task 2", 2, False, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Misc Task 3", 3, False),
        ("Misc Task 4", 4, False, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Misc Task 5", 5, True),
    ]
    tasks = []
    for td in task_defs:
        if len(td) == 4:
            name, points, is_good, image = td
        else:
            name, points, is_good = td
            image = None
        t = Task(name=name, points=points, is_good=is_good, image_id=image)
        task_db.insert(t.to_dict())
        tasks.append(t.to_dict())

    # Create 4 rewards
    reward_defs = [
        ("Sticker Pack", "Fun stickers", 3,'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Extra Screen Time", "30 minutes", 8, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("New Toy", "Small toy", 12, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
        ("Ice Cream", "One scoop", 5, 'a86feb1f-be65-4ca2-bdb5-9223ffbd6779'),
    ]
    rewards = []
    for name, desc, cost, image in reward_defs:
        r = Reward(name=name, description=desc, cost=cost, image_id=image)
        reward_db.insert(r.to_dict())
        rewards.append(r.to_dict())

    image_db.insert(Image(1, '.png', True, id='a86feb1f-be65-4ca2-bdb5-9223ffbd6779').to_dict())

    # Create 2 children and assign unique tasks/rewards per child
    children = []
    task_ids = [t['id'] for t in tasks]
    reward_ids = [r['id'] for r in rewards]
    child_names = [("Child One", 8, "boy01"), ("Child Two", 10, "girl01")]

    for name, age, image in child_names:
        chosen_tasks = random.sample(task_ids, 11)
        chosen_rewards = random.sample(reward_ids, 2)
        points = random.randint(0, 15)
        c = Child(name=name, age=age, tasks=chosen_tasks, rewards=chosen_rewards, points=points, image_id=image)
        child_db.insert(c.to_dict())
        children.append(c.to_dict())

    return {
        "children": children,
        "tasks": tasks,
        "rewards": rewards
    }

if __name__ == "__main__":
    result = populate_debug_data(clear_existing=True)
    print("Inserted debug data:")
    print(f"Children: {[c['name'] for c in result['children']]}")
    print(f"Tasks: {[t['name'] for t in result['tasks']]}")
    print(f"Rewards: {[r['name'] for r in result['rewards']]}")