from pathlib import Path

from fastapi.testclient import TestClient

from app.main import create_app


def make_client(tmp_path: Path) -> TestClient:
    app = create_app(state_dir=tmp_path)
    return TestClient(app)


def test_health_endpoint_reports_ok(tmp_path: Path) -> None:
    client = make_client(tmp_path)

    response = client.get('/api/health')

    assert response.status_code == 200
    assert response.json() == {'status': 'ok'}


def test_dashboard_summary_seeds_manndra_first(tmp_path: Path) -> None:
    client = make_client(tmp_path)

    response = client.get('/api/dashboard/summary')

    assert response.status_code == 200
    payload = response.json()
    assert payload['profile']['id'] == 'manndra'
    assert payload['profile']['display_name'] == 'Manndra'
    assert payload['profile']['persona'] == 'Minerva'
    assert payload['counts'] == {
        'agenda': 3,
        'reminders': 3,
        'lists': 2,
        'notes': 2,
        'links': 3,
        'meal_ideas': 3,
        'shortcuts': 3,
        'quick_actions': 3,
    }
    assert payload['agenda'][0]['title'] == 'Morning check-in'
    assert payload['lists'][0]['title'] == 'Errands'
    assert payload['meal_ideas'][0]['title'] == 'Easy comfort dinner'


def test_quick_add_note_updates_summary_and_persists_newest_first(tmp_path: Path) -> None:
    client = make_client(tmp_path)

    response = client.post(
        '/api/notes/quick-add',
        json={
            'title': 'Field trip forms',
            'body': 'Check whether anything needs signing tonight.',
            'category': 'school',
            'source': 'api-test',
            'pinned': False,
        },
    )

    assert response.status_code == 201
    saved = response.json()
    assert saved['title'] == 'Field trip forms'
    assert saved['category'] == 'school'
    assert saved['source'] == 'api-test'

    summary = client.get('/api/dashboard/summary')
    assert summary.status_code == 200
    payload = summary.json()
    assert payload['counts']['notes'] == 3
    assert payload['notes'][0]['title'] == 'Field trip forms'
    assert payload['notes'][0]['body'] == 'Check whether anything needs signing tonight.'


def test_homepage_renders_ravenclaw_common_room_dashboard(tmp_path: Path) -> None:
    client = make_client(tmp_path)

    response = client.get('/')

    assert response.status_code == 200
    body = response.text
    assert 'Minerva Dashboard' in body
    assert 'Ravenclaw common room' in body
    assert 'Capture a note before it disappears' in body
    assert 'Dinner sparks' in body
    assert '/static/images/library-hero.jpg' in body
    assert '/static/images/owl-engraving.jpg' in body
    assert '/static/images/blue-interior.jpg' in body
    assert '/static/images/art-nouveau-frame.svg' in body


def test_form_post_redirects_after_note_capture(tmp_path: Path) -> None:
    client = make_client(tmp_path)

    response = client.post(
        '/quick-actions/notes',
        data={
            'title': 'Gift reminder',
            'body': 'Look for one elegant but practical option.',
            'category': 'household',
        },
        follow_redirects=False,
    )

    assert response.status_code == 303
    assert response.headers['location'] == '/'

    summary = client.get('/api/dashboard/summary').json()
    assert summary['counts']['notes'] == 3
    assert summary['notes'][0]['title'] == 'Gift reminder'
