mod common;

use axum::http::StatusCode;
use serde_json::json;
use sqlx::PgPool;
use tower::ServiceExt;
use uuid::Uuid;
#[allow(unused_imports)]
use serde_json as _;

use common::MultipartBuilder;

async fn seed_manga(h: &common::Harness, cookie: &str, title: &str) -> Uuid {
    common::seed_manga_via_api(&h.app, cookie, title).await
}

async fn seed_chapter(
    pool: &PgPool,
    manga_id: Uuid,
    number: i32,
    title: Option<&str>,
) -> Uuid {
    // Historical seed — uploaded_by remains NULL, mirroring the
    // pre-Phase-5 rows in the production DB.
    mangalord::repo::chapter::create(pool, manga_id, number, title, None)
        .await
        .unwrap()
        .id
}

#[sqlx::test(migrations = "./migrations")]
async fn list_chapters_is_empty_initially(pool: PgPool) {
    let h = common::harness(pool);
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;

    let resp = h
        .app
        .oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = common::body_json(resp).await;
    assert_eq!(body["items"], json!([]));
    assert_eq!(body["page"]["limit"], 50);
    assert_eq!(body["page"]["offset"], 0);
    assert!(body["page"]["total"].is_null());
}

#[sqlx::test(migrations = "./migrations")]
async fn uncrawled_chapter_reports_zero_storage(pool: PgPool) {
    // A page-less chapter (page_count = 0, no page rows) is "uncrawled"
    // — its size_bytes is 0, which the frontend renders as an em-dash.
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    seed_chapter(&pool, manga_id, 1, Some("Unfetched")).await;

    let resp = h
        .app
        .oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
        .await
        .unwrap();
    let body = common::body_json(resp).await;
    assert_eq!(body["items"][0]["page_count"].as_i64().unwrap(), 0);
    assert_eq!(body["items"][0]["size_bytes"].as_i64().unwrap(), 0);
}

#[sqlx::test(migrations = "./migrations")]
async fn chapter_with_unmeasured_page_reports_null_storage(pool: PgPool) {
    // A crawled chapter whose pages predate the size backfill (size_bytes
    // IS NULL) must report size_bytes = null — "unknown" — so the frontend
    // shows an em-dash, not a misleading "0 B".
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    let chapter_id = seed_chapter(&pool, manga_id, 1, Some("Crawled")).await;
    // One measured page, one unmeasured (NULL) → whole chapter is unknown.
    sqlx::query(
        "INSERT INTO pages (chapter_id, page_number, storage_key, content_type, size_bytes) \
         VALUES ($1, 1, 'k/1.jpg', 'image/jpeg', 100)",
    )
    .bind(chapter_id)
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query(
        "INSERT INTO pages (chapter_id, page_number, storage_key, content_type) \
         VALUES ($1, 2, 'k/2.jpg', 'image/jpeg')",
    )
    .bind(chapter_id)
    .execute(&pool)
    .await
    .unwrap();

    let resp = h
        .app
        .clone()
        .oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
        .await
        .unwrap();
    let body = common::body_json(resp).await;
    assert!(
        body["items"][0]["size_bytes"].is_null(),
        "unmeasured page should make the chapter size unknown (null), got {}",
        body["items"][0]["size_bytes"]
    );

    // The manga detail total is likewise unknown (null), not a silent
    // undercount of just the measured page.
    let detail = h
        .app
        .oneshot(common::get(&format!("/api/v1/mangas/{manga_id}")))
        .await
        .unwrap();
    let detail = common::body_json(detail).await;
    assert!(
        detail["chapter_storage_bytes"].is_null(),
        "manga total should be unknown when a page is unmeasured, got {}",
        detail["chapter_storage_bytes"]
    );
}

#[sqlx::test(migrations = "./migrations")]
async fn list_chapters_returned_in_number_order(pool: PgPool) {
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;

    seed_chapter(&pool, manga_id, 3, Some("The Black Swordsman")).await;
    seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
    seed_chapter(&pool, manga_id, 2, None).await;

    let resp = h
        .app
        .oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = common::body_json(resp).await;
    let numbers: Vec<i64> = body["items"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["number"].as_i64().unwrap())
        .collect();
    assert_eq!(numbers, vec![1, 2, 3]);
    assert_eq!(body["items"][1]["title"], serde_json::Value::Null);
}

#[sqlx::test(migrations = "./migrations")]
async fn list_chapters_returns_404_for_unknown_manga(pool: PgPool) {
    let h = common::harness(pool);
    let unknown = Uuid::nil();
    let resp = h
        .app
        .oneshot(common::get(&format!("/api/v1/mangas/{unknown}/chapters")))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let body = common::body_json(resp).await;
    assert_eq!(body["error"]["code"], "not_found");
}

#[sqlx::test(migrations = "./migrations")]
async fn get_chapter_by_id(pool: PgPool) {
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    let chapter_id = seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;

    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{manga_id}/chapters/{chapter_id}"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = common::body_json(resp).await;
    assert_eq!(body["number"], 1);
    assert_eq!(body["title"], "The Brand");
    assert_eq!(body["page_count"], 0);
    assert_eq!(body["id"], chapter_id.to_string());
}

#[sqlx::test(migrations = "./migrations")]
async fn get_chapter_unknown_id_is_404(pool: PgPool) {
    let h = common::harness(pool);
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    let unknown_chapter = Uuid::new_v4();

    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let body = common::body_json(resp).await;
    assert_eq!(body["error"]["code"], "not_found");
}

#[sqlx::test(migrations = "./migrations")]
async fn get_chapter_unknown_manga_is_404(pool: PgPool) {
    let h = common::harness(pool);
    let unknown_manga = Uuid::nil();
    let unknown_chapter = Uuid::new_v4();
    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{unknown_manga}/chapters/{unknown_chapter}"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

/// Cross-manga isolation: a chapter id belonging to manga A must not
/// resolve when accessed via manga B's URL. The (manga_id, id) scoping
/// in `find_by_id_in_manga` enforces this.
#[sqlx::test(migrations = "./migrations")]
async fn get_chapter_from_wrong_manga_is_404(pool: PgPool) {
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_a = seed_manga(&h, &cookie, "Berserk").await;
    let manga_b = seed_manga(&h, &cookie, "Vagabond").await;
    let chapter_id = seed_chapter(&pool, manga_a, 1, Some("Episode 1")).await;

    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{manga_b}/chapters/{chapter_id}"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[sqlx::test(migrations = "./migrations")]
async fn list_pages_empty_for_chapter_without_upload(pool: PgPool) {
    let h = common::harness(pool.clone());
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    let chapter_id = seed_chapter(&pool, manga_id, 1, None).await;

    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{manga_id}/chapters/{chapter_id}/pages"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = common::body_json(resp).await;
    assert_eq!(body["pages"], json!([]));
}

#[sqlx::test(migrations = "./migrations")]
async fn list_pages_returns_404_for_unknown_chapter(pool: PgPool) {
    let h = common::harness(pool);
    let (_, cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &cookie, "Berserk").await;
    let unknown_chapter = Uuid::new_v4();

    let resp = h
        .app
        .oneshot(common::get(&format!(
            "/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}/pages"
        )))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[sqlx::test(migrations = "./migrations")]
async fn non_owner_can_upload_chapter(pool: PgPool) {
    // Contract lock: chapter upload is INTENTIONALLY open to any
    // authenticated user, not just the manga's creator. A user who did not
    // create the manga can still contribute a chapter (community
    // contributions), unlike manga-record edits which gate on ownership.
    // If this ever needs to become owner-only, that's a deliberate change —
    // this test (and the doc comment on `api::chapters::create`) should be
    // updated together, not silently broken.
    let h = common::harness(pool);

    // Owner creates the manga.
    let (_owner, owner_cookie) = common::register_user(&h.app).await;
    let manga_id = seed_manga(&h, &owner_cookie, "Berserk").await;

    // A different, non-owner user uploads a chapter to it.
    let (_other, other_cookie) = common::register_user(&h.app).await;
    let resp = h
        .app
        .clone()
        .oneshot(common::post_multipart_with_cookie(
            &format!("/api/v1/mangas/{manga_id}/chapters"),
            MultipartBuilder::new()
                .add_json("metadata", json!({ "number": 1, "title": "Contributed" }))
                .add_file("page", "1.png", "image/png", &common::fake_png_bytes()),
            &other_cookie,
        ))
        .await
        .unwrap();

    assert_eq!(
        resp.status(),
        StatusCode::CREATED,
        "a non-owner authenticated user must be able to upload a chapter"
    );
    let body = common::body_json(resp).await;
    assert_eq!(body["number"], 1);
    assert_eq!(body["title"], "Contributed");
    assert_eq!(body["page_count"], 1);
}
