import ForgejoKit
import SwiftUI

struct WorkflowRunRow: View {
    let run: WorkflowRun

    var body: some View {
        HStack(alignment: .top, spacing: 10) {
            WorkflowStatusIcon(status: run.status)
                .font(.body)
                .padding(.top, 2)

            VStack(alignment: .leading, spacing: 4) {
                Text(run.displayName)
                    .font(.headline)
                    .lineLimit(2)

                HStack(spacing: 6) {
                    if let index = run.indexInRepo {
                        Text("#\(index)")
                    }
                    if let ref = run.prettyRef, !ref.isEmpty {
                        Label(ref, systemImage: "arrow.triangle.branch")
                    }
                    if let sha = run.shortCommitSha {
                        Label(sha, systemImage: "number")
                    }
                }
                .font(.caption)
                .foregroundStyle(.secondary)

                HStack(spacing: 6) {
                    if let event = run.event, !event.isEmpty {
                        Text(event)
                    }
                    if let actor = run.triggerUser?.login, !actor.isEmpty {
                        Text("by \(actor)")
                    }
                    if let updated = run.updatedDisplay {
                        Text(updated, style: .relative)
                            .foregroundStyle(.tertiary)
                    }
                }
                .font(.caption2)
                .foregroundStyle(.secondary)
            }
        }
        .padding(.vertical, 2)
        .stateAccent(run.statusStyle.tint)
    }
}
