//
// WorkoutStarter.swift
// Workouts
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import IndieSync
import Foundation

/// The one place a workout is created from a routine. Both start paths — the routine
/// picker sheet and the Today board — call this so the plan-time log snapshot and
/// the returned id stay identical per site.
@MainActor
enum WorkoutStarter {
    /// Builds and saves a fresh workout from a routine (plan-time log snapshot per
    /// exercise) and returns the new workout's id so the caller can navigate once the
    /// cache catches up. Deliberately does NOT touch the watch: the wrist session
    /// launches only when the first exercise actually starts (see
    /// `SyncEngine.onWorkoutBecameActive`), so a peek-then-back never spins one up.
    static func start(routine: Routine, sync: SyncEngine) async -> String {
        let startDate = Date()
        let logs = routine.exercisesArray.enumerated().map { index, exercise in
            WorkoutLogDocument(planFrom: ExerciseDocument(from: exercise), order: index, date: startDate)
        }

        // A freshly started workout has no `end` — only completion stamps it.
        let doc = WorkoutDocument(
            schemaVersion: WorkoutDocument.currentSchemaVersion,
            id: ULID.make(),
            routineID: routine.id,
            routineName: routine.name,
            start: startDate,
            end: nil,
            status: WorkoutStatus.notStarted.rawValue,
            createdAt: startDate,
            updatedAt: startDate,
            logs: logs,
            restSeconds: routine.restSeconds, autoAdvance: routine.autoAdvance,
            targetHeartRate: routine.targetHeartRate
        )

        await sync.save(workout: doc)
        return doc.id
    }
}
