// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.22.0
// source: mail.sql

package db

import (
	"context"
	"time"

	"github.com/lib/pq"
)

const createMail = `-- name: CreateMail :one
INSERT INTO mails (
    "from",
    "to",
    "cc",
    "subject",
    "body",
    "timestamp",
    "creator",
    "changer"
) 
VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8
) 
RETURNING id, "from", "to", cc, timestamp, subject, body, creator, created, changer, changed
`

type CreateMailParams struct {
	From      string    `json:"from"`
	To        []string  `json:"to"`
	Cc        []string  `json:"cc"`
	Subject   string    `json:"subject"`
	Body      string    `json:"body"`
	Timestamp time.Time `json:"timestamp"`
	Creator   string    `json:"creator"`
	Changer   string    `json:"changer"`
}

func (q *Queries) CreateMail(ctx context.Context, arg CreateMailParams) (Mail, error) {
	row := q.db.QueryRowContext(ctx, createMail,
		arg.From,
		pq.Array(arg.To),
		pq.Array(arg.Cc),
		arg.Subject,
		arg.Body,
		arg.Timestamp,
		arg.Creator,
		arg.Changer,
	)
	var i Mail
	err := row.Scan(
		&i.ID,
		&i.From,
		pq.Array(&i.To),
		pq.Array(&i.Cc),
		&i.Timestamp,
		&i.Subject,
		&i.Body,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const deleteMail = `-- name: DeleteMail :exec

DELETE FROM mails
WHERE "id" = $1
`

// -- name: UpdateMail :one
// UPDATE mails
// SET
//
//	"from" = COALESCE(sqlc.narg(from), "from"),
//	"to" = COALESCE(sqlc.narg(to), "to"),
//	cc = COALESCE(sqlc.narg(cc), cc),
//	"subject" = COALESCE(sqlc.narg(subject), "subject"),
//	body = COALESCE(sqlc.narg(body), body),
//	"timestamp" = COALESCE(sqlc.narg(timestamp), "timestamp"),
//	changer = $2,
//	changed = now()
//
// WHERE "id" = $1
// RETURNING *;
func (q *Queries) DeleteMail(ctx context.Context, id uint64) error {
	_, err := q.db.ExecContext(ctx, deleteMail, id)
	return err
}

const getMail = `-- name: GetMail :one
SELECT id, "from", "to", cc, timestamp, subject, body, creator, created, changer, changed FROM mails
WHERE "id" = $1 LIMIT 1
`

func (q *Queries) GetMail(ctx context.Context, id uint64) (Mail, error) {
	row := q.db.QueryRowContext(ctx, getMail, id)
	var i Mail
	err := row.Scan(
		&i.ID,
		&i.From,
		pq.Array(&i.To),
		pq.Array(&i.Cc),
		&i.Timestamp,
		&i.Subject,
		&i.Body,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const listMails = `-- name: ListMails :many
SELECT id, "from", "to", cc, timestamp, subject, body, creator, created, changer, changed FROM mails
ORDER BY "timestamp", "from"
LIMIT $1
OFFSET $2
`

type ListMailsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListMails(ctx context.Context, arg ListMailsParams) ([]Mail, error) {
	rows, err := q.db.QueryContext(ctx, listMails, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Mail{}
	for rows.Next() {
		var i Mail
		if err := rows.Scan(
			&i.ID,
			&i.From,
			pq.Array(&i.To),
			pq.Array(&i.Cc),
			&i.Timestamp,
			&i.Subject,
			&i.Body,
			&i.Creator,
			&i.Created,
			&i.Changer,
			&i.Changed,
		); 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
}
