import SwiftUI

/// Per-clip instruction editing: trim points, audio, text overlays.
/// Works entirely on the recipe — usable even when the source file is missing,
/// since the fingerprint remembers the source duration.
struct DraftClipDetailView: View {
    @Environment(\.dismiss) private var dismiss
    @Binding var clip: DraftClip
    let source: SourceClip?

    private var sourceDuration: Double {
        source?.fingerprint.duration ?? max(clip.trimEnd, clip.trimStart + 1)
    }

    var body: some View {
        NavigationStack {
            Form {
                Section("Trim") {
                    VStack(alignment: .leading) {
                        Text("Start: \(timeString(clip.trimStart))")
                            .font(.caption)
                        Slider(value: $clip.trimStart, in: 0...max(0.1, clip.trimEnd - 0.1))
                    }
                    VStack(alignment: .leading) {
                        Text("End: \(timeString(clip.trimEnd))")
                            .font(.caption)
                        Slider(value: $clip.trimEnd, in: min(clip.trimStart + 0.1, sourceDuration)...sourceDuration)
                    }
                    LabeledContent("Segment length", value: timeString(clip.segmentDuration))
                }
                Section("Audio") {
                    Toggle("Mute clip audio", isOn: $clip.isMuted)
                    VStack(alignment: .leading) {
                        Text("Volume: \(Int(clip.volume * 100))%")
                            .font(.caption)
                        Slider(value: $clip.volume, in: 0...1)
                    }
                    .disabled(clip.isMuted)
                }
                Section {
                    ForEach($clip.textOverlays) { $overlay in
                        VStack(alignment: .leading, spacing: 8) {
                            TextField("Text", text: $overlay.text)
                            HStack {
                                Text("Size: \(Int(overlay.fontSize))")
                                    .font(.caption)
                                    .frame(width: 64, alignment: .leading)
                                Slider(value: $overlay.fontSize, in: 12...96)
                            }
                            Toggle("Background", isOn: $overlay.hasBackground)
                        }
                    }
                    .onDelete { offsets in
                        clip.textOverlays.remove(atOffsets: offsets)
                    }
                    Button {
                        clip.textOverlays.append(.centered())
                    } label: {
                        Label("Add text overlay", systemImage: "plus")
                    }
                } header: {
                    Text("Text overlays")
                } footer: {
                    Text("New overlays start auto-centered. Rendering happens in step 3.")
                }
                if source == nil {
                    Section {
                        Label("This clip's source was removed from the library. Your edits are kept; relink or re-import it to restore playback.", systemImage: "exclamationmark.triangle.fill")
                            .foregroundStyle(.orange)
                            .font(.callout)
                    }
                }
            }
            .navigationTitle(source?.displayName ?? "Missing source")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Done") { dismiss() }
                }
            }
        }
    }
}
