package webhook

import (
	"encoding/json"

	webhookdomain "github.com/codex-k8s/kodex/libs/go/domain/webhook"
)

type githubRunPayload struct {
	Source         string                     `json:"source"`
	DeliveryID     string                     `json:"delivery_id"`
	EventType      string                     `json:"event_type"`
	ReceivedAt     string                     `json:"received_at"`
	Repository     githubRunRepositoryPayload `json:"repository"`
	Installation   githubInstallationPayload  `json:"installation"`
	Sender         githubActorPayload         `json:"sender"`
	Action         string                     `json:"action"`
	RawPayload     json.RawMessage            `json:"raw_payload"`
	CorrelationID  string                     `json:"correlation_id"`
	Project        githubRunProjectPayload    `json:"project"`
	LearningMode   bool                       `json:"learning_mode"`
	DiscussionMode bool                       `json:"discussion_mode,omitempty"`
	Agent          githubRunAgentPayload      `json:"agent"`
	Issue          *githubRunIssuePayload     `json:"issue,omitempty"`
	PullRequest    *githubRunPRPayload        `json:"pull_request,omitempty"`
	Trigger        *githubIssueTriggerPayload `json:"trigger,omitempty"`
	ProfileHints   *githubRunProfileHints     `json:"profile_hints,omitempty"`
	Runtime        githubRunRuntimePayload    `json:"runtime"`
}

type githubRunRepositoryPayload struct {
	ID       int64  `json:"id"`
	FullName string `json:"full_name"`
	Name     string `json:"name"`
	Private  bool   `json:"private"`
	Fork     bool   `json:"fork"`
}

type githubInstallationPayload struct {
	ID int64 `json:"id"`
}

type githubActorPayload struct {
	ID    int64  `json:"id"`
	Login string `json:"login"`
}

type githubRunProjectPayload struct {
	ID              string `json:"id"`
	RepositoryID    string `json:"repository_id"`
	ServicesYAML    string `json:"services_yaml"`
	BindingResolved bool   `json:"binding_resolved"`
}

type githubRunAgentPayload struct {
	ID   string `json:"id,omitempty"`
	Key  string `json:"key"`
	Name string `json:"name"`
}

type githubRunIssuePayload struct {
	ID          int64                     `json:"id"`
	Number      int64                     `json:"number"`
	Title       string                    `json:"title"`
	HTMLURL     string                    `json:"html_url"`
	State       string                    `json:"state"`
	User        githubActorPayload        `json:"user"`
	PullRequest *githubPullRequestPayload `json:"pull_request,omitempty"`
}

type githubPullRequestPayload struct {
	URL     string `json:"url"`
	HTMLURL string `json:"html_url"`
}

type githubRunPRPayload struct {
	ID      int64              `json:"id"`
	Number  int64              `json:"number"`
	Title   string             `json:"title"`
	HTMLURL string             `json:"html_url"`
	State   string             `json:"state"`
	Head    githubRunPRRef     `json:"head"`
	Base    githubRunPRRef     `json:"base"`
	User    githubActorPayload `json:"user"`
}

type githubRunPRRef struct {
	Ref string `json:"ref"`
	SHA string `json:"sha,omitempty"`
}

type githubIssueTriggerPayload struct {
	Source string                    `json:"source"`
	Label  string                    `json:"label"`
	Kind   webhookdomain.TriggerKind `json:"kind"`
}

type githubRunProfileHints struct {
	LastRunIssueLabels       []string `json:"last_run_issue_labels,omitempty"`
	LastRunPullRequestLabels []string `json:"last_run_pull_request_labels,omitempty"`
}

type githubRunRuntimePayload struct {
	Mode          string `json:"mode"`
	Source        string `json:"source,omitempty"`
	TargetEnv     string `json:"target_env,omitempty"`
	Namespace     string `json:"namespace,omitempty"`
	BuildRef      string `json:"build_ref,omitempty"`
	DeployOnly    bool   `json:"deploy_only,omitempty"`
	AccessProfile string `json:"access_profile,omitempty"`
}

type githubFlowEventPayload struct {
	Source            string                      `json:"source"`
	DeliveryID        string                      `json:"delivery_id"`
	EventType         string                      `json:"event_type"`
	Action            string                      `json:"action"`
	CorrelationID     string                      `json:"correlation_id"`
	Sender            githubActorPayload          `json:"sender"`
	Repository        githubFlowRepositoryPayload `json:"repository"`
	Inserted          *bool                       `json:"inserted,omitempty"`
	RunID             string                      `json:"run_id,omitempty"`
	Label             string                      `json:"label,omitempty"`
	RunKind           webhookdomain.TriggerKind   `json:"run_kind,omitempty"`
	IssueNumber       int64                       `json:"issue_number,omitempty"`
	Reason            string                      `json:"reason,omitempty"`
	ConflictingLabels []string                    `json:"conflicting_labels,omitempty"`
	SuggestedLabels   []string                    `json:"suggested_labels,omitempty"`
	BindingResolved   *bool                       `json:"binding_resolved,omitempty"`
	Issue             *githubIgnoredIssuePayload  `json:"issue,omitempty"`
}

type githubFlowRepositoryPayload struct {
	ID       int64  `json:"id"`
	FullName string `json:"full_name"`
	Name     string `json:"name"`
}

type githubIgnoredIssuePayload struct {
	ID      int64  `json:"id"`
	Number  int64  `json:"number"`
	Title   string `json:"title"`
	HTMLURL string `json:"html_url"`
	State   string `json:"state"`
}
