package release

import (
	"github.com/tidbcloud/devops/api"
	relent "github.com/tidbcloud/devops/entity/release"
)

type DefaultChangeReq struct {
	ReleaseID       uint64 `form:"release_id" binding:"required"`
	ApplicationName string `form:"application_name" binding:"required"`
}

type DefaultChangeResp struct {
	api.BaseResp

	WindowStartAt int64 `json:"window_start_at"`
	WindowEndAt   int64 `json:"window_end_at"`
}

type ListChangeResp struct {
	api.BaseResp

	Changes []*relent.Change `json:"items"`
}

type Deployment struct {
	Order             int                       `json:"order" binding:"required"`
	Regions           []relent.RegionName       `json:"regions" binding:"required"`
	DeployMethod      relent.ChangeDeployMethod `json:"deploy_method" binding:"required"`
	DeployCDLink      string                    `json:"deploy_cd_link"`
	DeployDescription string                    `json:"deploy_description"`
	Repo              string                    `json:"repo"`
	Branch            string                    `json:"branch"`
	Commit            string                    `json:"commit"`
}

type CreateChangeReq struct {
	Name              string              `json:"name" binding:"required"`
	WindowStartAt     int64               `json:"window_start_at" binding:"required"`
	WindowEndAt       int64               `json:"window_end_at" binding:"required"`
	Tier              []relent.ChangeTier `json:"tier" binding:"required"`
	RiskLevel         relent.RiskLevel    `json:"risk_level" binding:"required"`
	NeedOutage        bool                `json:"need_outage"`
	Note              string              `json:"note"`
	RollbackPlan      string              `json:"rollback_plan"`
	StagingTestReport string              `json:"staging_test_report"`
	Type              relent.ChangeType   `json:"type"`
	ChangeItemNames   []string            `json:"change_item_names"`
	Deployments       []Deployment        `json:"deployments"`

	ReleaseID       uint64 `json:"release_id" binding:"required"`
	ApplicationName string `json:"application_name" binding:"required"`

	IsDraft   bool   `json:"is_draft"`
	Applicant string `json:"applicant"`
}

type CreateChangeResp struct {
	api.BaseResp

	ChangeID *uint64 `json:"change_id"`
}

type GetChangeReq struct {
	WithChangeDetail bool `form:"with_detail"`
	WithChangeItems  bool `form:"with_change_items"`
	WithRelease      bool `form:"with_release"`
	WithApplicant    bool `form:"with_applicant"`
	WithApplication  bool `form:"with_application"`
	WithTickets      bool `form:"with_tickets"`
}

type GetChangeResp struct {
	api.BaseResp

	Change         *relent.Change          `json:"change"`
	ReleaseTickets []*relent.ReleaseTicket `json:"release_tickets"`
}

// UpdateChangeReq is the request to update a change.
// Update a field if it is not nil.
type UpdateChangeReq struct {
	Name              *string              `json:"name,omitempty"`
	WindowStartAt     *int64               `json:"window_start_at,omitempty"` //
	WindowEndAt       *int64               `json:"window_end_at,omitempty"`   //
	Regions           *[]relent.RegionName `json:"regions,omitempty"`
	Tier              *[]relent.ChangeTier `json:"tier,omitempty"`
	RiskLevel         *relent.RiskLevel    `json:"risk_level,omitempty"`
	NeedOutage        *bool                `json:"need_outage,omitempty"`
	Note              *string              `json:"note,omitempty"`
	RollbackPlan      *string              `json:"rollback_plan,omitempty"`
	StagingTestReport *string              `json:"staging_test_report"`
	Type              *relent.ChangeType   `json:"type"`
	ChangeItemNames   *[]string            `json:"change_item_names,omitempty"`

	Deployments []Deployment `json:"deployments,omitempty"`
}

type ExecuteDeploymentReq struct {
	Force bool `json:"force"`
}

type VerifyDeploymentReq struct {
	Note string `json:"note"`
}

type IsDeploymentReadyResp struct {
	api.BaseResp

	IsReady bool     `json:"is_ready"`
	Reasons []string `json:"reasons"`
}

type CloseChangeReq struct {
	Result  relent.ChangeResult `json:"result" binding:"required"`
	Summary string              `json:"summary" binding:"required"`
}
