/*
Copyright 2025 The Kubernetes Authors.

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 charttemplates

import (
	"path/filepath"

	"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
	"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha/internal/common"
)

var _ machinery.Template = &Notes{}

// Notes scaffolds the NOTES.txt file for Helm charts
type Notes struct {
	machinery.TemplateMixin
	machinery.ProjectNameMixin

	// OutputDir specifies the output directory for the chart
	OutputDir string
	// Force if true allows overwriting the scaffolded file
	Force bool
}

// SetTemplateDefaults sets the default template configuration
func (f *Notes) SetTemplateDefaults() error {
	if f.Path == "" {
		outputDir := f.OutputDir
		if outputDir == "" {
			outputDir = common.DefaultOutputDir
		}
		f.Path = filepath.Join(outputDir, "chart", "templates", "NOTES.txt")
	}

	f.TemplateBody = f.generateNotesTemplate()

	if f.Force {
		f.IfExistsAction = machinery.OverwriteFile
	} else {
		f.IfExistsAction = machinery.SkipFile
	}

	return nil
}

// generateNotesTemplate creates the NOTES.txt content with project-specific information
func (f *Notes) generateNotesTemplate() string {
	return notesTemplate
}

const notesTemplate = `Thank you for installing {{` + "`" + `{{ .Chart.Name }}` + "`" + `}}.

Your release is named {{` + "`" + `{{ .Release.Name }}` + "`" + `}}.

The controller and CRDs have been installed in namespace {{` + "`" + `{{ .Release.Namespace }}` + "`" + `}}.

To verify the installation:

  kubectl get pods -n {{` + "`" + `{{ .Release.Namespace }}` + "`" + `}}
  kubectl get customresourcedefinitions

To learn more about the release, try:

  $ helm status {{` + "`" + `{{ .Release.Name }}` + "`" + `}} -n {{` + "`" + `{{ .Release.Namespace }}` + "`" + `}}
  $ helm get all {{` + "`" + `{{ .Release.Name }}` + "`" + `}} -n {{` + "`" + `{{ .Release.Namespace }}` + "`" + `}}
`
