import Foundation
import Testing
@testable import IReminderSync

@Suite("IReminderSync.Reader")
struct ReaderTests {
    @Test("ReminderEntry round-trips its fields")
    func reminderEntryFields() {
        let due = Date(timeIntervalSince1970: 1_750_000_000)
        let mod = Date(timeIntervalSince1970: 1_751_000_000)
        let entry = ReminderEntry(
            reminderIdentifier: "rem-1",
            calendarIdentifier: "list-1",
            title: "Buy milk",
            notes: "2%",
            dueDate: due,
            priority: 5,
            isCompleted: false,
            creationDate: nil,
            lastModifiedDate: mod
        )
        #expect(entry.reminderIdentifier == "rem-1")
        #expect(entry.calendarIdentifier == "list-1")
        #expect(entry.title == "Buy milk")
        #expect(entry.notes == "2%")
        #expect(entry.dueDate == due)
        #expect(entry.priority == 5)
        #expect(entry.isCompleted == false)
        #expect(entry.lastModifiedDate == mod)
    }

    @Test("ReminderCalendarEntry round-trips its fields")
    func calendarEntryFields() {
        let cal = ReminderCalendarEntry(
            calendarIdentifier: "list-1",
            title: "Groceries",
            source: "iCloud",
            color: "#FF8800"
        )
        #expect(cal.calendarIdentifier == "list-1")
        #expect(cal.title == "Groceries")
        #expect(cal.source == "iCloud")
        #expect(cal.color == "#FF8800")
    }

    @Test("IReminderSyncStats defaults are zero")
    func statsDefaults() {
        let s = IReminderSyncStats()
        #expect(s.calendarCount == 0)
        #expect(s.reminderCount == 0)
    }

    @Test("IReminderSyncError surface")
    func errorMessages() {
        #expect(IReminderSyncError.none.message == "")
        #expect(IReminderSyncError.reminderAccessRequired.message == "Reminders access required")
        #expect(IReminderSyncError.backendUnreachable.isConnectionError)
        #expect(IReminderSyncError.connectionFailed("x").isConnectionError)
        #expect(!IReminderSyncError.reminderAccessRequired.isConnectionError)
    }

    @Test("SyncReminderPayload dictionary maps every field")
    func payloadDictionary() {
        let payload = SyncReminderPayload(
            reminderIdentifier: "rem-1",
            calendarIdentifier: "list-1",
            title: "Buy milk",
            notes: nil,
            dueDate: "2026-05-13T10:00:00Z",
            priority: 5,
            isCompleted: false,
            modifiedAt: "2026-05-12T09:00:00Z"
        )
        let dict = payload.dictionary
        #expect(dict["reminderIdentifier"] as? String == "rem-1")
        #expect(dict["calendarIdentifier"] as? String == "list-1")
        #expect(dict["title"] as? String == "Buy milk")
        #expect(dict["dueDate"] as? String == "2026-05-13T10:00:00Z")
        #expect(dict["priority"] as? Int == 5)
        #expect(dict["isCompleted"] as? Bool == false)
    }
}
