import SwiftUI

/// Inline card for group invite links — renders inside message bubble (iOS 26+ SwiftUI path).
/// Matches Desktop `GroupInviteMessage` and Android `GroupInviteInlineCard` behavior.
struct GroupInviteCardView: View {
    let inviteString: String
    let title: String
    let groupId: String
    let isOutgoing: Bool
    let actions: MessageCellActions

    @State private var status: CardStatus = .notJoined

    enum CardStatus {
        case notJoined, joined, invalid, banned

        var color: Color {
            switch self {
            case .notJoined: Color(red: 0.14, green: 0.54, blue: 0.90)
            case .joined: Color(red: 0.20, green: 0.78, blue: 0.35)
            case .invalid, .banned: Color(red: 0.98, green: 0.23, blue: 0.19)
            }
        }

        var statusText: String {
            switch self {
            case .notJoined: "Invite to join this group"
            case .joined: "You are a member"
            case .invalid: "This invite is invalid"
            case .banned: "You are banned"
            }
        }

        var buttonTitle: String {
            switch self {
            case .notJoined: "Join Group"
            case .joined: "Open Group"
            case .invalid: "Invalid"
            case .banned: "Banned"
            }
        }

        var isActionable: Bool {
            self == .notJoined || self == .joined
        }
    }

    var body: some View {
        HStack(alignment: .top, spacing: 10) {
            AvatarView(
                initials: RosettaColors.groupInitial(name: title.isEmpty ? "Group" : title, publicKey: groupId),
                colorIndex: RosettaColors.avatarColorIndex(for: title.isEmpty ? "Group" : title, publicKey: groupId),
                size: 44
            )

            VStack(alignment: .leading, spacing: 3) {
                Text(title.isEmpty ? "Group" : title)
                    .font(.system(size: 15, weight: .semibold))
                    .foregroundStyle(isOutgoing ? .white : Color(status.color))
                    .lineLimit(1)

                Text(status.statusText)
                    .font(.system(size: 12))
                    .foregroundStyle(isOutgoing ? .white.opacity(0.7) : .secondary)
                    .lineLimit(1)

                Button(action: handleTap) {
                    Text(status.buttonTitle)
                        .font(.system(size: 13, weight: .medium))
                        .foregroundStyle(.white)
                        .padding(.horizontal, 14)
                        .padding(.vertical, 5)
                        .background(status.color, in: Capsule())
                }
                .buttonStyle(.plain)
                .disabled(!status.isActionable)
                .opacity(status.isActionable ? 1.0 : 0.5)
            }
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 8)
        .task { await checkStatus() }
    }

    private func handleTap() {
        if status == .joined {
            let dialogKey = "#group:\(groupId)"
            actions.onGroupInviteOpen(dialogKey)
        } else if status == .notJoined {
            actions.onGroupInviteTap(inviteString)
        }
    }

    private func checkStatus() async {
        // Local check first
        let isJoined = GroupRepository.shared.hasGroup(for: groupId)
        if isJoined { status = .joined }

        // Server check for authoritative status
        if let (_, serverStatus) = try? await GroupService.shared.checkInviteStatus(groupId: groupId) {
            let newStatus: CardStatus = switch serverStatus {
            case .joined: .joined
            case .notJoined: .notJoined
            case .invalid: .invalid
            case .banned: .banned
            }
            status = newStatus
        }
    }
}
