//
//  WidgetActionTests.swift
//  honeyDueTests
//
//  Unit tests for WidgetDataManager.WidgetAction (Codable, Equatable, accessors)
//  and WidgetDataManager.parseDate static helper.
//

import Testing
import Foundation
@testable import honeyDue

// MARK: - WidgetAction Codable Tests

struct WidgetActionCodableTests {

    @Test func encodeDecodeRoundTrip() throws {
        let original = WidgetDataManager.WidgetAction.completeTask(taskId: 42, taskTitle: "Fix leak")
        let data = try JSONEncoder().encode(original)
        let decoded = try JSONDecoder().decode(WidgetDataManager.WidgetAction.self, from: data)
        #expect(decoded == original)
    }

    @Test func decodedValuesMatch() throws {
        let original = WidgetDataManager.WidgetAction.completeTask(taskId: 99, taskTitle: "Paint walls")
        let data = try JSONEncoder().encode(original)
        let decoded = try JSONDecoder().decode(WidgetDataManager.WidgetAction.self, from: data)
        #expect(decoded.taskId == 99)
        #expect(decoded.taskTitle == "Paint walls")
    }
}

// MARK: - WidgetAction Equatable Tests

struct WidgetActionEquatableTests {

    @Test func sameValuesAreEqual() {
        let a = WidgetDataManager.WidgetAction.completeTask(taskId: 1, taskTitle: "Test")
        let b = WidgetDataManager.WidgetAction.completeTask(taskId: 1, taskTitle: "Test")
        #expect(a == b)
    }

    @Test func differentTaskIdNotEqual() {
        let a = WidgetDataManager.WidgetAction.completeTask(taskId: 1, taskTitle: "Test")
        let b = WidgetDataManager.WidgetAction.completeTask(taskId: 2, taskTitle: "Test")
        #expect(a != b)
    }

    @Test func differentTaskTitleNotEqual() {
        let a = WidgetDataManager.WidgetAction.completeTask(taskId: 1, taskTitle: "Alpha")
        let b = WidgetDataManager.WidgetAction.completeTask(taskId: 1, taskTitle: "Beta")
        #expect(a != b)
    }
}

// MARK: - WidgetAction Accessor Tests

struct WidgetActionAccessorTests {

    @Test func taskIdReturnsCorrectValue() {
        let action = WidgetDataManager.WidgetAction.completeTask(taskId: 55, taskTitle: "Mow lawn")
        #expect(action.taskId == 55)
    }

    @Test func taskTitleReturnsCorrectValue() {
        let action = WidgetDataManager.WidgetAction.completeTask(taskId: 55, taskTitle: "Mow lawn")
        #expect(action.taskTitle == "Mow lawn")
    }
}

// MARK: - parseDate Tests

struct ParseDateTests {

    @Test func validDateStringReturnsDate() {
        let date = WidgetDataManager.parseDate("2024-06-15")
        #expect(date != nil)
    }

    @Test func nilInputReturnsNil() {
        let date = WidgetDataManager.parseDate(nil)
        #expect(date == nil)
    }

    @Test func emptyStringReturnsNil() {
        let date = WidgetDataManager.parseDate("")
        #expect(date == nil)
    }

    @Test func isoDateTimeExtractsDatePart() {
        let date = WidgetDataManager.parseDate("2025-12-26T00:00:00Z")
        #expect(date != nil)
        // Should parse the same as just the date part
        let dateDirect = WidgetDataManager.parseDate("2025-12-26")
        #expect(date == dateDirect)
    }

    @Test func invalidStringReturnsNil() {
        let date = WidgetDataManager.parseDate("not-a-date")
        #expect(date == nil)
    }
}
