package webhook

import "encoding/json"

// OpenAPI beschrijft de JSON-body die een webhook ontvangt.
func OpenAPI() []byte {
	obj := func(desc string, props map[string]any, required []string) map[string]any {
		return map[string]any{
			"type": "object", "description": desc,
			"properties": props, "required": required,
		}
	}
	str := func(desc string) map[string]any {
		return map[string]any{"type": "string", "description": desc}
	}
	num := func(desc string) map[string]any {
		return map[string]any{"type": "number", "description": desc}
	}
	boolean := func(desc string) map[string]any {
		return map[string]any{"type": "boolean", "description": desc}
	}
	post := func(summary, ref string) map[string]any {
		return map[string]any{
			"post": map[string]any{
				"summary": summary,
				"requestBody": map[string]any{
					"required": true,
					"content": map[string]any{
						"application/json": map[string]any{
							"schema": map[string]any{"$ref": ref},
						},
					},
				},
				"responses": map[string]any{"200": map[string]any{"description": "The receiver accepted the event."}},
			},
		}
	}
	doc := map[string]any{
		"openapi": "3.1.0",
		"info": map[string]any{
			"title":   "BlueGit webhook payloads",
			"version": "1",
			"description": "JSON body of a BlueGit webhook when the body template is empty. " +
				"pipeline_success and pipeline_failure include the pipeline and every job. " +
				"A failed job adds the last 200 log lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +
				"log_truncated is true when the log was cut. log_url is the full log. " +
				"A push lists ref, before, after and up to 20 commits. " +
				"A merge request uses action opened, merged or commented, and commented includes the comment text. " +
				"A release lists the tag, name, commit and asset URLs.",
		},
		"webhooks": map[string]any{
			"pipeline":      post("Pipeline finished", "#/components/schemas/PipelineEvent"),
			"push":          post("Push", "#/components/schemas/PushEvent"),
			"merge_request": post("Merge request", "#/components/schemas/MergeRequestEvent"),
			"release":       post("Release published", "#/components/schemas/ReleaseEvent"),
		},
		"components": map[string]any{
			"schemas": map[string]any{
				"PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{
					"object_kind": str("Always pipeline."),
					"type":        str("pipeline_success or pipeline_failure."),
					"project":     str("owner/name."),
					"actor":       str("User who started the pipeline."),
					"summary":     str("One-line summary."),
					"url":         str("Pipeline page."),
					"pipeline_id": str("Same id as pipeline.id, as text."),
					"pipeline":    map[string]any{"$ref": "#/components/schemas/Pipeline"},
					"jobs":        map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Job"}},
				}, []string{"object_kind", "type", "project", "pipeline", "jobs"}),
				"Pipeline": obj("The pipeline this event is about.", map[string]any{
					"id":             map[string]any{"type": "integer", "description": "Pipeline id."},
					"status":         str("success or failed."),
					"ref":            str("Full ref, such as refs/heads/master."),
					"sha":            str("Commit SHA."),
					"commit_message": str("Subject and body of the commit."),
					"author":         str("User who started the pipeline."),
					"duration":       num("Seconds from start to finish."),
					"url":            str("Pipeline page."),
				}, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),
				"Job": obj("One job. log, log_truncated and log_url are present only when status is failed.", map[string]any{
					"name":          str("Job name."),
					"stage":         str("Stage name."),
					"status":        str("Job status."),
					"duration":      num("Seconds the job ran."),
					"allow_failure": boolean("True when a failure does not fail the pipeline."),
					"web_url":       str("Job page."),
					"log":           str("Tail of the console output."),
					"log_truncated": boolean("True when log is not the full console output."),
					"log_url":       str("Plain-text URL of the full log."),
				}, []string{"name", "stage", "status", "duration", "allow_failure", "web_url"}),
				"PushEvent": obj("A branch or tag update.", map[string]any{
					"object_kind": str("Always push."),
					"type":        str("Always push."),
					"project":     str("owner/name."),
					"actor":       str("User who pushed."),
					"ref":         str("Full ref."),
					"before":      str("Previous SHA, or 40 zeros when the ref is new."),
					"after":       str("New SHA."),
					"commits":     map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Commit"}},
				}, []string{"object_kind", "type", "project", "ref", "before", "after", "commits"}),
				"Commit": obj("One commit made reachable by the push.", map[string]any{
					"sha":     str("Commit SHA."),
					"message": str("Subject and body."),
					"author":  str("Name and email."),
					"url":     str("Commit page."),
				}, []string{"sha", "message", "author", "url"}),
				"MergeRequestEvent": obj("Opened, merged or commented.", map[string]any{
					"object_kind":   str("Always merge_request."),
					"type":          str("Always merge_request."),
					"project":       str("owner/name."),
					"actor":         str("User who did this action."),
					"merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},
					"comment":       str("Comment text. Present only when action is commented."),
				}, []string{"object_kind", "type", "project", "merge_request"}),
				"MergeRequest": obj("The merge request.", map[string]any{
					"id":            map[string]any{"type": "integer", "description": "Number in the project."},
					"title":         str("Title."),
					"description":   str("Description. May be empty."),
					"source_branch": str("Source branch."),
					"target_branch": str("Target branch."),
					"status":        str("open, merged or closed."),
					"author":        str("User who opened the merge request."),
					"action":        str("opened, merged or commented."),
					"url":           str("Merge request page."),
				}, []string{"id", "title", "description", "source_branch", "target_branch", "status", "author", "action"}),
				"ReleaseEvent": obj("A published release.", map[string]any{
					"object_kind": str("Always release."),
					"type":        str("Always release_published."),
					"project":     str("owner/name."),
					"release_tag": str("Same value as release.tag."),
					"release":     map[string]any{"$ref": "#/components/schemas/Release"},
				}, []string{"object_kind", "type", "project", "release"}),
				"Release": obj("The release.", map[string]any{
					"tag":    str("Tag name."),
					"name":   str("Release title."),
					"commit": str("Commit SHA."),
					"assets": map[string]any{"type": "array", "items": map[string]any{"$ref": "#/components/schemas/Asset"}},
				}, []string{"tag", "name", "commit", "assets"}),
				"Asset": obj("A file attached to the release.", map[string]any{
					"name": str("File name."),
					"url":  str("Download URL."),
					"size": map[string]any{"type": "integer", "description": "Size in bytes."},
				}, []string{"name", "url", "size"}),
			},
		},
	}
	raw, err := json.MarshalIndent(doc, "", "  ")
	if err != nil {
		return []byte("{}\n")
	}
	return append(raw, '\n')
}
