package output

var HTMLTemplate = `
{{define "style"}}
body {
  margin: 0;
  padding: 0.5rem 1rem;
  font-family: sans-serif;
  color: #111827;
}

a {
  color: #3b82f6;
}

.metadata {
  margin-bottom: 1.5rem;
}

.metadata ul {
  list-style-type: none;
  padding: 0;
}

.metadata ul li {
  margin-bottom: 0.5rem;
}

.metadata .label {
  display: inline-block;
  font-weight: bold;
  margin-right: 0.5rem;
  width: 8rem;
}

.warnings {
  margin-top: 1.5rem;
}

table {
  border: 1px solid #6b7280;
  border-collapse: collapse;
}

th, td {
  padding: 0.25rem 0.5rem;
  text-align: left;
}

td.name {
  max-width: 32rem;
}

td.monthly-quantity, td.price, td.hourly-cost, td.monthly-cost {
  text-align: right;
}

tr.group {
  background-color: #e0e7ff;
}

tr.resource {
  background-color: #e5e7eb;
}

tr.resource.top-level {
  background-color: #6b7280;
  color: #ffffff;
}

tr.tags {
  background-color: #6b7280;
  color: #ffffff;
  font-size: 0.75rem;
}

tr.tags td {
  padding-top: 0;
}

tr.total {
  background-color: #d8dce2;
  font-weight: bold;
}

table.overall-total tr.total {
  background-color: #ffdfb9;
  font-weight: bold;
}

table.overall-total tr.total td {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

.arrow {
  color: #96a0b5;
}

.usage-cost {
  color: #6b7280;
}

@media screen and (max-width: 1024px) {
  table.breakdown, table.overall-total {
    min-width: auto;
  }
}
table.breakdown, table.overall-total {
  min-width: 946px;
}

table.overall-total {
  margin-top: 1rem;
}

{{end}}

{{define "faviconBase64"}}
 
{{end}}

{{define "emptyTableRows"}}
  {{if contains .Fields "monthlyQuantity"}}
    <td class="monthly-quantity"></td>
  {{end}}
  {{if contains .Fields "unit"}}
    <td class="unit"></td>
  {{end}}
  {{if contains .Fields "price"}}
    <td class="price"></td>
  {{end}}
  {{if contains .Fields "hourlyCost"}}
    <td class="hourly-cost"></td>
  {{end}}
  {{if contains .Fields "monthlyCost"}}
    <td class="monthly-cost"></td>
  {{end}}
{{end}}

{{define "resourceRows"}}
  {{$fields := .Fields}}
  {{- $filteredCostComponents := filterZeroValComponents .Resource.CostComponents .Resource.Name}}
  {{- $filteredSubResources := filterZeroValResources .Resource.SubResources .Resource.Name}}
  {{- if hasCost $filteredCostComponents $filteredSubResources .Resource.Name }}
  <tr class="resource{{if eq .Indent 0}} top-level{{end}}">
    <td class="name">
      {{if gt .Indent 1}}{{repeat (int (add .Indent -1)) "&nbsp;&nbsp;&nbsp;&nbsp;" | safeHTML}}{{end}}
      {{if gt .Indent 0}}<span class="arrow">&#8627;</span>{{end}}
      {{.Resource.Name}}
    </td>
    {{template "emptyTableRows" dict "Fields" $fields}}
  </tr>
  {{ if .Resource.Tags}}
    <tr class="tags">
      <td class="name">
        {{$tags := list}}
        {{range $k, $v := .Resource.Tags}}
          {{$t := list $k "=" $v | join "" }}
          {{$tags = append $tags $t}}
        {{end}}
        <span class="label">Tags:</span>
        <span>{{$tags | join ", "}}</span>
      </td>
      {{template "emptyTableRows" dict "Fields" $fields}}
    </tr>
  {{end}}
  {{$ident := add .Indent 1}}
  {{range $filteredCostComponents}}
    {{template "costComponentRow" dict "CostComponent" . "Fields" $fields "Indent" $ident}}
  {{end}}
  {{range $filteredSubResources}}
    {{template "resourceRows" dict "Resource" . "Fields" $fields "Indent" $ident}}
  {{end}}
  {{- end}}
{{end}}

{{define "costComponentRow"}}
  <tr class="cost-component">
    <td class="name">
      {{if gt .Indent 1}}{{repeat (int (add .Indent -1)) "&nbsp;&nbsp;&nbsp;&nbsp;" | safeHTML}}{{end}}
      {{if gt .Indent 0}}<span class="arrow">&#8627;</span>{{end}}
      {{.CostComponent.Name}}
    </td>
    {{if .CostComponent.MonthlyCost}}
      {{if contains .Fields "monthlyQuantity"}}
        <td class="monthly-quantity">{{.CostComponent.MonthlyQuantity | formatQuantity }}</td>
      {{end}}
      {{if contains .Fields "unit"}}
        <td class="unit">{{.CostComponent.Unit}}</td>
      {{end}}
      {{if contains .Fields "price"}}
        <td class="price">{{.CostComponent.Price | formatPrice }}</td>
      {{end}}
      {{if contains .Fields "hourlyCost"}}
        <td class="hourly-cost">{{.CostComponent.HourlyCost | formatCost2DP}}</td>
      {{end}}
      {{if contains .Fields "monthlyCost"}}
        <td class="monthly-cost">{{.CostComponent.MonthlyCost | formatCost2DP}}</td>
      {{end}}
    {{else}}
      <td colspan="{{len .Fields}}" class="usage-cost">Cost depends on usage: {{.CostComponent.Price | formatPrice}} per {{.CostComponent.Unit}}</td>
    {{end}}
  </tr>
{{end}}

{{define "tableHeaders"}}
  <th class="name">Name</th>
  {{if contains .Fields "monthlyQuantity"}}
    <td class="monthly-quantity">Monthly Qty</td>
  {{end}}
  {{if contains .Fields "unit"}}
    <td class="unit">Unit</td>
  {{end}}
  {{if contains .Fields "price"}}
    <td class="price">{{ "Price" | formatTitleWithCurrency }}</td>
  {{end}}
  {{if contains .Fields "hourlyCost"}}
    <td class="hourly-cost">{{ "Hourly Cost" | formatTitleWithCurrency }}</td>
  {{end}}
  {{if contains .Fields "monthlyCost"}}
    <td class="monthly-cost">{{ "Monthly Cost" | formatTitleWithCurrency }}</td>
  {{end}}
{{end}}

{{define "projectBlock"}}
  {{$fields := .Options.Fields}}
  <p class="project-name">Project: {{.Project | projectLabel}}</p>
  <table class="breakdown">
    <thead>
      {{template "tableHeaders" dict "Fields" $fields}}
    </thead>
    <tbody>
      {{range .Resources}}
        {{template "resourceRows" dict "Resource" . "Fields" $fields "Indent" 0}}
      {{end}}
      <tr class="total">
        <td class="name" colspan="{{len .Options.Fields}}">Project total</td>
        <td class="monthly-cost">{{.Project.Breakdown.TotalMonthlyCost | formatCost2DP}}</td>
      </tr>
    </tbody>
  </table>
{{end}}

<!doctype html>
<html>
  <head>
    <title>Infracost cost report</title>
    <style>
      {{template "style"}}
    </style>
    <link id="favicon" rel="shortcut icon" type="image/png" href="data:image/png;base64,{{template "faviconBase64"}}">
  </head>

  <body>
    <div class="metadata">
      <ul>
        <li>
          <span class="label">Generated by:</span>
          <span class="value"><a href="https://infracost.io" target="_blank">Infracost</a></span>
        </li>
        <li>
          <span class="label">Time generated:</span>
          <span class="value">{{.Root.TimeGenerated | date "2006-01-02 15:04:05 MST"}}</span>
        </li>
      </ul>
    </div>

    {{$options := .Options}}

    {{range .Root.Projects}}
      {{$resources := .Breakdown.Resources}}
      {{template "projectBlock" dict "Project" . "Options" $options "Resources" $resources "Indent" 0}}
    {{end}}

    <table class="overall-total">
      <tbody>
        <tr class="total">
          <td class="name" colspan="{{len .Options.Fields}}">{{ "Overall total" | formatTitleWithCurrency }}</td>
          <td class="monthly-cost">{{.Root.TotalMonthlyCost | formatCost2DP}}</td>
        </tr>
      </tbody>
    </table>

    <div class="warnings">
      <p>{{.SummaryMessage | stripColor | replaceNewLines}}</p>
    </div>
  </body>
</html>`

var CommentMarkdownWithHTMLTemplate = `
{{- define "summaryRow"}}
    <tr>
      <td>{{ truncateMiddle .Name 64 "..." }}</td>
      <td align="right">{{ formatCost .PastCost }}</td>
      <td align="right">{{ formatCost .Cost }}</td>
      <td>{{ formatCostChange .PastCost .Cost }}</td>
    </tr>
{{- end}}
💰 Infracost estimate: **{{ formatCostChangeSentence .Root.Currency .Root.PastTotalMonthlyCost .Root.TotalMonthlyCost true }}**
<table>
  <thead>
    <td>Project</td>
    <td>Previous</td>
    <td>New</td>
    <td>Diff</td>
  </thead>
{{- if gt (len .Root.Projects) 1  }}
  <tbody>
  {{- range .Root.Projects }}
    {{- if hasDiff . }}
      {{- template "summaryRow" dict "Name" .Name "PastCost" .PastBreakdown.TotalMonthlyCost "Cost" .Breakdown.TotalMonthlyCost  }}
    {{- end }}
  {{- end }}
  {{- template "summaryRow" dict "Name" "All projects" "PastCost" .Root.PastTotalMonthlyCost "Cost" .Root.TotalMonthlyCost  }}
  </tbody>
</table>

  {{- if eq .SkippedProjectCount 1 }}

1 project has no cost estimate changes.
  {{- else if gt .SkippedProjectCount  0 }}

{{ .SkippedProjectCount }} projects have no cost estimate changes.
  {{- end }}
{{- else }}
  <tbody>
  {{- range .Root.Projects }}
    {{- template "summaryRow" dict "Name" .Name "PastCost" .PastBreakdown.TotalMonthlyCost "Cost" .Breakdown.TotalMonthlyCost  }}
  {{- end }}
  </tbody>
</table>
{{- end }}

<details>
<summary><strong>Infracost output</strong></summary>

` + "```" /* can't escape backticks */ + `
{{ .DiffOutput }}
` + "```" /* can't escape backticks */ + `
</details>
{{- if .Options.PolicyChecks.Enabled }}
	{{- if gt (len .Options.PolicyChecks.Failures) 0 }}
		<details>
			<summary><strong>❌ Policy checks failed</strong></summary>
				{{ range $v, $f := .Options.PolicyChecks.Failures}}
> {{ $f }}
				{{- end}}
		</details>
	{{ else }}
		<details>
			<summary><strong>✅ Policy checks passed</strong></summary>
			{{ range $v, $f := .Options.PolicyChecks.Passed}}
> {{ $f }}
			{{- end}}
		</details>
	{{- end }}
{{- end }}
{{- if .MarkdownOptions.WillUpdate }}

This comment will be updated when the cost estimate changes.
{{- end}}
{{- if .MarkdownOptions.WillReplace }}

This comment will be replaced when the cost estimate changes
{{- end}}
{{- if .MarkdownOptions.IncludeFeedbackLink }}

<sub>
  Is this comment useful? <a href="https://www.infracost.io/feedback/submit/?value=yes" rel="noopener noreferrer" target="_blank">Yes</a>, <a href="https://www.infracost.io/feedback/submit/?value=no" rel="noopener noreferrer" target="_blank">No</a>
</sub>
{{- end}}
`

var CommentMarkdownTemplate = `
{{- define "summaryRow"}}
| {{ truncateMiddle .Name 64 "..." }} | {{ formatCost .PastCost }} | {{ formatCost .Cost }} | {{ formatCostChange .PastCost .Cost }} |
{{- end }}
{{- define "totalRow"}}
| **{{ truncateMiddle .Name 64 "..." }}** | **{{ formatCost .PastCost }}** | **{{ formatCost .Cost }}** | **{{ formatCostChange .PastCost .Cost }}** |
{{- end }}
## Infracost estimate: **{{ formatCostChangeSentence .Root.Currency .Root.PastTotalMonthlyCost .Root.TotalMonthlyCost false }}**

| **Project** | **Previous** | **New** | **Diff** |
| ----------- | -----------: | ------: | -------- |

{{- if gt (len .Root.Projects) 1  }}
  {{- range .Root.Projects }}
    {{- if hasDiff . }}
      {{- template "summaryRow" dict "Name" .Name "PastCost" .PastBreakdown.TotalMonthlyCost "Cost" .Breakdown.TotalMonthlyCost  }}
    {{- end }}
  {{- end }}
  {{- template "totalRow" dict "Name" "All projects" "PastCost" .Root.PastTotalMonthlyCost "Cost" .Root.TotalMonthlyCost  }}

  {{- if eq .SkippedProjectCount 1 }}

1 project has no cost estimate changes.
  {{- else if gt .SkippedProjectCount 0 }}

{{ .SkippedProjectCount }} projects have no cost estimate changes.
  {{- end }}
{{- else }}
  {{- range .Root.Projects }}
    {{- template "summaryRow" dict "Name" .Name "PastCost" .PastBreakdown.TotalMonthlyCost "Cost" .Breakdown.TotalMonthlyCost  }}
  {{- end }}
{{- end }}

**Infracost output:**

` + "```" /* can't escape backticks */ + `
{{ .DiffOutput }}
` + "```" /* can't escape backticks */ + `

{{- if .Options.PolicyChecks.Enabled }}
	{{- if gt (len .Options.PolicyChecks.Failures) 0 }}
**Policy checks failed:**
` + "```" /* can't escape backticks */ + `
				{{ range $v, $f := .Options.PolicyChecks.Failures}}
> {{ $f }}
				{{- end}}
` + "```" /* can't escape backticks */ + `
	{{ else }}
**Policy checks passed:**
` + "```" /* can't escape backticks */ + `
			{{ range $v, $f := .Options.PolicyChecks.Passed}}
> {{ $f }}
			{{- end}}
` + "```" /* can't escape backticks */ + `
	{{- end }}
{{- end }}
{{- if .MarkdownOptions.WillUpdate }}

This comment will be updated when the cost estimate changes.
{{- end }}
{{- if .MarkdownOptions.WillReplace }}

This comment will be replaced when the cost estimate changes
{{- end }}
{{- if .MarkdownOptions.IncludeFeedbackLink }}

Is this comment useful? [Yes](https://www.infracost.io/feedback/submit/?value=yes), [No](https://www.infracost.io/feedback/submit/?value=no)
{{- end }}
`
