import pytest


def test_create_and_get_video_project(tmp_db):
    from app.db import init_db, create_video_project, get_video_project
    init_db()
    proj = create_video_project({"track_id": 1, "format": "visualizer", "target_countries": ["BR", "ID"]})
    assert proj["id"] == 1
    assert proj["format"] == "visualizer"
    assert proj["status"] == "pending"
    assert "BR" in proj["target_countries"]

    fetched = get_video_project(1)
    assert fetched["id"] == 1
    assert fetched["track_id"] == 1


def test_update_video_project_status(tmp_db):
    from app.db import init_db, create_video_project, update_video_project_status, get_video_project
    init_db()
    create_video_project({"track_id": 2, "format": "slideshow"})
    update_video_project_status(
        1, "done",
        output_path="/data/videos/1/output.mp4",
        output_url="/media/videos/1/output.mp4",
        thumbnail_path="/data/videos/1/thumbnail.jpg",
        yt_title="Chill Beats Brazil",
        yt_description="relaxing lofi",
        yt_tags=["lofi", "chill"],
    )
    proj = get_video_project(1)
    assert proj["status"] == "done"
    assert proj["yt_title"] == "Chill Beats Brazil"
    assert "lofi" in proj["yt_tags"]
    assert proj["completed_at"] is not None


def test_delete_video_project(tmp_db):
    from app.db import init_db, create_video_project, delete_video_project, get_video_project
    init_db()
    create_video_project({"track_id": 1, "format": "visualizer"})
    assert delete_video_project(1) is True
    assert get_video_project(1) is None
    assert delete_video_project(99) is False


def test_create_revenue_record(tmp_db):
    from app.db import init_db, create_revenue_record, get_all_revenue_records
    import pytest
    init_db()
    rec = create_revenue_record({
        "yt_video_id": "abc123",
        "record_month": "2026-04",
        "views": 10000,
        "watch_hours": 500.0,
        "revenue_usd": 25.0,
        "country": "BR",
    })
    assert rec["id"] == 1
    assert rec["rpm_usd"] == pytest.approx(2.5)

    records = get_all_revenue_records(yt_video_id="abc123")
    assert len(records) == 1


def test_revenue_dashboard(tmp_db):
    from app.db import init_db, create_revenue_record, get_revenue_dashboard
    init_db()
    create_revenue_record({"yt_video_id": "v1", "record_month": "2026-04", "views": 5000, "revenue_usd": 10.0})
    create_revenue_record({"yt_video_id": "v2", "record_month": "2026-04", "views": 5000, "revenue_usd": 15.0})
    dash = get_revenue_dashboard()
    assert dash["total_revenue_usd"] == pytest.approx(25.0)
    assert dash["total_views"] == 10000
    assert len(dash["by_month"]) == 1


def test_create_revenue_record_zero_views(tmp_db):
    from app.db import init_db, create_revenue_record
    init_db()
    rec = create_revenue_record({
        "yt_video_id": "zero",
        "record_month": "2026-04",
        "views": 0,
        "revenue_usd": 0.0,
    })
    assert rec["rpm_usd"] == 0.0


def test_update_revenue_record(tmp_db):
    from app.db import init_db, create_revenue_record, update_revenue_record
    init_db()
    create_revenue_record({"yt_video_id": "x", "record_month": "2026-04", "views": 1000, "revenue_usd": 5.0})
    updated = update_revenue_record(1, {"views": 2000, "revenue_usd": 8.0})
    assert updated["rpm_usd"] == pytest.approx(4.0)
    assert update_revenue_record(999, {}) is None


def test_delete_revenue_record(tmp_db):
    from app.db import init_db, create_revenue_record, delete_revenue_record, get_all_revenue_records
    init_db()
    create_revenue_record({"yt_video_id": "del", "record_month": "2026-04", "views": 100, "revenue_usd": 1.0})
    assert delete_revenue_record(1) is True
    assert delete_revenue_record(99) is False
    assert get_all_revenue_records(yt_video_id="del") == []


def test_video_project_status_failed(tmp_db):
    from app.db import init_db, create_video_project, update_video_project_status, get_video_project
    init_db()
    create_video_project({"track_id": 1, "format": "visualizer"})
    update_video_project_status(1, "failed", error="FFmpeg error")
    proj = get_video_project(1)
    assert proj["status"] == "failed"
    assert proj["completed_at"] is not None
    assert proj["error"] == "FFmpeg error"


def test_video_project_status_rendering_no_completed_at(tmp_db):
    from app.db import init_db, create_video_project, update_video_project_status, get_video_project
    init_db()
    create_video_project({"track_id": 1, "format": "visualizer"})
    update_video_project_status(1, "rendering")
    proj = get_video_project(1)
    assert proj["status"] == "rendering"
    assert proj["completed_at"] is None
