import SwiftUI

// MARK: - ChatRowShimmerView

/// Placeholder shimmer row displayed during chat list loading.
struct ChatRowShimmerView: View {
    @State private var phase: CGFloat = 0
    @Environment(\.colorScheme) private var colorScheme

    var body: some View {
        HStack(spacing: 12) {
            Circle()
                .fill(shimmerGradient)
                .frame(width: 62, height: 62)

            VStack(alignment: .leading, spacing: 8) {
                RoundedRectangle(cornerRadius: 4)
                    .fill(shimmerGradient)
                    .frame(width: 140, height: 14)

                RoundedRectangle(cornerRadius: 4)
                    .fill(shimmerGradient)
                    .frame(width: 200, height: 12)
            }

            Spacer()
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 10)
        .task {
            withAnimation(.linear(duration: 1.4).repeatForever(autoreverses: false)) {
                phase = 1
            }
        }
    }

    private var shimmerGradient: LinearGradient {
        let baseOpacity = colorScheme == .dark ? 0.06 : 0.08
        let peakOpacity = colorScheme == .dark ? 0.12 : 0.16
        return LinearGradient(
            colors: [
                Color.gray.opacity(baseOpacity),
                Color.gray.opacity(peakOpacity),
                Color.gray.opacity(baseOpacity),
            ],
            startPoint: UnitPoint(x: phase - 0.4, y: 0),
            endPoint: UnitPoint(x: phase + 0.4, y: 0)
        )
    }
}
