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

package db

import (
	"context"
	"database/sql"
)

const CrawlDomain = `-- name: CrawlDomain :many
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated
FROM domain_crawl_list
WHERE id > $1
ORDER BY id
LIMIT $2
`

type CrawlDomainParams struct {
	ID    int64
	Limit int64
}

func (q *Queries) CrawlDomain(ctx context.Context, arg CrawlDomainParams) ([]DomainCrawlList, error) {
	rows, err := q.db.Query(ctx, CrawlDomain, arg.ID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DomainCrawlList{}
	for rows.Next() {
		var i DomainCrawlList
		if err := rows.Scan(
			&i.ID,
			&i.Site,
			&i.BaseDomain,
			&i.WwwDomain,
			&i.Nameserver,
			&i.MxRecord,
			&i.V6Only,
			&i.AsnID,
			&i.CountryID,
			&i.Disabled,
			&i.TsBaseDomain,
			&i.TsWwwDomain,
			&i.TsNameserver,
			&i.TsMxRecord,
			&i.TsV6Only,
			&i.TsCheck,
			&i.TsUpdated,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const DisableDomain = `-- name: DisableDomain :exec
UPDATE
    domain
SET disabled = TRUE
WHERE site = $1
`

func (q *Queries) DisableDomain(ctx context.Context, site string) error {
	_, err := q.db.Exec(ctx, DisableDomain, site)
	return err
}

const GetDomainsByName = `-- name: GetDomainsByName :many
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated, rank, asname, country_name
FROM domain_view_list
WHERE site LIKE '%' || $1 || '%'
ORDER BY rank
LIMIT $2 OFFSET $3
`

type GetDomainsByNameParams struct {
	Column1 sql.NullString
	Limit   int64
	Offset  int64
}

func (q *Queries) GetDomainsByName(ctx context.Context, arg GetDomainsByNameParams) ([]DomainViewList, error) {
	rows, err := q.db.Query(ctx, GetDomainsByName, arg.Column1, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DomainViewList{}
	for rows.Next() {
		var i DomainViewList
		if err := rows.Scan(
			&i.ID,
			&i.Site,
			&i.BaseDomain,
			&i.WwwDomain,
			&i.Nameserver,
			&i.MxRecord,
			&i.V6Only,
			&i.AsnID,
			&i.CountryID,
			&i.Disabled,
			&i.TsBaseDomain,
			&i.TsWwwDomain,
			&i.TsNameserver,
			&i.TsMxRecord,
			&i.TsV6Only,
			&i.TsCheck,
			&i.TsUpdated,
			&i.Rank,
			&i.Asname,
			&i.CountryName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const InitSpaceTimestamps = `-- name: InitSpaceTimestamps :exec
WITH DomainCount AS (SELECT count(*)::DECIMAL AS total_records
                     FROM domain),
     IntervalCalculation AS (SELECT (NOW() - '3 days'::INTERVAL)         AS calculatedStartTime,
                                    ('3 days'::INTERVAL) / total_records AS calculatedIntervalStep
                             FROM DomainCount),
     SpacedTimestampUpdates AS (SELECT d.id,
                                       ic.calculatedStartTime + ic.calculatedIntervalStep * 
                                       ROW_NUMBER() OVER (ORDER BY d.id) AS newSpacedTimestamp
                                FROM domain d,
                                     IntervalCalculation ic)
UPDATE domain
SET ts_check = stu.newSpacedTimestamp
FROM SpacedTimestampUpdates stu
WHERE domain.id = stu.id
`

func (q *Queries) InitSpaceTimestamps(ctx context.Context) error {
	_, err := q.db.Exec(ctx, InitSpaceTimestamps)
	return err
}

const InsertDomain = `-- name: InsertDomain :exec
INSERT INTO domain(site)
VALUES ($1)
ON CONFLICT DO NOTHING
`

func (q *Queries) InsertDomain(ctx context.Context, site string) error {
	_, err := q.db.Exec(ctx, InsertDomain, site)
	return err
}

const ListDomain = `-- name: ListDomain :many
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated, rank, asname, country_name
FROM domain_view_list
WHERE base_domain = 'unsupported'
   OR www_domain = 'unsupported'
ORDER BY rank
LIMIT $1 OFFSET $2
`

type ListDomainParams struct {
	Limit  int64
	Offset int64
}

func (q *Queries) ListDomain(ctx context.Context, arg ListDomainParams) ([]DomainViewList, error) {
	rows, err := q.db.Query(ctx, ListDomain, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DomainViewList{}
	for rows.Next() {
		var i DomainViewList
		if err := rows.Scan(
			&i.ID,
			&i.Site,
			&i.BaseDomain,
			&i.WwwDomain,
			&i.Nameserver,
			&i.MxRecord,
			&i.V6Only,
			&i.AsnID,
			&i.CountryID,
			&i.Disabled,
			&i.TsBaseDomain,
			&i.TsWwwDomain,
			&i.TsNameserver,
			&i.TsMxRecord,
			&i.TsV6Only,
			&i.TsCheck,
			&i.TsUpdated,
			&i.Rank,
			&i.Asname,
			&i.CountryName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListDomainHeroes = `-- name: ListDomainHeroes :many
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated, rank, asname, country_name
FROM domain_view_list
WHERE base_domain = 'supported'
  AND www_domain = 'supported'
  AND nameserver = 'supported'
  AND mx_record != 'unsupported'
ORDER BY rank
LIMIT $1 OFFSET $2
`

type ListDomainHeroesParams struct {
	Limit  int64
	Offset int64
}

func (q *Queries) ListDomainHeroes(ctx context.Context, arg ListDomainHeroesParams) ([]DomainViewList, error) {
	rows, err := q.db.Query(ctx, ListDomainHeroes, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DomainViewList{}
	for rows.Next() {
		var i DomainViewList
		if err := rows.Scan(
			&i.ID,
			&i.Site,
			&i.BaseDomain,
			&i.WwwDomain,
			&i.Nameserver,
			&i.MxRecord,
			&i.V6Only,
			&i.AsnID,
			&i.CountryID,
			&i.Disabled,
			&i.TsBaseDomain,
			&i.TsWwwDomain,
			&i.TsNameserver,
			&i.TsMxRecord,
			&i.TsV6Only,
			&i.TsCheck,
			&i.TsUpdated,
			&i.Rank,
			&i.Asname,
			&i.CountryName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListDomainShamers = `-- name: ListDomainShamers :many
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated, shame_id, shame_site
FROM domain_shame_view
`

func (q *Queries) ListDomainShamers(ctx context.Context) ([]DomainShameView, error) {
	rows, err := q.db.Query(ctx, ListDomainShamers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []DomainShameView{}
	for rows.Next() {
		var i DomainShameView
		if err := rows.Scan(
			&i.ID,
			&i.Site,
			&i.BaseDomain,
			&i.WwwDomain,
			&i.Nameserver,
			&i.MxRecord,
			&i.V6Only,
			&i.AsnID,
			&i.CountryID,
			&i.Disabled,
			&i.TsBaseDomain,
			&i.TsWwwDomain,
			&i.TsNameserver,
			&i.TsMxRecord,
			&i.TsV6Only,
			&i.TsCheck,
			&i.TsUpdated,
			&i.ShameID,
			&i.ShameSite,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateDomain = `-- name: UpdateDomain :exec
UPDATE
    domain
SET base_domain    = $2,
    www_domain     = $3,
    nameserver     = $4,
    mx_record      = $5,
    v6_only        = $6,
    ts_base_domain = $7,
    ts_www_domain  = $8,
    ts_nameserver  = $9,
    ts_mx_record   = $10,
    ts_v6_only     = $11,
    ts_check       = $12,
    ts_updated     = $13,
    asn_id         = $14,
    country_id     = $15
WHERE site = $1
`

type UpdateDomainParams struct {
	Site         string
	BaseDomain   string
	WwwDomain    string
	Nameserver   string
	MxRecord     string
	V6Only       string
	TsBaseDomain sql.NullTime
	TsWwwDomain  sql.NullTime
	TsNameserver sql.NullTime
	TsMxRecord   sql.NullTime
	TsV6Only     sql.NullTime
	TsCheck      sql.NullTime
	TsUpdated    sql.NullTime
	AsnID        sql.NullInt64
	CountryID    sql.NullInt64
}

func (q *Queries) UpdateDomain(ctx context.Context, arg UpdateDomainParams) error {
	_, err := q.db.Exec(ctx, UpdateDomain,
		arg.Site,
		arg.BaseDomain,
		arg.WwwDomain,
		arg.Nameserver,
		arg.MxRecord,
		arg.V6Only,
		arg.TsBaseDomain,
		arg.TsWwwDomain,
		arg.TsNameserver,
		arg.TsMxRecord,
		arg.TsV6Only,
		arg.TsCheck,
		arg.TsUpdated,
		arg.AsnID,
		arg.CountryID,
	)
	return err
}

const ViewDomain = `-- name: ViewDomain :one
SELECT id, site, base_domain, www_domain, nameserver, mx_record, v6_only, asn_id, country_id, disabled, ts_base_domain, ts_www_domain, ts_nameserver, ts_mx_record, ts_v6_only, ts_check, ts_updated, rank, asname, country_name
FROM domain_view_list
WHERE site = $1
LIMIT 1
`

func (q *Queries) ViewDomain(ctx context.Context, site sql.NullString) (DomainViewList, error) {
	row := q.db.QueryRow(ctx, ViewDomain, site)
	var i DomainViewList
	err := row.Scan(
		&i.ID,
		&i.Site,
		&i.BaseDomain,
		&i.WwwDomain,
		&i.Nameserver,
		&i.MxRecord,
		&i.V6Only,
		&i.AsnID,
		&i.CountryID,
		&i.Disabled,
		&i.TsBaseDomain,
		&i.TsWwwDomain,
		&i.TsNameserver,
		&i.TsMxRecord,
		&i.TsV6Only,
		&i.TsCheck,
		&i.TsUpdated,
		&i.Rank,
		&i.Asname,
		&i.CountryName,
	)
	return i, err
}
