package com.doctate.watch.domain

import com.google.common.truth.Truth.assertThat
import org.junit.Test

class OnelinersJsonTest {
    @Test fun parses_full_response_with_all_oneliner_kinds() {
        val json = """
            {
              "as_of": "2026-04-26T12:00:00Z",
              "window_hours": 72,
              "oneliners": [
                {
                  "case_id": "c-ready",
                  "oneliner": {"kind":"ready","text":"Hello","generated_at":"2026-04-26T11:00:00Z"},
                  "created_at": "2026-04-26T10:00:00Z",
                  "last_recording_at": "2026-04-26T10:30:00Z",
                  "updated_at": "2026-04-26T11:00:00Z"
                },
                {
                  "case_id": "c-empty",
                  "oneliner": {"kind":"empty","generated_at":"2026-04-26T11:01:00Z"},
                  "created_at": "2026-04-26T10:01:00Z",
                  "last_recording_at": null,
                  "updated_at": null
                },
                {
                  "case_id": "c-error",
                  "oneliner": {"kind":"error","generated_at":"2026-04-26T11:02:00Z"},
                  "created_at": "2026-04-26T10:02:00Z",
                  "last_recording_at": "2026-04-26T10:30:00Z",
                  "updated_at": "2026-04-26T11:02:00Z"
                },
                {
                  "case_id": "c-manual",
                  "oneliner": {"kind":"manual","text":"Doctor","set_at":"2026-04-26T11:03:00Z"},
                  "created_at": "2026-04-26T10:03:00Z",
                  "last_recording_at": "2026-04-26T10:30:00Z",
                  "updated_at": "2026-04-26T11:03:00Z"
                },
                {
                  "case_id": "c-no-oneliner",
                  "oneliner": null,
                  "created_at": "2026-04-26T10:04:00Z",
                  "last_recording_at": null,
                  "updated_at": null
                }
              ]
            }
        """.trimIndent()
        val parsed = parseOnelinersResponse(json)
        assertThat(parsed.asOf).isEqualTo("2026-04-26T12:00:00Z")
        assertThat(parsed.windowHours).isEqualTo(72)
        assertThat(parsed.oneliners.map { it.caseId })
            .containsExactly("c-ready", "c-empty", "c-error", "c-manual", "c-no-oneliner").inOrder()

        val ready = parsed.oneliners[0].oneliner as OnelinerState.Ready
        assertThat(ready.text).isEqualTo("Hello")
        assertThat(parsed.oneliners[1].oneliner).isInstanceOf(OnelinerState.Empty::class.java)
        assertThat(parsed.oneliners[2].oneliner).isInstanceOf(OnelinerState.Error::class.java)
        val manual = parsed.oneliners[3].oneliner as OnelinerState.Manual
        assertThat(manual.text).isEqualTo("Doctor")
        assertThat(parsed.oneliners[4].oneliner).isNull()
    }

    @Test fun roundtrip_preserves_all_fields() {
        val original = OnelinersResponse(
            asOf = "2026-04-26T12:00:00Z",
            windowHours = 24,
            oneliners = listOf(
                OnelinerEntry(
                    caseId = "c-1",
                    oneliner = OnelinerState.Ready("hi", "2026-04-26T11:00:00Z"),
                    createdAt = "2026-04-26T10:00:00Z",
                    lastRecordingAt = "2026-04-26T10:30:00Z",
                    updatedAt = "2026-04-26T11:00:00Z",
                ),
            ),
        )
        val parsed = parseOnelinersResponse(onelinersResponseToJson(original))
        assertThat(parsed).isEqualTo(original)
    }

    @Test fun null_optional_fields_round_trip_as_null() {
        val original = OnelinersResponse(
            asOf = "2026-04-26T12:00:00Z",
            windowHours = 24,
            oneliners = listOf(
                OnelinerEntry(
                    caseId = "c-no-data",
                    oneliner = null,
                    createdAt = "2026-04-26T10:00:00Z",
                    lastRecordingAt = null,
                    updatedAt = null,
                ),
            ),
        )
        val parsed = parseOnelinersResponse(onelinersResponseToJson(original))
        assertThat(parsed.oneliners.single().oneliner).isNull()
        assertThat(parsed.oneliners.single().lastRecordingAt).isNull()
        assertThat(parsed.oneliners.single().updatedAt).isNull()
    }
}
