import SwiftUI

/// Apple Intelligence 式多彩流光线:蓝→紫→粉→橙→青,横向无缝循环流动。
/// 全 App「AI 计算中」时刻的统一视觉点缀(日记 AI 辅助、身体档案报告生成/检索等待)。
///
/// 注意:这条线的颜色是刻意走出 `Tj.Palette` 单色系统的 AI 高光点缀(应产品要求的
/// Apple 风格),仅此组件如此;其余 UI 仍严格守 §9 单色 token。
struct AIFlowBar: View {
    var height: CGFloat = 3
    /// 流动一整圈的秒数,越小越快。
    var cycle: Double = 1.0

    @State private var phase: CGFloat = 0

    /// 颜色重复一遍:offset 走完一个整段时首尾同色,循环无缝。
    private static let flow: [Color] = {
        let base: [Color] = [
            Color(red: 0.35, green: 0.47, blue: 0.98),  // 蓝
            Color(red: 0.62, green: 0.36, blue: 0.92),  // 紫
            Color(red: 0.96, green: 0.40, blue: 0.62),  // 粉
            Color(red: 1.00, green: 0.55, blue: 0.30),  // 橙
            Color(red: 0.30, green: 0.80, blue: 0.92),  // 青
        ]
        return base + base
    }()

    var body: some View {
        GeometryReader { geo in
            let w = geo.size.width
            Capsule()
                .fill(LinearGradient(colors: Self.flow,
                                     startPoint: .leading, endPoint: .trailing))
                .frame(width: w * 2)
                .offset(x: phase)
                .onAppear {
                    phase = 0
                    withAnimation(.linear(duration: cycle).repeatForever(autoreverses: false)) {
                        phase = -w
                    }
                }
        }
        .frame(height: height)
        .clipShape(Capsule())
    }
}
