import ForgejoKit
import SwiftUI

struct PullRequestsOverviewView: View {
    let authService: AuthenticationService

    init(authService: AuthenticationService) {
        self.authService = authService
    }

    var body: some View {
        SearchableOverviewView(
            authService: authService,
            issueType: "pulls",
            navigationTitle: "Pull Requests",
            searchPrompt: "Search pull requests",
            emptyTitle: "No Pull Requests",
            emptyOpenIcon: "arrow.triangle.pull",
            emptyClosedIcon: "arrow.triangle.pull",
            itemNoun: "pull requests",
            createButtonId: "pr-create-button",
            showReviewRequested: true,
            row: { issue in
                PullRequestOverviewRow(issue: issue)
            },
            detail: { repository, number, auth in
                PullRequestDetailView(
                    repository: repository,
                    prNumber: number,
                    authService: auth,
                )
            },
            createView: { repo, auth, embedded, onCreated in
                PullRequestCreateView(
                    repository: repo,
                    authService: auth,
                    embeddedInNavigation: embedded,
                    onCreated: onCreated,
                )
            },
        )
    }

    #if DEBUG
        init(preview _: Void, authService: AuthenticationService, pullRequests _: [Issue]) {
            self.authService = authService
        }
    #endif
}

struct PullRequestOverviewRow: View {
    let issue: Issue
    var instanceName: String?

    private var status: PRStatusStyle {
        PRStatusStyle(state: issue.pullRequestStateValue, merged: issue.pullRequest?.merged, draft: nil)
    }

    var body: some View {
        HStack(alignment: .top, spacing: 10) {
            Image(systemName: status.icon)
                .foregroundStyle(status.color)
                .font(.body)
                .padding(.top, 2)

            VStack(alignment: .leading, spacing: 4) {
                HStack(spacing: 6) {
                    Text(issue.title)
                        .font(.headline)
                        .lineLimit(2)
                    if let instanceName {
                        InstanceBadge(name: instanceName)
                    }
                }

                if let repository = issue.repository {
                    Text(repository.fullName)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }

                HStack(spacing: 6) {
                    Text("#\(issue.number)")
                    if issue.comments > 0 {
                        Label("\(issue.comments)", systemImage: "bubble.right")
                    }
                }
                .font(.caption)
                .foregroundStyle(.secondary)

                if !issue.labels.isEmpty {
                    FlowLayout(spacing: 4) {
                        ForEach(issue.labels) { label in
                            IssueLabelView(label: label)
                        }
                    }
                }
            }
        }
        .padding(.vertical, 2)
        .stateAccent(status.color)
    }
}

#if DEBUG
    #Preview {
        NavigationStack {
            PullRequestsOverviewView(
                preview: (),
                authService: .previewDefault,
                pullRequests: [.previewPullRequest],
            )
        }
    }
#endif
