import Foundation
import Testing
import IndieSync
@testable import Workouts

/// Pins the plan-time snapshot (`WorkoutLogDocument(planFrom:order:date:)`) — the
/// single helper every "start workout" / "add exercise" path funnels through. The
/// machine comfort settings must ride along with the other plan fields, and the
/// nil-vs-empty distinction (not-a-machine vs machine-with-nothing-recorded) must
/// be preserved exactly.
struct WorkoutLogSnapshotTests {
    private static let ts = Date(timeIntervalSince1970: 1_700_000_000)

    @Test func snapshotCopiesMachineSettingsAndPlanFields() {
        let exercise = ExerciseDocument(
            id: ULID.make(), name: "Leg Press", order: 2, sets: 4, reps: 10,
            weight: 140, loadType: 1, durationSeconds: 0,
            machineSettings: [
                MachineSetting(name: "Seat Height", value: "4"),
                MachineSetting(name: "Back Rest Incline", value: "45°")
            ]
        )

        let log = WorkoutLogDocument(planFrom: exercise, order: 5, date: Self.ts)

        #expect(log.machineSettings == exercise.machineSettings)
        #expect(log.exerciseName == "Leg Press")
        #expect(log.order == 5)             // the passed order, not the exercise's
        #expect(log.sets == 4)
        #expect(log.reps == 10)
        #expect(log.weight == 140)
        #expect(log.loadType == 1)
        #expect(log.durationSeconds == 0)
        #expect(log.date == Self.ts)
        #expect(log.currentStateIndex == 0)
        #expect(log.status == WorkoutStatus.notStarted.rawValue)
    }

    @Test func snapshotPreservesNilForNonMachineExercise() {
        let exercise = ExerciseDocument(
            id: ULID.make(), name: "Plank", order: 0, sets: 3, reps: 0,
            weight: 0, loadType: 2, durationSeconds: 45
        )

        let log = WorkoutLogDocument(planFrom: exercise, order: 0, date: Self.ts)

        #expect(log.machineSettings == nil)
    }

    @Test func snapshotPreservesEmptyMachineSettings() {
        // Empty ≠ nil: a machine exercise with nothing recorded yet must stay empty,
        // not collapse to "not a machine exercise".
        let exercise = ExerciseDocument(
            id: ULID.make(), name: "Chest Press", order: 0, sets: 4, reps: 10,
            weight: 40, loadType: 1, durationSeconds: 0, machineSettings: []
        )

        let log = WorkoutLogDocument(planFrom: exercise, order: 0, date: Self.ts)

        #expect(log.machineSettings == [])
    }
}
