in swiftui now every button has corner radius. i need corner radius only for last button and for whole list of buttons //
//  ActionSheetButtonView.swift
//  LikePost
//

import SwiftUI

struct ActionSheetButtonView: View {
    
    @Binding var selectedButtons: [String]
    
    @ObservedObject var config: ActionSheetButton
    let buttonType: ActionSheetSection.ActionType
    let isLastButton: Bool
    
    let action: () -> Void
    private let iconSize: CGFloat = 16
    
    init(
        selectedButtons: Binding<[String]>,
        config: ActionSheetButton,
        buttonType: ActionSheetSection.ActionType,
        isLastButton: Bool = false, // Default to false
        action: @escaping () -> Void
    ) {
        self._selectedButtons = selectedButtons
        self.config = config
        self.buttonType = buttonType
        self.isLastButton = isLastButton
        self.action = action
    }
    
    
    var body: some View {
        VStack(spacing: .zero) {
            buttonItem
                .padding(.leading, .lpUI.spacing.xxxs)
                .background(Color.lpUI.color.surface10)
         //       .padding(.bottom, .lpUI.spacing.xxxs)
               
            if !isLastButton {
                Divider()
                    .padding(.horizontal, .lpUI.spacing.xs)
            }
            

              
            if config.expandableData != nil {
                expandedSection
                    .padding(.leading, .lpUI.spacing.xxxs)
                    .frame(height: config.isExpanded ? nil : .zero)
                    .opacity(config.isExpanded ? 1 : 0)
            }
        }
    //    .background(config.isExpanded ? Color.lpUI.color.effects : .red)
   //     .cornerRadius(.lpUI.radius.s)
    }
    
    var buttonItem: some View {
        Button {
            if config.expandableData != nil {
                config.isExpanded.toggle()
            } else {
                action()
            }
        } label: {
            HStack {
                if let icon = config.icon {
                    Image(icon)
                        .resizable()
                        .scaledToFit()
                        .frame(size: iconSize)
                }
                VStack(alignment: .leading) {
                    Text(LocalizedStringKey(config.label))
                        .font(.lpUI.font.small)
                        .multilineTextAlignment(.leading)
                    if let subtitle = config.subtitle {
                        Text(subtitle)
                            .font(.subheadline)
                            .foregroundColor(.gray)
                    }
                }
                Spacer()
                if let trailingButton = config.trailingButton {
                    Text(LocalizedStringKey(trailingButton))
                        .font(.lpUI.font.small)
                } else if let trailingIcon = config.trailingIcon {
                    Image(trailingIcon)
                } else if buttonType == .checkbox || buttonType == .singeSelection || buttonType == .exclusiveCheckbox {
                    Image(.tickIcon)
                        .padding(.horizontal, .lpUI.spacing.xs)
                        .opacity(selectedButtons.contains(config.tag) || config.selected ? 1 : 0)
                }
            }
        }
        .disabled(config.disabled)
        .foregroundColor(foregroundColor)
        .padding(.horizontal, .lpUI.spacing.xs)
        .padding(.vertical, .lpUI.spacing.small)
        .frame(maxWidth: .infinity)
     //   .buttonStyle(.borderless)
        .cornerRadius(.lpUI.radius.l)
    }
    
    var expandedSection: some View {
        ActionSheetButtonsContainer(isExpandable: false, content: {
            if let expandableData = config.expandableData {
                ForEach(expandableData.buttons) { button in
                    ActionSheetButtonView(
                        selectedButtons: $selectedButtons,
                        config: button,
                        buttonType: expandableData.type
                    ) {
                        self.expandableButtonAction(button: button, in: expandableData)
                    }
                }
            }
        })
    }
    
    var foregroundColor: Color {
        guard !config.disabled else { return Color.lpUI.color.onSurface0.opacity(0.6)}
        switch config.type {
        case .plain: return Color.lpUI.color.onSurface0
        case .destructive: return Color.lpUI.color.errorAccented
        }
    }
    
    func expandableButtonAction(button: ActionSheetButton, in section: ActionSheetSection) {
        switch section.type {
        case .checkbox:
            button.selected.toggle()
        case .singeSelection:
            config.trailingButton = button.label
            button.selected = true
            for item in section.buttons {
                item.selected = item == button
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                config.isExpanded = false
            }
        case .exclusiveCheckbox:
            if button.exclusiveInSelection {
                button.selected = true
                for item in section.buttons where item != button {
                    item.selected = false
                }
            } else {
                button.selected.toggle()
                let uniqueIsSelected = section.buttons.filter({ $0.selected }).isEmpty
                for item in section.buttons where item.exclusiveInSelection {
                    item.selected = uniqueIsSelected
                }
            }
        case .default: break
        }
        button.action?()
    }
}
    func complainListSheet(list: ListModel, listComplainTypes: [ComplaintReasonModel]) -> ActionBottomSheetConfig {
        var sections = [
            ActionSheetSection(listComplainTypes.filter { $0.code != ComplaintReasonModel.copyrightInfringementCode }
                .map { type in
                ActionSheetButton(
                    type.titleText,
                    tag: type.id,
                    selected: false
                )
            }, type: .checkbox)
        ]
        if let complaint = listComplainTypes.first(where: { $0.code == ComplaintReasonModel.copyrightInfringementCode }) {
            sections.append(ActionSheetSection([
                ActionSheetButton(
                    complaint.title ?? "",
                    icon: complaint.icon,
                    trailingIcon: .exportButtonIcon,
                    disabled: false) { [weak self] in
                        self?.copyrightInfringementFormManager.openCopyrightInfringementForm(type: .list, entityId: list.id)
                    }
            ]))
        }
        return ActionBottomSheetConfig(
            header: ActionBottomSheetHeader(
                title: "Report this list",
                subtitle: "Please select one or more categories."),
            sections: sections,
            bottomButton: ActionSheetBottomButton(
                "Submit",
                enabledBehaviour: .enabledOnAnySelection) { [weak self] selected in
                    guard let self, let selected else { return }
                    self.complain(list: list, reasons: selected)
                }
        )
    }