package utils

import (
	_ "github.com/mattn/go-sqlite3"
)

const (
	SERVERLESS_EXPENSIVE_SQL_EMAIL_TEMPLATE_ID = "d-cbb2bcdaabe44c3ab4c9b0d69122271d"
)

type Query struct {
	Query   string
	Count   string
	TotalRU string
	MeanRU  string
	P99     float64
}

type ResourceUsed struct {
	StorageUsed       string
	RUUsed            string
	FreeQuotaUsedRate string /* for free cluster*/
	Cost              string /* dollar, for scalable cluster */
	FreeCredit        string
	SpendingLimit     string /* dollar, for scalable cluster */
}

type Cluster struct {
	ClusterName       string
	ClusterId         string
	PlanType          string /* Free | Scalable*/
	Region            string
	StatementTemplate []Query
	SlowQueries       []Query
	Resource          ResourceUsed
}

type ExpensiveSQLEmailReport struct {
	Clusters []Cluster
}

type ClusterCost struct {
	ClusterId uint64
	Cost      int64
}

const ExpensiveSQLEmailReportHTML = `{{range $id, $element := .Clusters}}
    <h2>{{addOne $id}}. {{.ClusterName}}</h2>
    <h3>Queries that cost most RUs [<a href="https://tidbcloud.com/console/clusters/{{.ClusterId}}/diagnosis/sql-statement?sort=-sum_ru">View</a>]</h3>
    {{range .StatementTemplate}}
    <table class="custom-border">
        <tr>
            <tr><td>{{.Query}}</td></tr>
            <tr><td>Count: {{.Count}}  |  Total RU: {{.TotalRU}}  |  Mean RU: {{.MeanRU}}</td></tr>
        </tr>
    </table>
	<div class="spacer-row"></div>
    {{end}}
    <h3>Slowest Queries [<a href="https://tidbcloud.com/console/clusters/{{.ClusterId}}/diagnosis/slow-query?desc=true&sort=query_time">View</a>]</h3>
    {{range .SlowQueries}}
    <table class="custom-border">
        <tr>
            <tr><td>{{.Query}}</td></tr>
            <tr><td>Count: {{.Count}} | P99: {{.P99}}s</td></tr>
        </tr>
    </table>
	<div class="spacer-row"></div>
    {{end}}
    <h3>Resource Utilization [<a href="https://tidbcloud.com/console/clusters/{{.ClusterId}}/overview">View</a>]</h3>
    {{if eq .PlanType "Free"}}
    <p><a href="https://tidbcloud.com/console/clusters/{{.ClusterId}}/edit-spendlimit">Upgrade to Scalable Cluster</a> to get more storage and RU to your cluster.</p>
    <table class="custom-border">
        <tr>
            <th>Cluster Plan</th>
            <th>Region</th>
            <th>Storage Used</th>
            <th>RU Used</th>
            <th>Free Quota Used</th>
        </tr>
        <tr>
            <td>{{.PlanType}}</td>
            <td>{{.Region}}</td>
            <td>{{.Resource.StorageUsed}}</td>
            <td>{{.Resource.RUUsed}}</td>
            <td>{{.Resource.FreeQuotaUsedRate}}</td>
        </tr>
    </table>
    <div class="spacer-row"></div>
    {{else}}
    <p><a href="https://tidbcloud.com/console/clusters/{{.ClusterId}}/edit-spendlimit">Update your Spending limit</a> to get more storage and RU to your cluster.</p>
    <table class="custom-border">
        <tr>
            <th>Cluster Plan</th>
            <th>Region</th>
            <th>Storage Used</th>
            <th>RU Used</th>
            <th>Cost / Spending Limit</th>
        </tr>
        <tr>
            <td>{{.PlanType}}</td>
            <td>{{.Region}}</td>
            <td>{{.Resource.StorageUsed}}</td>
            <td>{{.Resource.RUUsed}}</td>
            <td>${{.Resource.Cost}} / ${{.Resource.SpendingLimit}}</td>
        </tr>
    </table>
    {{end}}
    <div class="spacer-row"></div>
    {{end}}
`
