package com.applemand.zmanim.core.calsync

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import java.io.StringReader
import java.io.StringWriter
import java.time.Instant

class IcsRoundTripTest {

    @Test
    fun `single event survives write then read`() {
        val event = IcsEvent(
            uid = "luach-1-test@local",
            summary = "Candle Lighting",
            start = Instant.parse("2026-04-17T22:33:00Z"),
            end = Instant.parse("2026-04-17T22:34:00Z"),
            allDay = false,
            description = "Test",
            category = "ZMANIM",
        )
        val sw = StringWriter()
        IcsWriter(sw).writeCalendar(tzid = "UTC", events = listOf(event))

        val parsed = IcsParser().parse(StringReader(sw.toString()))
        assertThat(parsed).hasSize(1)
        val out = parsed.single()
        assertThat(out.uid).isEqualTo(event.uid)
        assertThat(out.summary).isEqualTo(event.summary)
        assertThat(out.start).isEqualTo(event.start)
        assertThat(out.allDay).isFalse()
    }

    @Test
    fun `all-day event flagged on read`() {
        val event = IcsEvent(
            uid = "luach-2-holiday@local",
            summary = "Pesach",
            start = Instant.parse("2026-04-02T00:00:00Z"),
            allDay = true,
            category = "HOLIDAY",
        )
        val sw = StringWriter()
        IcsWriter(sw).writeCalendar(tzid = "UTC", events = listOf(event))
        val parsed = IcsParser().parse(StringReader(sw.toString()))
        assertThat(parsed.single().allDay).isTrue()
    }
}
