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

import SwiftData

/// Where a bundled seed currently stands relative to the live cache — computed fresh
/// from `@Query` routines on every render so a local edit, a remote change, or a
/// fork-on-edit clone is reflected immediately. Shared by the iOS gallery list, its
/// preview screen, and the Mac equivalents so all classify a seed identically.
enum StarterSeedState {
    /// A live routine with the seed's fixed id exists — the starter is already in the
    /// user's library (possibly since edited elsewhere; the id itself only forks at
    /// edit time, so "added" still matches a pristine seed).
    case added(Routine)
    /// Not added, but some *other* live routine already carries the seed's name —
    /// restoring would read as a confusing duplicate, so the add action is blocked.
    case nameTaken
    /// Neither added nor blocked — free to restore.
    case restorable
}

extension SeedLibrary.Seed {
    /// Classify this seed against the live routine set. See `StarterSeedState`.
    func state(among routines: [Routine]) -> StarterSeedState {
        if let live = routines.first(where: { $0.id == id }) { return .added(live) }
        if routines.contains(where: { $0.name == doc.name }) { return .nameTaken }
        return .restorable
    }
}
