//
//  EQUIPMENT_INTEGRATION_GUIDE.md
//  iSMARTtrain
//
//  Created by Stuart Tevendale on 11/03/2026.
//

# Equipment Integration Guide

This guide explains all the changes made to integrate Equipment management into iSMARTtrain.

## Files Created

### 1. EquipmentEditorView.swift
A comprehensive view for adding and editing equipment items with:
- Basic information (name, brand, model, purchase date)
- Type selection (bike, shoes, other)
- Conditional bike-specific fields (bike type, linked to)
- Component management (for bikes only)
- Add, edit, and delete components
- Notes section

### 2. EquipmentListView.swift
A list view displaying all equipment with:
- Search functionality
- Grouped by equipment type (Bikes, Shoes, Other)
- Tap to edit equipment
- Swipe actions for edit/delete
- Empty state view
- Component count badges for bikes
- Displays brand, model, purchase date, and linked sport

### 3. AppCommands.swift
Menu bar commands for the Manage menu with:
- User Settings (⌘⇧U)
- Sports (⌘⇧S)
- Equipment (⌘⇧E)

## Files Modified

### 1. DetailViewConstants.swift
Added:
```swift
static let kShowEquipment = 12
```

### 2. SidebarView.swift
Added Equipment navigation link under the MANAGE section:
```swift
NavigationLink(value: DetailViewConstants.kShowEquipment) {
    Label {
        Text("Equipment")
    } icon: {
        Image(systemName: "bicycle.circle")
            .frame(width: 20, height: 20)
            .background(Color.gray.opacity(0.2))
            .cornerRadius(2)
    }
}
```

### 3. DetailView.swift
Added routing for Equipment view:
```swift
else if selected == DetailViewConstants.kShowEquipment {
    EquipmentListView()
}
```

## Integration Steps

### Step 1: Add Menu Commands to Your App
In your main app file (e.g., `iSMARTtrainApp.swift`), add the commands:

```swift
@main
struct iSMARTtrainApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .modelContainer(for: [
                    Item.self,
                    User.self,
                    ISTSport.self,
                    Equipment.self,
                    Component.self
                ])
        }
        .commands {
            AppCommands()  // Add this line
        }
    }
}
```

### Step 2: Add Notification Name (if needed)
If you have a separate file for notification names, add:

```swift
extension Notification.Name {
    static let changeSelection = Notification.Name("changeSelection")
    static let showAddSession = Notification.Name("showAddSession")
    static let showEquipment = Notification.Name("showEquipment")
}
```

## Features

### Equipment Management
- ✅ Add new equipment (bikes, shoes, other)
- ✅ Edit existing equipment
- ✅ Delete equipment
- ✅ Search equipment by name, brand, or model
- ✅ Group equipment by type

### Bike-Specific Features
- ✅ Bike type selection (Road, Mountain, Time Trial, Gravel, Track, Other)
- ✅ Linked to sport/activity type
- ✅ Component tracking (wheels, tires, brakes, chain, cassette, etc.)
- ✅ Add/edit/delete components

### Navigation
- ✅ Sidebar navigation under MANAGE section
- ✅ Menu bar item under Manage menu
- ✅ Keyboard shortcut: ⌘⇧E

## Usage

### From Sidebar
1. Click "Equipment" in the MANAGE section
2. Click "Add Equipment" button
3. Fill in details
4. If bike type, add components as needed
5. Click "Save"

### From Menu Bar
1. Go to Manage → Equipment (or press ⌘⇧E)
2. Follow same steps as above

### Editing Equipment
- **Click** on any equipment item to edit
- **Swipe** (or right-click) for edit/delete options

### Managing Components (Bikes Only)
When editing a bike:
1. Scroll to Components section
2. Click "Add Component"
3. Enter brand, model, date added, and notes
4. Click "Save"
5. Tap component to edit, or swipe to delete

## Data Model Relationships

```
User
  └─ Equipment (one-to-many)
       └─ Component (one-to-many, bikes only)
```

Equipment includes:
- timestamp, purchased date
- name, brand, model, notes
- type (bike/shoes/other)
- bikeType (road/mountain/etc.) - bikes only
- linkedTo (sport type) - bikes only
- components array - bikes only

## Notes

- Component management only appears when equipment type is "bike"
- If equipment type changes from bike to non-bike, all components are deleted
- Components use cascade delete rule, so deleting equipment deletes its components
- All views are compatible with macOS (removed iOS-only modifiers)
