//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package gitlab

import (
	"net/http"
)

type (
	MergeRequestApprovalSettingsServiceInterface interface {
		GetGroupMergeRequestApprovalSettings(gid any, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error)
		UpdateGroupMergeRequestApprovalSettings(gid any, opt *UpdateGroupMergeRequestApprovalSettingsOptions, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error)
		GetProjectMergeRequestApprovalSettings(pid any, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error)
		UpdateProjectMergeRequestApprovalSettings(pid any, opt *UpdateProjectMergeRequestApprovalSettingsOptions, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error)
	}

	// MergeRequestApprovalSettingsService handles communication with the merge
	// requests approval settings related methods of the GitLab API.
	//
	// GitLab API docs:
	// https://docs.gitlab.com/api/merge_request_approval_settings/
	MergeRequestApprovalSettingsService struct {
		client *Client
	}
)

var _ MergeRequestApprovalSettingsServiceInterface = (*MergeRequestApprovalSettingsService)(nil)

// MergeRequestApprovalSettings represents the merge request approval settings.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/
type MergeRequestApprovalSettings struct {
	AllowAuthorApproval                         MergeRequestApprovalSetting `json:"allow_author_approval"`
	AllowCommitterApproval                      MergeRequestApprovalSetting `json:"allow_committer_approval"`
	AllowOverridesToApproverListPerMergeRequest MergeRequestApprovalSetting `json:"allow_overrides_to_approver_list_per_merge_request"`
	RetainApprovalsOnPush                       MergeRequestApprovalSetting `json:"retain_approvals_on_push"`
	SelectiveCodeOwnerRemovals                  MergeRequestApprovalSetting `json:"selective_code_owner_removals"`
	RequirePasswordToApprove                    MergeRequestApprovalSetting `json:"require_password_to_approve"`
	RequireReauthenticationToApprove            MergeRequestApprovalSetting `json:"require_reauthentication_to_approve"`
}

// MergeRequestApprovalSetting represents an individual merge request approval
// setting.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/
type MergeRequestApprovalSetting struct {
	Value         bool   `json:"value"`
	Locked        bool   `json:"locked"`
	InheritedFrom string `json:"inherited_from"`
}

// GetGroupMergeRequestApprovalSettings gets the merge request approval settings
// of a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#get-group-mr-approval-settings
func (s *MergeRequestApprovalSettingsService) GetGroupMergeRequestApprovalSettings(gid any, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error) {
	return do[*MergeRequestApprovalSettings](s.client,
		withPath("groups/%s/merge_request_approval_setting", GroupID{gid}),
		withRequestOpts(options...),
	)
}

// UpdateProjectMergeRequestApprovalSettingsOptions represents the available
// UpdateProjectMergeRequestApprovalSettings()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#update-project-mr-approval-settings
type UpdateProjectMergeRequestApprovalSettingsOptions struct {
	AllowAuthorApproval                         *bool `url:"allow_author_approval,omitempty" json:"allow_author_approval,omitempty"`
	AllowCommitterApproval                      *bool `url:"allow_committer_approval,omitempty" json:"allow_committer_approval,omitempty"`
	AllowOverridesToApproverListPerMergeRequest *bool `url:"allow_overrides_to_approver_list_per_merge_request,omitempty" json:"allow_overrides_to_approver_list_per_merge_request,omitempty"`
	RetainApprovalsOnPush                       *bool `url:"retain_approvals_on_push,omitempty" json:"retain_approvals_on_push,omitempty"`
	RequireReauthenticationToApprove            *bool `url:"require_reauthentication_to_approve,omitempty" json:"require_reauthentication_to_approve,omitempty"`
	SelectiveCodeOwnerRemovals                  *bool `url:"selective_code_owner_removals,omitempty" json:"selective_code_owner_removals,omitempty"`
}

// UpdateGroupMergeRequestApprovalSettingsOptions represents the available
// UpdateGroupRequestApprovalSettings()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#update-group-mr-approval-settings
type UpdateGroupMergeRequestApprovalSettingsOptions struct {
	AllowAuthorApproval                         *bool `url:"allow_author_approval,omitempty" json:"allow_author_approval,omitempty"`
	AllowCommitterApproval                      *bool `url:"allow_committer_approval,omitempty" json:"allow_committer_approval,omitempty"`
	AllowOverridesToApproverListPerMergeRequest *bool `url:"allow_overrides_to_approver_list_per_merge_request,omitempty" json:"allow_overrides_to_approver_list_per_merge_request,omitempty"`
	RetainApprovalsOnPush                       *bool `url:"retain_approvals_on_push,omitempty" json:"retain_approvals_on_push,omitempty"`
	RequireReauthenticationToApprove            *bool `url:"require_reauthentication_to_approve,omitempty" json:"require_reauthentication_to_approve,omitempty"`
}

// UpdateGroupMergeRequestApprovalSettings updates the merge request approval
// settings of a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#update-group-mr-approval-settings
func (s *MergeRequestApprovalSettingsService) UpdateGroupMergeRequestApprovalSettings(gid any, opt *UpdateGroupMergeRequestApprovalSettingsOptions, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error) {
	return do[*MergeRequestApprovalSettings](s.client,
		withMethod(http.MethodPut),
		withPath("groups/%s/merge_request_approval_setting", GroupID{gid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// GetProjectMergeRequestApprovalSettings gets the merge request approval settings
// of a project.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#get-project-mr-approval-settings
func (s *MergeRequestApprovalSettingsService) GetProjectMergeRequestApprovalSettings(pid any, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error) {
	return do[*MergeRequestApprovalSettings](s.client,
		withPath("projects/%s/merge_request_approval_setting", ProjectID{pid}),
		withRequestOpts(options...),
	)
}

// UpdateProjectMergeRequestApprovalSettings updates the merge request approval
// settings of a project.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_request_approval_settings/#update-project-mr-approval-settings
func (s *MergeRequestApprovalSettingsService) UpdateProjectMergeRequestApprovalSettings(pid any, opt *UpdateProjectMergeRequestApprovalSettingsOptions, options ...RequestOptionFunc) (*MergeRequestApprovalSettings, *Response, error) {
	return do[*MergeRequestApprovalSettings](s.client,
		withMethod(http.MethodPut),
		withPath("projects/%s/merge_request_approval_setting", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}
