//
//  ScenicPromotionSettingsMaterialUploadCard.swift
//  suixinkan
//
//  Created by Codex on 2026/7/2.
//

import SwiftUI

/// 设置页视频 / 图片上传模块。
struct ScenicPromotionSettingsMaterialUploadCard: View {
    @ObservedObject var viewModel: ScenicPromotionSettingsViewModel
    let kind: ScenicPromotionUploadKind
    let onUploadFailed: () -> Void

    var body: some View {
        let materials = viewModel.materials(for: kind)

        ScenicPromotionSettingsCard(
            title: kind.title,
            subtitle: viewModel.selectedMaterialPoint == nil
                ? "先选择素材所属打卡点后再上传"
                : "当前展示 \(viewModel.selectedMaterialPointName) 已上传素材",
            icon: kind.iconName,
            tint: kind == .video ? Color(hex: 0x7A4DFF) : AppDesign.primary
        ) {
            HStack {
                Text(viewModel.selectedMaterialPointName)
                    .font(.system(size: 14, weight: .semibold))
                    .foregroundStyle(AppDesign.textSecondary)
                    .lineLimit(1)
                    .minimumScaleFactor(0.78)
                Spacer()
                Button {
                    guard viewModel.addMaterial(kind: kind) else {
                        onUploadFailed()
                        return
                    }
                } label: {
                    Label(kind.uploadTitle, systemImage: "square.and.arrow.up")
                        .font(.system(size: 13, weight: .bold))
                        .foregroundStyle(.white)
                        .padding(.horizontal, 12)
                        .frame(height: 34)
                        .background(
                            viewModel.selectedMaterialPoint == nil
                            ? Color(hex: 0xAAB7C7)
                            : AppDesign.primary,
                            in: Capsule()
                        )
                }
                .buttonStyle(.plain)
            }

            if viewModel.selectedMaterialPoint == nil {
                ScenicPromotionSettingsInlineNotice(
                    text: "请选择素材所属打卡点后再上传\(kind.shortTitle)",
                    tint: AppDesign.warning
                )
            } else if materials.isEmpty {
                ScenicPromotionSettingsMaterialEmptyView(kind: kind)
            } else {
                VStack(spacing: 10) {
                    ForEach(materials) { material in
                        ScenicPromotionSettingsMaterialRowView(
                            material: material,
                            onPreview: { viewModel.openMaterialPreview(material) },
                            onDelete: { viewModel.deleteMaterial(material) }
                        )
                    }
                }
            }
        }
    }
}

#if DEBUG
#Preview("视频上传卡片") {
    ScenicPromotionSettingsMaterialUploadCard(
        viewModel: ScenicPromotionPreviewSupport.settingsViewModel(),
        kind: .video,
        onUploadFailed: {}
    )
    .padding()
    .background(AppDesign.background)
}
#endif
