package com.correx.core.tasks

import com.correx.core.events.events.EventMetadata
import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.StoredEvent
import com.correx.core.events.events.TaskCancelledEvent
import com.correx.core.events.events.TaskClaimedEvent
import com.correx.core.events.events.TaskCompletedEvent
import com.correx.core.events.events.TaskCreatedEvent
import com.correx.core.events.events.TaskNoteAddedEvent
import com.correx.core.events.events.TaskSubmittedForReviewEvent
import com.correx.core.events.types.EventId
import com.correx.core.events.types.ProjectId
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.TaskId
import com.correx.core.events.types.TaskNoteAuthor
import kotlinx.datetime.Instant
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class TaskHistoryTest {

    private val taskId = TaskId("auth-1")

    private fun stored(payload: EventPayload, seq: Long) = StoredEvent(
        metadata = EventMetadata(
            eventId = EventId("e-$seq"),
            sessionId = SessionId("tasks:auth"),
            timestamp = Instant.parse("2026-01-0${seq}T00:00:00Z"),
            schemaVersion = 1,
            causationId = null,
            correlationId = null,
        ),
        sequence = seq,
        sessionSequence = seq,
        payload = payload,
    )

    private fun created() = TaskCreatedEvent(
        taskId = taskId,
        projectId = ProjectId("auth"),
        key = "auth-1",
        title = "Add JWT refresh",
        goal = "users stay authenticated",
    )

    @Test
    fun `renders the lifecycle as a timeline in order`() {
        val events = listOf(
            stored(created(), 1),
            stored(TaskClaimedEvent(taskId, "claude-opus"), 2),
            stored(TaskNoteAddedEvent(taskId, TaskNoteAuthor.AGENT, "migration retried"), 3),
            stored(TaskSubmittedForReviewEvent(taskId), 4),
            stored(TaskCompletedEvent(taskId), 5),
        )

        val lines = TaskHistory.render(events).lines()

        assertEquals(5, lines.size)
        assertTrue(lines[0].contains("2026-01-01T00:00:00Z"), lines[0])
        assertTrue(lines[0].contains("created auth-1: Add JWT refresh"), lines[0])
        assertTrue(lines[1].contains("claimed by claude-opus"), lines[1])
        assertTrue(lines[2].contains("note [AGENT] migration retried"), lines[2])
        assertTrue(lines[3].contains("submitted for review"), lines[3])
        assertTrue(lines[4].contains("completed"), lines[4])
    }

    @Test
    fun `cancellation reason is included`() {
        val lines = TaskHistory.render(
            listOf(stored(created(), 1), stored(TaskCancelledEvent(taskId, "superseded"), 2)),
        ).lines()
        assertTrue(lines[1].contains("cancelled: superseded"), lines[1])
    }

    @Test
    fun `empty event list renders as no history`() {
        assertEquals("no history", TaskHistory.render(emptyList()))
    }
}
