package wang.yaojia.webterm.api.models

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable

/**
 * One commit from `git log` (`src/types.ts` `CommitLogEntry`). [hash] and [at] are REQUIRED — a
 * commit missing either is dropped by the list-lossy [CommitLogEntryListSerializer] (its siblings
 * survive). [subject] defaults to empty so a subject-less commit still decodes. `at` = `%ct * 1000`
 * (epoch millis). All fields are rendered INERT (plain text; no autolink) at the screen (plan §8).
 */
@Serializable
public data class CommitLogEntry(
    val hash: String,
    val at: Long,
    val subject: String = "",
)

/**
 * `GET /projects/log` result (`src/types.ts` `GitLogResult`). [truncated] = more commits exist
 * beyond the server cap. The commit list decodes lossily (drop-one-keep-rest).
 */
@Serializable
public data class GitLogResult(
    @Serializable(with = CommitLogEntryListSerializer::class)
    val commits: List<CommitLogEntry> = emptyList(),
    val truncated: Boolean = false,
)

/** Drops a commit missing `hash`/`at`, keeps the rest (nested list-lossy, like worktrees). */
internal object CommitLogEntryListSerializer :
    KSerializer<List<CommitLogEntry>> by LossyListSerializer(CommitLogEntry.serializer())
