import type { BranchSummary, CommitSummary, TagSummary, TreeEntrySummary } from "$lib/api/repo";

export type { BranchSummary, CommitSummary, TagSummary, TreeEntrySummary };

export interface RepoInfo {
	uri: string;
	rkey: string;
	name: string;
	ownerDid: string;
	ownerHandle: string;
	repoDid?: string;
	knot: string;
	spindle?: string;
	description?: string;
	website?: string;
	topics?: string[];
	source?: RepoSource;
	defaultBranch: string;
}

/** the repo this one was forked from */
export interface RepoSource {
	ownerHandle: string;
	name: string;
}

export interface RepoCounts {
	stars: number;
	/** open only, same as the appview's tabs. drop the filter for the total */
	issues: number;
	/** open only, so neither merged nor closed */
	pulls: number;
	forks: number;
}

export interface LanguageSlice {
	name: string;
	percentage: number;
	share: number;
}

export interface IssueSummary {
	uri: string;
	rkey: string;
	title: string;
	state: "open" | "closed";
	authorHandle: string;
	authorDid: string;
	createdAt: string;
	commentCount: number;
}

/** the spindle's workflow states, same strings as sh.tangled.ci.pipeline */
export type PipelineStatus = "pending" | "running" | "success" | "failed" | "timeout" | "cancelled";

export interface WorkflowSummary {
	name: string;
	status: PipelineStatus;
	/** wall time in ms; 0 until the workflow finishes */
	duration: number;
	/** first line of the spindle's error, when it failed before running */
	error?: string;
}

export type PipelineTrigger =
	| { kind: "push"; targetRef: string }
	| {
			kind: "pull_request";
			targetRef: string;
			/** where the head came from — `handle/repo:branch` when it is a fork */
			sourceLabel: string;
			/** the pull page, absent when the pull record is gone */
			pullPath?: string;
	  }
	| { kind: "manual" };

export type PipelineTriggerKind = PipelineTrigger["kind"];

export interface PipelineSummary {
	id: string;
	/** the commit the spindle checked out */
	sha: string;
	createdAt: string;
	trigger: PipelineTrigger;
	/** empty while the spindle has not reported back yet */
	workflows: WorkflowSummary[];
}
