// Copyright 2016 The Gogs Authors. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gitea

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"time"
)

// PayloadUser represents the author or committer of a commit
type PayloadUser struct {
	// Full name of the commit author
	Name     string `json:"name"`
	Email    string `json:"email"`
	UserName string `json:"username"`
}

// PayloadCommit represents a commit
type PayloadCommit struct {
	// sha1 hash of the commit
	ID           string                     `json:"id"`
	Message      string                     `json:"message"`
	URL          string                     `json:"url"`
	Author       *PayloadUser               `json:"author"`
	Committer    *PayloadUser               `json:"committer"`
	Verification *PayloadCommitVerification `json:"verification"`
	Timestamp    time.Time                  `json:"timestamp"`
	Added        []string                   `json:"added"`
	Removed      []string                   `json:"removed"`
	Modified     []string                   `json:"modified"`
}

// PayloadCommitVerification represents the GPG verification of a commit
type PayloadCommitVerification struct {
	Verified  bool   `json:"verified"`
	Reason    string `json:"reason"`
	Signature string `json:"signature"`
	Payload   string `json:"payload"`
}

// Branch represents a repository branch
type Branch struct {
	Name                          string         `json:"name"`
	Commit                        *PayloadCommit `json:"commit"`
	Protected                     bool           `json:"protected"`
	RequiredApprovals             int64          `json:"required_approvals"`
	EnableStatusCheck             bool           `json:"enable_status_check"`
	StatusCheckContexts           []string       `json:"status_check_contexts"`
	UserCanPush                   bool           `json:"user_can_push"`
	UserCanMerge                  bool           `json:"user_can_merge"`
	EffectiveBranchProtectionName string         `json:"effective_branch_protection_name"`
}

// ListRepoBranchesOptions options for listing a repository's branches
type ListRepoBranchesOptions struct {
	ListOptions
}

// ListRepoBranches list all the branches of one repository
func (c *RepositoriesService) ListRepoBranches(ctx context.Context, user, repo string, opt ListRepoBranchesOptions) ([]*Branch, *Response, error) {
	if err := escapeValidatePathSegments(&user, &repo); err != nil {
		return nil, nil, err
	}
	opt.setDefaults()
	branches := make([]*Branch, 0, opt.PageSize)
	resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &branches)
	return branches, resp, err
}

// GetRepoBranch get one branch's information of one repository
func (c *RepositoriesService) GetRepoBranch(ctx context.Context, user, repo, branch string) (*Branch, *Response, error) {
	if err := escapeValidatePathSegments(&user, &repo); err != nil {
		return nil, nil, err
	}
	branch = pathEscapeSegments(branch)
	b := new(Branch)
	resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b)
	if err != nil {
		return nil, resp, err
	}
	return b, resp, nil
}

// DeleteRepoBranch delete a branch in a repository
func (c *RepositoriesService) DeleteRepoBranch(ctx context.Context, user, repo, branch string) (bool, *Response, error) {
	if err := escapeValidatePathSegments(&user, &repo); err != nil {
		return false, nil, err
	}
	branch = pathEscapeSegments(branch)
	if err := c.checkServerVersionGreaterThanOrEqual(ctx, version1_12_0); err != nil {
		return false, nil, err
	}
	status, resp, err := c.getStatusCode(ctx, "DELETE", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil)
	if err != nil {
		return false, resp, err
	}
	return status == 204, resp, nil
}

type UpdateRepoBranchOption struct {
	Name string `json:"name"`
}

func (opt UpdateRepoBranchOption) Validate() error {
	if len(opt.Name) == 0 {
		return errors.New("empty Name field")
	}

	return nil
}

// RenameRepoBranchOption renames a repository branch.
type RenameRepoBranchOption struct {
	Name string `json:"name"`
}

// Validate checks whether the rename payload is valid.
func (opt RenameRepoBranchOption) Validate() error {
	if len(opt.Name) == 0 {
		return errors.New("empty Name field")
	}
	return nil
}

// UpdateRepoBranchRefOption updates a branch ref to point to a new commit.
type UpdateRepoBranchRefOption struct {
	NewCommitID string `json:"new_commit_id"`
	OldCommitID string `json:"old_commit_id"`
	Force       bool   `json:"force"`
}

// Validate checks whether the branch update payload is valid.
func (opt UpdateRepoBranchRefOption) Validate() error {
	if len(opt.NewCommitID) == 0 {
		return errors.New("empty NewCommitID field")
	}
	return nil
}

// RenameRepoBranch renames a branch in a repository.
func (c *RepositoriesService) RenameRepoBranch(ctx context.Context, user, repo, branch string, opt RenameRepoBranchOption) (successful bool, resp *Response, err error) {
	if err := escapeValidatePathSegments(&user, &repo); err != nil {
		return false, nil, err
	}
	branch = pathEscapeSegments(branch)
	if err := c.checkServerVersionGreaterThanOrEqual(ctx, version1_24_0); err != nil {
		return false, nil, err
	}
	if err := opt.Validate(); err != nil {
		return false, nil, err
	}
	body, err := json.Marshal(&opt)
	if err != nil {
		return false, nil, err
	}
	status, resp, err := c.getStatusCode(ctx, "PATCH", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), jsonHeader, bytes.NewReader(body))
	return status == 204, resp, err
}

// UpdateRepoBranchRef updates the commit a branch points to.
// Available on Gitea main/nightly; first released version not assigned yet.
// Upstream: gitea/gitea#35951 (a440116a16c42956f21031bea8422ffbb003c732).
func (c *RepositoriesService) UpdateRepoBranchRef(ctx context.Context, user, repo, branch string, opt UpdateRepoBranchRefOption) (successful bool, resp *Response, err error) {
	if err := escapeValidatePathSegments(&user, &repo); err != nil {
		return false, nil, err
	}
	branch = pathEscapeSegments(branch)
	if err := opt.Validate(); err != nil {
		return false, nil, err
	}
	body, err := json.Marshal(&opt)
	if err != nil {
		return false, nil, err
	}
	status, resp, err := c.getStatusCode(ctx, "PUT", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), jsonHeader, bytes.NewReader(body))
	return status == 204, resp, err
}

// CreateBranchOption options when creating a branch in a repository
type CreateBranchOption struct {
	// Name of the branch to create
	BranchName string `json:"new_branch_name"`
	// Name of the old branch to create from (optional)
	OldBranchName string `json:"old_branch_name"`
}

// Validate the CreateBranchOption struct
func (opt CreateBranchOption) Validate() error {
	if len(opt.BranchName) == 0 {
		return fmt.Errorf("BranchName is empty")
	}
	if len(opt.BranchName) > 100 {
		return fmt.Errorf("BranchName to long")
	}
	if len(opt.OldBranchName) > 100 {
		return fmt.Errorf("OldBranchName to long")
	}
	return nil
}

// CreateBranch creates a branch for a user's repository
func (c *RepositoriesService) CreateBranch(ctx context.Context, owner, repo string, opt CreateBranchOption) (*Branch, *Response, error) {
	if err := escapeValidatePathSegments(&owner, &repo); err != nil {
		return nil, nil, err
	}
	if err := c.checkServerVersionGreaterThanOrEqual(ctx, version1_13_0); err != nil {
		return nil, nil, err
	}
	if err := opt.Validate(); err != nil {
		return nil, nil, err
	}
	body, err := json.Marshal(&opt)
	if err != nil {
		return nil, nil, err
	}
	branch := new(Branch)
	resp, err := c.getParsedResponse(ctx, "POST", fmt.Sprintf("/repos/%s/%s/branches", owner, repo), jsonHeader, bytes.NewReader(body), branch)
	return branch, resp, err
}
