package template

// builtinTemplates returns the map of built-in template names to their source strings.
func builtinTemplates() map[string]string {
	return map[string]string{
		"env":            envTemplate,
		"docker-compose": dockerComposeTemplate,
		"k8s-secret":     k8sSecretTemplate,
		"github-actions": githubActionsTemplate,
		"terraform":      terraformTemplate,
	}
}

// envTemplate generates a shell environment file.
const envTemplate = `# Generated by OpenPass
{{- range $key, $ref := .Refs }}
export {{ $key }}={{ index $.Values $key }}
{{- end }}
`

// dockerComposeTemplate generates a docker-compose.yml with secret references.
const dockerComposeTemplate = `version: "3.8"
services:
  app:
    environment:
{{- range $key, $ref := .Refs }}
      - {{ $key }}={{ index $.Values $key }}
{{- end }}
    secrets:
{{- range $key, $ref := .Refs }}
      - {{ $key | lower }}_secret
{{- end }}
secrets:
{{- range $key, $ref := .Refs }}
  {{ $key | lower }}_secret:
    file: ./secrets/{{ $key | lower }}.txt
{{- end }}
`

// k8sSecretTemplate generates a Kubernetes Secret manifest.
// Values are base64-encoded as required by Kubernetes.
// #nosec G101 -- this is a Go template string, not hardcoded credentials.
const k8sSecretTemplate = `apiVersion: v1
kind: Secret
metadata:
  name: {{ .Name }}
type: Opaque
data:
{{- range $key, $ref := .Refs }}
  {{ $key }}: {{ index $.Values $key | b64enc }}
{{- end }}
`

// githubActionsTemplate generates a GitHub Actions workflow snippet for secrets.
const githubActionsTemplate = `name: Deploy {{ .Name }}

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Configure secrets
        env:
{{- range $key, $ref := .Refs }}
          {{ $key }}: {{ index $.Values $key }}
{{- end }}
        run: |
          echo "Secrets configured"
`

// terraformTemplate generates a Terraform variables file.
const terraformTemplate = `# Terraform variables for {{ .Name }}
# Generated by OpenPass
{{- range $key, $ref := .Refs }}
variable "{{ $key | lower }}" {
  type      = string
  sensitive = true
  default   = {{ index $.Values $key | tojson }}
}
{{- end }}
`
