package model

// GiteaWebhook represents the webhook payload from Gitea
type GiteaWebhook struct {
	Secret     string     `json:"secret"`
	Ref        string     `json:"ref"`
	Before     string     `json:"before"`
	After      string     `json:"after"`
	CompareURL string     `json:"compare_url"`
	Commits    []Commit   `json:"commits"`
	Repository Repository `json:"repository"`
	Pusher     User       `json:"pusher"`
}

// Commit represents a Git commit in the webhook payload
type Commit struct {
	ID      string `json:"id"`
	Message string `json:"message"`
	URL     string `json:"url"`
	Author  User   `json:"author"`
}

// Repository represents a Git repository in the webhook payload
type Repository struct {
	ID            int    `json:"id"`
	Name          string `json:"name"`
	Owner         User   `json:"owner"`
	FullName      string `json:"full_name"`
	Private       bool   `json:"private"`
	CloneURL      string `json:"clone_url"`
	SSHURL        string `json:"ssh_url"`
	HTMLURL       string `json:"html_url"`
	DefaultBranch string `json:"default_branch"`
}

// User represents a Gitea user in the webhook payload
type User struct {
	ID       int    `json:"id"`
	Login    string `json:"login"`
	FullName string `json:"full_name"`
	Email    string `json:"email,omitempty"`
	Username string `json:"username,omitempty"`
}

// GetBranchName extracts the branch name from the ref
func (w *GiteaWebhook) GetBranchName() string {
	const prefix = "refs/heads/"
	if len(w.Ref) > len(prefix) {
		return w.Ref[len(prefix):]
	}
	return w.Ref
}

// GetEventID generates a unique event ID for the webhook
func (w *GiteaWebhook) GetEventID() string {
	return w.Repository.FullName + "-" + w.After
}
