//
//  ContentView.swift
//  Workouts
//
//  Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//

import SwiftUI

struct ContentView: View {
    @Environment(LiveRunState.self) private var liveRun

    var body: some View {
        TabView {
            Tab("Today", systemImage: "sun.max.fill") {
                TodayView()
            }
            Tab("Progress", systemImage: "chart.line.uptrend.xyaxis") {
                ProgressTabView()
            }
            Tab("Library", systemImage: "books.vertical.fill") {
                LibraryView()
            }
            Tab("Settings", systemImage: "gearshape.2") {
                SettingsView()
            }
        }
        // Prop the phone up and it runs the live Apple Watch workout — two-way: drive from
        // either device and the other follows.
        .fullScreenCover(isPresented: Binding(
            get: { liveRun.presentable != nil },
            set: { presenting in if !presenting { liveRun.mute() } }
        )) {
            if let frame = liveRun.presentable {
                // Re-key on the followed log so a flow-mode auto-advance (the driver
                // moving to the next exercise) rebuilds the mirror for the new exercise
                // instead of leaving a stale one pinned to the previous log.
                LiveRunCoverView(frame: frame) { liveRun.mute() }
                    .id(frame.logID)
            }
        }
    }
}
