// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.25.0
// source: certificate.sql

package database

import (
	"context"
	"database/sql"
	"time"
)

const addCertificate = `-- name: AddCertificate :exec
INSERT INTO certificates (owner, dns, not_after, updated_at)
VALUES (?, ?, ?, ?)
`

type AddCertificateParams struct {
	Owner     string        `json:"owner"`
	Dns       sql.NullInt64 `json:"dns"`
	NotAfter  time.Time     `json:"not_after"`
	UpdatedAt time.Time     `json:"updated_at"`
}

func (q *Queries) AddCertificate(ctx context.Context, arg AddCertificateParams) error {
	_, err := q.db.ExecContext(ctx, addCertificate,
		arg.Owner,
		arg.Dns,
		arg.NotAfter,
		arg.UpdatedAt,
	)
	return err
}

const addTempCertificate = `-- name: AddTempCertificate :exec
INSERT INTO certificates (owner, dns, active, updated_at, temp_parent)
VALUES (?, NULL, 1, ?, ?)
`

type AddTempCertificateParams struct {
	Owner      string        `json:"owner"`
	UpdatedAt  time.Time     `json:"updated_at"`
	TempParent sql.NullInt64 `json:"temp_parent"`
}

func (q *Queries) AddTempCertificate(ctx context.Context, arg AddTempCertificateParams) error {
	_, err := q.db.ExecContext(ctx, addTempCertificate, arg.Owner, arg.UpdatedAt, arg.TempParent)
	return err
}

const checkCertOwner = `-- name: CheckCertOwner :one
SELECT id, owner
FROM certificates
WHERE active = 1
  and id = ?
`

type CheckCertOwnerRow struct {
	ID    int64  `json:"id"`
	Owner string `json:"owner"`
}

func (q *Queries) CheckCertOwner(ctx context.Context, id int64) (CheckCertOwnerRow, error) {
	row := q.db.QueryRowContext(ctx, checkCertOwner, id)
	var i CheckCertOwnerRow
	err := row.Scan(&i.ID, &i.Owner)
	return i, err
}

const findNextCert = `-- name: FindNextCert :one
SELECT cert.id, cert.not_after, dns_acme.type, dns_acme.token, cert.temp_parent
FROM certificates AS cert
         LEFT OUTER JOIN dns_acme ON cert.dns = dns_acme.id
WHERE cert.active = 1
  AND (cert.auto_renew = 1 OR cert.not_after IS NULL)
  AND cert.renewing = 0
  AND DATETIME() > DATETIME(cert.renew_retry)
  AND (cert.not_after IS NULL OR DATETIME(cert.not_after, 'utc', '-30 days') < DATETIME())
ORDER BY cert.temp_parent, cert.not_after DESC NULLS FIRST
LIMIT 1
`

type FindNextCertRow struct {
	ID         int64          `json:"id"`
	NotAfter   time.Time      `json:"not_after"`
	Type       sql.NullString `json:"type"`
	Token      sql.NullString `json:"token"`
	TempParent sql.NullInt64  `json:"temp_parent"`
}

func (q *Queries) FindNextCert(ctx context.Context) (FindNextCertRow, error) {
	row := q.db.QueryRowContext(ctx, findNextCert)
	var i FindNextCertRow
	err := row.Scan(
		&i.ID,
		&i.NotAfter,
		&i.Type,
		&i.Token,
		&i.TempParent,
	)
	return i, err
}

const findOwnedCerts = `-- name: FindOwnedCerts :many
SELECT cert.id,
       cert.auto_renew,
       cert.active,
       cert.renewing,
       cert.renew_retry,
       cert.not_after,
       cert.updated_at,
       certificate_domains.domain
FROM certificates AS cert
         INNER JOIN certificate_domains ON cert.id = certificate_domains.cert_id
`

type FindOwnedCertsRow struct {
	ID         int64     `json:"id"`
	AutoRenew  bool      `json:"auto_renew"`
	Active     bool      `json:"active"`
	Renewing   bool      `json:"renewing"`
	RenewRetry time.Time `json:"renew_retry"`
	NotAfter   time.Time `json:"not_after"`
	UpdatedAt  time.Time `json:"updated_at"`
	Domain     string    `json:"domain"`
}

func (q *Queries) FindOwnedCerts(ctx context.Context) ([]FindOwnedCertsRow, error) {
	rows, err := q.db.QueryContext(ctx, findOwnedCerts)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []FindOwnedCertsRow
	for rows.Next() {
		var i FindOwnedCertsRow
		if err := rows.Scan(
			&i.ID,
			&i.AutoRenew,
			&i.Active,
			&i.Renewing,
			&i.RenewRetry,
			&i.NotAfter,
			&i.UpdatedAt,
			&i.Domain,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const removeCertificate = `-- name: RemoveCertificate :exec
UPDATE certificates
SET active = 0
WHERE id = ?
`

func (q *Queries) RemoveCertificate(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, removeCertificate, id)
	return err
}

const setRetryFlag = `-- name: SetRetryFlag :exec
UPDATE certificates
SET renew_retry = DATETIME('now', '+1 day')
WHERE id = ?
`

func (q *Queries) SetRetryFlag(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, setRetryFlag, id)
	return err
}

const updateCertAfterRenewal = `-- name: UpdateCertAfterRenewal :exec
UPDATE certificates
SET renewing   = 0,
    renew_retry=0,
    not_after=?,
    updated_at=?
WHERE id = ?
`

type UpdateCertAfterRenewalParams struct {
	NotAfter  time.Time `json:"not_after"`
	UpdatedAt time.Time `json:"updated_at"`
	ID        int64     `json:"id"`
}

func (q *Queries) UpdateCertAfterRenewal(ctx context.Context, arg UpdateCertAfterRenewalParams) error {
	_, err := q.db.ExecContext(ctx, updateCertAfterRenewal, arg.NotAfter, arg.UpdatedAt, arg.ID)
	return err
}

const updateRenewingState = `-- name: UpdateRenewingState :exec
UPDATE certificates
SET renewing    = ?,
    renew_retry = ?
WHERE id = ?
`

type UpdateRenewingStateParams struct {
	Renewing   bool      `json:"renewing"`
	RenewRetry time.Time `json:"renew_retry"`
	ID         int64     `json:"id"`
}

func (q *Queries) UpdateRenewingState(ctx context.Context, arg UpdateRenewingStateParams) error {
	_, err := q.db.ExecContext(ctx, updateRenewingState, arg.Renewing, arg.RenewRetry, arg.ID)
	return err
}
