import pytest
import os
from flask import Flask
from api.child_api import child_api
from db.db import child_db, reward_db, task_db
from tinydb import Query
from models.child import Child

@pytest.fixture
def client():
    app = Flask(__name__)
    app.register_blueprint(child_api)
    app.config['TESTING'] = True
    with app.test_client() as client:
        yield client

@pytest.fixture(scope="session", autouse=True)
def cleanup_db():
    yield
    child_db.close()
    reward_db.close()
    task_db.close()
    if os.path.exists('children.json'):
        os.remove('children.json')
    if os.path.exists('rewards.json'):
        os.remove('rewards.json')
    if os.path.exists('tasks.json'):
        os.remove('tasks.json')

def test_add_child(client):
    response = client.put('/child/add', json={'name': 'Alice', 'age': 8})
    assert response.status_code == 201
    assert b'Child Alice added.' in response.data
    children = child_db.all()
    assert any(c.get('name') == 'Alice' and c.get('age') == 8 and c.get('image_id') == 'boy01' for c in children)

    response = client.put('/child/add', json={'name': 'Mike', 'age': 4, 'image_id': 'girl02'})
    assert response.status_code == 201
    assert b'Child Mike added.' in response.data
    children = child_db.all()
    assert any(c.get('name') == 'Mike' and c.get('age') == 4 and c.get('image_id') == 'girl02' for c in children)

def test_list_children(client):
    child_db.truncate()
    child_db.insert(Child(name='Alice', age=8, image_id="boy01").to_dict())
    child_db.insert(Child(name='Mike', age=4, image_id="boy01").to_dict())
    response = client.get('/child/list')
    assert response.status_code == 200
    data = response.json
    assert len(data['children']) == 2

def test_assign_and_remove_task(client):
    client.put('/child/add', json={'name': 'Bob', 'age': 10})
    children = client.get('/child/list').get_json()['children']
    child_id = children[0]['id']
    response = client.post(f'/child/{child_id}/assign-task', json={'task_id': 'task123'})
    assert response.status_code == 200
    ChildQuery = Query()
    child = child_db.search(ChildQuery.id == child_id)[0]
    assert 'task123' in child.get('tasks', [])
    response = client.post(f'/child/{child_id}/remove-task', json={'task_id': 'task123'})
    assert response.status_code == 200
    child = child_db.search(ChildQuery.id == child_id)[0]
    assert 'task123' not in child.get('tasks', [])

def test_get_child_not_found(client):
    response = client.get('/child/nonexistent-id')
    assert response.status_code == 404
    assert b'Child not found' in response.data

def test_remove_task_not_found(client):
    response = client.post('/child/nonexistent-id/remove-task', json={'task_id': 'task123'})
    assert response.status_code == 404
    assert b'Child not found' in response.data

def test_remove_reward_not_found(client):
    response = client.post('/child/nonexistent-id/remove-reward', json={'reward_id': 'reward123'})
    assert response.status_code == 404
    assert b'Child not found' in response.data

def test_affordable_rewards(client):
    reward_db.insert({'id': 'r_cheep', 'name': 'Sticker', 'cost': 5})
    reward_db.insert({'id': 'r_exp', 'name': 'Bike', 'cost': 20})
    client.put('/child/add', json={'name': 'Charlie', 'age': 9})
    children = client.get('/child/list').get_json()['children']
    child_id = next((c['id'] for c in children if c.get('name') == 'Charlie'), children[0]['id'])
    ChildQuery = Query()
    child_db.update({'points': 10}, ChildQuery.id == child_id)
    client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_cheep'})
    client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_exp'})
    resp = client.get(f'/child/{child_id}/affordable-rewards')
    assert resp.status_code == 200
    data = resp.get_json()
    affordable_ids = [r['id'] for r in data['affordable_rewards']]
    assert 'r_cheep' in affordable_ids and 'r_exp' not in affordable_ids

def test_reward_status(client):
    reward_db.insert({'id': 'r1', 'name': 'Candy', 'cost': 3, 'image': 'candy.png'})
    reward_db.insert({'id': 'r2', 'name': 'Game', 'cost': 8, 'image': 'game.png'})
    reward_db.insert({'id': 'r3', 'name': 'Trip', 'cost': 15, 'image': 'trip.png'})
    client.put('/child/add', json={'name': 'Dana', 'age': 11})
    children = client.get('/child/list').get_json()['children']
    child_id = next((c['id'] for c in children if c.get('name') == 'Dana'), children[0]['id'])
    ChildQuery = Query()
    child_db.update({'points': 7}, ChildQuery.id == child_id)
    for rid in ['r1', 'r2', 'r3']:
        client.post(f'/child/{child_id}/assign-reward', json={'reward_id': rid})
    resp = client.get(f'/child/{child_id}/reward-status')
    assert resp.status_code == 200
    data = resp.get_json()
    mapping = {s['id']: s['points_needed'] for s in data['reward_status']}
    assert mapping['r1'] == 0 and mapping['r2'] == 1 and mapping['r3'] == 8

def test_list_child_tasks_returns_tasks(client):
    task_db.insert({'id': 't_list_1', 'name': 'Task One', 'points': 2, 'is_good': True})
    task_db.insert({'id': 't_list_2', 'name': 'Task Two', 'points': 3, 'is_good': False})
    child_db.insert({
        'id': 'child_list_1',
        'name': 'Eve',
        'age': 8,
        'points': 0,
        'tasks': ['t_list_1', 't_list_2', 't_missing'],
        'rewards': []
    })
    resp = client.get('/child/child_list_1/list-tasks')
    assert resp.status_code == 200
    data = resp.get_json()
    returned_ids = {t['id'] for t in data['tasks']}
    assert returned_ids == {'t_list_1', 't_list_2'}
    for t in data['tasks']:
        assert 'name' in t and 'points' in t and 'is_good' in t

def test_list_assignable_tasks_returns_expected_ids(client):
    child_db.truncate()
    task_db.truncate()
    task_db.insert({'id': 'tA', 'name': 'Task A', 'points': 1, 'is_good': True})
    task_db.insert({'id': 'tB', 'name': 'Task B', 'points': 2, 'is_good': True})
    task_db.insert({'id': 'tC', 'name': 'Task C', 'points': 3, 'is_good': False})
    client.put('/child/add', json={'name': 'Zoe', 'age': 7})
    child_id = client.get('/child/list').get_json()['children'][0]['id']
    client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tA'})
    client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tC'})
    resp = client.get(f'/child/{child_id}/list-assignable-tasks')
    assert resp.status_code == 200
    data = resp.get_json()
    assert len(data['tasks']) == 1
    assert data['tasks'][0]['id'] == 'tB'
    assert data['count'] == 1

def test_list_assignable_tasks_when_none_assigned(client):
    child_db.truncate()
    task_db.truncate()
    ids = ['t1', 't2', 't3']
    for i, tid in enumerate(ids, 1):
        task_db.insert({'id': tid, 'name': f'Task {i}', 'points': i, 'is_good': True})
    client.put('/child/add', json={'name': 'Liam', 'age': 6})
    child_id = client.get('/child/list').get_json()['children'][0]['id']
    resp = client.get(f'/child/{child_id}/list-assignable-tasks')
    assert resp.status_code == 200
    data = resp.get_json()
    returned_ids = {t['id'] for t in data['tasks']}
    assert returned_ids == set(ids)
    assert data['count'] == len(ids)

def test_list_assignable_tasks_empty_task_db(client):
    child_db.truncate()
    task_db.truncate()
    client.put('/child/add', json={'name': 'Mia', 'age': 5})
    child_id = client.get('/child/list').get_json()['children'][0]['id']
    resp = client.get(f'/child/{child_id}/list-assignable-tasks')
    assert resp.status_code == 200
    data = resp.get_json()
    assert data['tasks'] == []
    assert data['count'] == 0

def test_list_assignable_tasks_child_not_found(client):
    resp = client.get('/child/does-not-exist/list-assignable-tasks')
    assert resp.status_code == 404
    assert b'Child not found' in resp.data

def setup_child_with_tasks(child_name='TestChild', age=8, assigned=None):
    child_db.truncate()
    task_db.truncate()
    assigned = assigned or []
    # Seed tasks
    task_db.insert({'id': 't1', 'name': 'Task 1', 'points': 1, 'is_good': True})
    task_db.insert({'id': 't2', 'name': 'Task 2', 'points': 2, 'is_good': False})
    task_db.insert({'id': 't3', 'name': 'Task 3', 'points': 3, 'is_good': True})
    # Seed child
    child = Child(name=child_name, age=age, image_id='boy01').to_dict()
    child['tasks'] = assigned[:]
    child_db.insert(child)
    return child['id']

def test_list_all_tasks_partitions_assigned_and_assignable(client):
    child_id = setup_child_with_tasks(assigned=['t1', 't3'])
    resp = client.get(f'/child/{child_id}/list-all-tasks')
    assert resp.status_code == 200
    data = resp.get_json()
    assigned_ids = {t['id'] for t in data['assigned_tasks']}
    assignable_ids = {t['id'] for t in data['assignable_tasks']}
    assert assigned_ids == {'t1', 't3'}
    assert assignable_ids == {'t2'}
    assert data['assigned_count'] == 2
    assert data['assignable_count'] == 1

def test_set_child_tasks_replaces_existing(client):
    child_id = setup_child_with_tasks(assigned=['t1', 't2'])
    # Provide new set including a valid and an invalid id (invalid should be filtered)
    payload = {'task_ids': ['t3', 'missing', 't3']}
    resp = client.put(f'/child/{child_id}/set-tasks', json=payload)
    assert resp.status_code == 200
    data = resp.get_json()
    assert data['task_ids'] == ['t3']
    assert data['count'] == 1
    ChildQuery = Query()
    child = child_db.get(ChildQuery.id == child_id)
    assert child['tasks'] == ['t3']

def test_set_child_tasks_requires_list(client):
    child_id = setup_child_with_tasks(assigned=['t2'])
    resp = client.put(f'/child/{child_id}/set-tasks', json={'task_ids': 'not-a-list'})
    assert resp.status_code == 400
    assert b'task_ids must be a list' in resp.data

def test_set_child_tasks_child_not_found(client):
    resp = client.put('/child/does-not-exist/set-tasks', json={'task_ids': ['t1', 't2']})
    assert resp.status_code == 404
    assert b'Child not found' in resp.data
