#if DEBUG
import Foundation
import SwiftData

/// DEBUG-only sample data + launch-arg plumbing for App Store screenshot capture.
/// Activated by launching with `--screenshot`; `--screen <name>` picks which screen
/// the app renders (see each target's screenshot root). Never compiled into release.
enum ScreenshotSeed {
    static var isActive: Bool { CommandLine.arguments.contains("--screenshot") }

    static func screen(default def: String) -> String {
        let args = CommandLine.arguments
        if let i = args.firstIndex(of: "--screen"), i + 1 < args.count { return args[i + 1] }
        return def
    }

    /// Insert a believable in-progress workout, a few finished sessions (so charts have
    /// a trend), and the starter splits. Returns the in-progress workout to display.
    @MainActor
    @discardableResult
    static func populate(_ context: ModelContext) -> Workout? {
        // Idempotent: clear any prior seed so repeated launches don't stack duplicates
        // (cascade deletes take the child exercises/logs with them).
        try? context.delete(model: Workout.self)
        try? context.delete(model: Split.self)

        let cal = Calendar(identifier: .gregorian)
        let today = Date()
        func daysAgo(_ n: Int) -> Date { cal.date(byAdding: .day, value: -n, to: today) ?? today }

        // ---- Splits (with exercises) ------------------------------------------
        let splits: [(String, String, String, [(String, Int, Int, Int)])] = [
            ("Upper Body", "purple", "dumbbell.fill", [
                ("Bench Press", 4, 10, 135), ("Overhead Press", 4, 10, 75),
                ("Lat Pulldown", 4, 12, 120), ("Bicep Curl", 3, 12, 30),
            ]),
            ("Lower Body", "blue", "figure.strengthtraining.traditional", [
                ("Back Squat", 5, 5, 185), ("Romanian Deadlift", 4, 8, 155),
                ("Leg Press", 4, 12, 270), ("Calf Raise", 4, 15, 90),
            ]),
            ("Core", "orange", "figure.core.training", [
                ("Plank", 3, 0, 0), ("Hanging Leg Raise", 3, 12, 0),
                ("Cable Crunch", 3, 15, 50),
            ]),
        ]

        for (sIndex, s) in splits.enumerated() {
            let split = Split(id: ULID.make(), name: s.0, color: s.1, systemImage: s.2,
                              order: sIndex, createdAt: today, updatedAt: today, jsonRelativePath: "")
            context.insert(split)
            for (eIndex, e) in s.3.enumerated() {
                let isDuration = e.0 == "Plank"
                let ex = Exercise(id: ULID.make(), name: e.0, order: eIndex, sets: e.1, reps: e.2,
                                  weight: e.3, loadType: (isDuration ? LoadType.duration : .weight).rawValue,
                                  durationTotalSeconds: isDuration ? 45 : 0, weightLastUpdated: today,
                                  weightReminderTimeIntervalWeeks: 2)
                ex.split = split
                context.insert(ex)
            }
        }

        let upper = splits[0]

        // ---- Past finished sessions (Bench Press trend for the chart) ----------
        let benchTrend = [115, 120, 125, 130]
        for (i, w) in benchTrend.enumerated() {
            let date = daysAgo((benchTrend.count - i) * 4)
            let workout = Workout(id: ULID.make(), splitID: nil, splitName: upper.0, start: date,
                                  end: date.addingTimeInterval(2400), statusRaw: WorkoutStatus.completed.rawValue,
                                  createdAt: date, updatedAt: date, jsonRelativePath: "")
            context.insert(workout)
            for (eIndex, e) in upper.3.enumerated() {
                let weight = e.0 == "Bench Press" ? w : e.3
                let log = WorkoutLog(id: ULID.make(), exerciseName: e.0, order: eIndex, sets: e.1,
                                     reps: e.2, weight: weight, loadType: LoadType.weight.rawValue,
                                     durationTotalSeconds: 0, currentStateIndex: e.1, completed: true,
                                     statusRaw: WorkoutStatus.completed.rawValue, notes: nil, date: date)
                log.workout = workout
                context.insert(log)
            }
        }

        // ---- The current in-progress session -----------------------------------
        let workout = Workout(id: ULID.make(), splitID: nil, splitName: upper.0, start: today,
                              end: nil, statusRaw: WorkoutStatus.inProgress.rawValue,
                              createdAt: today, updatedAt: today, jsonRelativePath: "")
        context.insert(workout)
        // Bench Press done, Overhead Press partway, the rest to go.
        let progress = [(4, WorkoutStatus.completed), (2, .inProgress), (0, .notStarted), (0, .notStarted)]
        for (eIndex, e) in upper.3.enumerated() {
            let (idx, status) = progress[eIndex]
            let log = WorkoutLog(id: ULID.make(), exerciseName: e.0, order: eIndex, sets: e.1, reps: e.2,
                                 weight: e.0 == "Bench Press" ? 135 : e.3, loadType: LoadType.weight.rawValue,
                                 durationTotalSeconds: 0, currentStateIndex: idx, completed: status == .completed,
                                 statusRaw: status.rawValue, notes: nil, date: today)
            log.workout = workout
            context.insert(log)
        }

        try? context.save()
        return workout
    }
}
#endif
