//
// Copyright 2021, Patrick Webster
//
// 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"
	"net/url"
)

type (
	InstanceVariablesServiceInterface interface {
		// ListVariables gets a list of all variables for an instance.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/instance_level_ci_variables/#list-all-instance-variables
		ListVariables(opt *ListInstanceVariablesOptions, options ...RequestOptionFunc) ([]*InstanceVariable, *Response, error)
		// GetVariable gets a variable.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/instance_level_ci_variables/#show-instance-variable-details
		GetVariable(key string, options ...RequestOptionFunc) (*InstanceVariable, *Response, error)
		// CreateVariable creates a new instance level CI variable.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/instance_level_ci_variables/#create-instance-variable
		CreateVariable(opt *CreateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error)
		// UpdateVariable updates an existing instance level CI variable.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/instance_level_ci_variables/#update-instance-variable
		UpdateVariable(key string, opt *UpdateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error)
		// RemoveVariable removes an instance level CI variable.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/instance_level_ci_variables/#remove-instance-variable
		RemoveVariable(key string, options ...RequestOptionFunc) (*Response, error)
	}

	// InstanceVariablesService handles communication with the
	// instance level CI variables related methods of the GitLab API.
	//
	// GitLab API docs:
	// https://docs.gitlab.com/api/instance_level_ci_variables/
	InstanceVariablesService struct {
		client *Client
	}
)

var _ InstanceVariablesServiceInterface = (*InstanceVariablesService)(nil)

// InstanceVariable represents a GitLab instance level CI Variable.
//
// GitLab API docs:
// https://docs.gitlab.com/api/instance_level_ci_variables/
type InstanceVariable struct {
	Key          string            `json:"key"`
	Value        string            `json:"value"`
	VariableType VariableTypeValue `json:"variable_type"`
	Protected    bool              `json:"protected"`
	Masked       bool              `json:"masked"`
	Raw          bool              `json:"raw"`
	Description  string            `json:"description"`
}

func (v InstanceVariable) String() string {
	return Stringify(v)
}

// ListInstanceVariablesOptions represents the available options for listing variables
// for an instance.
//
// GitLab API docs:
// https://docs.gitlab.com/api/instance_level_ci_variables/#list-all-instance-variables
type ListInstanceVariablesOptions struct {
	ListOptions
}

func (s *InstanceVariablesService) ListVariables(opt *ListInstanceVariablesOptions, options ...RequestOptionFunc) ([]*InstanceVariable, *Response, error) {
	return do[[]*InstanceVariable](s.client,
		withPath("admin/ci/variables"),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *InstanceVariablesService) GetVariable(key string, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
	return do[*InstanceVariable](s.client,
		withPath("admin/ci/variables/%s", url.PathEscape(key)),
		withRequestOpts(options...),
	)
}

// CreateInstanceVariableOptions represents the available CreateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/api/instance_level_ci_variables/#create-instance-variable
type CreateInstanceVariableOptions struct {
	Key          *string            `url:"key,omitempty" json:"key,omitempty"`
	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
	Description  *string            `url:"description,omitempty" json:"description,omitempty"`
	Masked       *bool              `url:"masked,omitempty" json:"masked,omitempty"`
	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
	Raw          *bool              `url:"raw,omitempty" json:"raw,omitempty"`
	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
}

func (s *InstanceVariablesService) CreateVariable(opt *CreateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
	return do[*InstanceVariable](s.client,
		withMethod(http.MethodPost),
		withPath("admin/ci/variables"),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// UpdateInstanceVariableOptions represents the available UpdateVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/api/instance_level_ci_variables/#update-instance-variable
type UpdateInstanceVariableOptions struct {
	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
	Description  *string            `url:"description,omitempty" json:"description,omitempty"`
	Masked       *bool              `url:"masked,omitempty" json:"masked,omitempty"`
	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
	Raw          *bool              `url:"raw,omitempty" json:"raw,omitempty"`
	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
}

func (s *InstanceVariablesService) UpdateVariable(key string, opt *UpdateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
	return do[*InstanceVariable](s.client,
		withMethod(http.MethodPut),
		withPath("admin/ci/variables/%s", url.PathEscape(key)),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *InstanceVariablesService) RemoveVariable(key string, options ...RequestOptionFunc) (*Response, error) {
	_, resp, err := do[none](s.client,
		withMethod(http.MethodDelete),
		withPath("admin/ci/variables/%s", url.PathEscape(key)),
		withRequestOpts(options...),
	)
	return resp, err
}
