import SwiftUI

// MARK: - MessageCallView

/// Displays a call attachment inside a message bubble.
///
/// Telegram iOS parity: `ChatMessageCallBubbleContentNode.swift`
/// — NO icon circle on left, title at left edge, arrow + duration below, phone icon right.
///
/// Preview format: duration seconds as plain int (0 = missed/rejected).
struct MessageCallView: View {

    let attachment: MessageAttachment
    let message: ChatMessage
    let outgoing: Bool
    let currentPublicKey: String
    let actions: MessageCellActions

    var body: some View {
        HStack(spacing: 0) {
            // Left: title + arrow/duration — Telegram: NO icon circle
            VStack(alignment: .leading, spacing: 2) {
                Text(titleText)
                    .font(.system(size: 16, weight: .medium))
                    .foregroundStyle(outgoing ? .white : RosettaColors.Adaptive.text)

                HStack(spacing: 4) {
                    Image(systemName: arrowIconName)
                        .font(.system(size: 10, weight: .bold))
                        .foregroundStyle(arrowColor)

                    Text(subtitleText)
                        .font(.system(size: 13))
                        .foregroundStyle(
                            isMissedOrRejected
                                ? Color(hex: 0xFF4747)
                                : (outgoing ? .white.opacity(0.6) : RosettaColors.Adaptive.textSecondary)
                        )
                }
            }

            Spacer(minLength: 8)

            // Right: phone icon only — Telegram: NO circle background
            Button {
                let peerKey = outgoing ? message.toPublicKey : message.fromPublicKey
                actions.onCall(peerKey)
            } label: {
                Image(systemName: "phone.fill")
                    .font(.system(size: 22))
                    .foregroundStyle(outgoing ? .white : Color(hex: 0x248AE6))
                    .frame(width: 36, height: 36)
            }
            .buttonStyle(.plain)
        }
        .padding(.leading, 11)
        .padding(.trailing, 10)
        .padding(.vertical, 6)
    }

    // MARK: - Computed Properties

    private var durationSeconds: Int {
        AttachmentPreviewCodec.parseCallDurationSeconds(attachment.preview)
    }

    private var isMissedOrRejected: Bool {
        durationSeconds == 0
    }

    private var isIncoming: Bool {
        !outgoing
    }

    private var titleText: String {
        if isMissedOrRejected {
            return isIncoming ? "Missed Call" : "Cancelled Call"
        }
        return isIncoming ? "Incoming Call" : "Outgoing Call"
    }

    private var subtitleText: String {
        if isMissedOrRejected {
            return "Declined"
        }
        let minutes = durationSeconds / 60
        let seconds = durationSeconds % 60
        return String(format: "%d:%02d", minutes, seconds)
    }

    private var arrowIconName: String {
        isIncoming ? "arrow.down.left" : "arrow.up.right"
    }

    private var arrowColor: Color {
        isMissedOrRejected ? Color(hex: 0xFF4747) : Color(hex: 0x36C033)
    }
}
