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

package db

import (
	"context"
	"database/sql"
)

const createDocument = `-- name: CreateDocument :one
INSERT INTO documents (
    "person_id",
    "name",
    "type",
    "path",
    "hash",
    "creator",
    "changer",
    "mail_id"
) VALUES (
    $1, 
    $2, 
    $3, 
    $4, 
    $5, 
    $6, 
    $6,
    $7
) RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type CreateDocumentParams struct {
	PersonID sql.NullInt64 `json:"person_id"`
	Name     string        `json:"name"`
	Type     string        `json:"type"`
	Path     string        `json:"path"`
	Hash     string        `json:"hash"`
	Creator  string        `json:"creator"`
	MailID   sql.NullInt64 `json:"mail_id"`
}

func (q *Queries) CreateDocument(ctx context.Context, arg CreateDocumentParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, createDocument,
		arg.PersonID,
		arg.Name,
		arg.Type,
		arg.Path,
		arg.Hash,
		arg.Creator,
		arg.MailID,
	)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const createDocumentMail = `-- name: CreateDocumentMail :one
INSERT INTO documents (
    "mail_id",
    "name",
    "type",
    "path",
    "hash",
    "creator",
    "changer",
    "person_id"
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, NULL
) RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type CreateDocumentMailParams struct {
	MailID  sql.NullInt64 `json:"mail_id"`
	Name    string        `json:"name"`
	Type    string        `json:"type"`
	Path    string        `json:"path"`
	Hash    string        `json:"hash"`
	Creator string        `json:"creator"`
	Changer string        `json:"changer"`
}

func (q *Queries) CreateDocumentMail(ctx context.Context, arg CreateDocumentMailParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, createDocumentMail,
		arg.MailID,
		arg.Name,
		arg.Type,
		arg.Path,
		arg.Hash,
		arg.Creator,
		arg.Changer,
	)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const createDocumentUpload = `-- name: CreateDocumentUpload :one
INSERT INTO documents (
    "person_id",
    "name",
    "type",
    "path",
    "hash",
    "creator",
    "changer",
    "mail_id"
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, NULL
) RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type CreateDocumentUploadParams struct {
	PersonID sql.NullInt64 `json:"person_id"`
	Name     string        `json:"name"`
	Type     string        `json:"type"`
	Path     string        `json:"path"`
	Hash     string        `json:"hash"`
	Creator  string        `json:"creator"`
	Changer  string        `json:"changer"`
}

func (q *Queries) CreateDocumentUpload(ctx context.Context, arg CreateDocumentUploadParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, createDocumentUpload,
		arg.PersonID,
		arg.Name,
		arg.Type,
		arg.Path,
		arg.Hash,
		arg.Creator,
		arg.Changer,
	)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const deleteDocument = `-- name: DeleteDocument :exec
DELETE FROM documents
WHERE "id" = $1
`

func (q *Queries) DeleteDocument(ctx context.Context, id uint64) error {
	_, err := q.db.ExecContext(ctx, deleteDocument, id)
	return err
}

const deleteDocumentsByPersonID = `-- name: DeleteDocumentsByPersonID :exec
DELETE FROM "documents"
WHERE "person_id" = $1
`

func (q *Queries) DeleteDocumentsByPersonID(ctx context.Context, personID sql.NullInt64) error {
	_, err := q.db.ExecContext(ctx, deleteDocumentsByPersonID, personID)
	return err
}

const getDocument = `-- name: GetDocument :one
SELECT id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed FROM documents
WHERE "id" = $1 LIMIT 1
`

func (q *Queries) GetDocument(ctx context.Context, id uint64) (Document, error) {
	row := q.db.QueryRowContext(ctx, getDocument, id)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const getDocumentByHash = `-- name: GetDocumentByHash :many
SELECT d."id" FROM documents d
INNER JOIN persons p
    ON d."person_id" = p."id"
WHERE p."account_id" = $1 AND
    d."hash" = $2
`

type GetDocumentByHashParams struct {
	AccountID uint64 `json:"account_id"`
	Hash      string `json:"hash"`
}

func (q *Queries) GetDocumentByHash(ctx context.Context, arg GetDocumentByHashParams) ([]uint64, error) {
	rows, err := q.db.QueryContext(ctx, getDocumentByHash, arg.AccountID, arg.Hash)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []uint64{}
	for rows.Next() {
		var id uint64
		if err := rows.Scan(&id); err != nil {
			return nil, err
		}
		items = append(items, id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getDocumentByIDWithAccountID = `-- name: GetDocumentByIDWithAccountID :one
SELECT d.id, d.person_id, d.name, d.type, d.path, d.hash, d.valid, d.valid_date, d.validated_by, d.mail_id, d.creator, d.created, d.changer, d.changed FROM documents d
INNER JOIN persons p
    ON d."person_id" = p."id"
WHERE d."id" = $1 AND p."account_id" = $2
`

type GetDocumentByIDWithAccountIDParams struct {
	ID        uint64 `json:"id"`
	AccountID uint64 `json:"account_id"`
}

func (q *Queries) GetDocumentByIDWithAccountID(ctx context.Context, arg GetDocumentByIDWithAccountIDParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, getDocumentByIDWithAccountID, arg.ID, arg.AccountID)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const invalidateDocument = `-- name: InvalidateDocument :one
UPDATE documents
SET
    "valid" = false,
    "valid_date" = NULL,
    "validated_by" = NULL,    
    "changer" = $2,
    "changed" = now()
WHERE "id" = $1
RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type InvalidateDocumentParams struct {
	ID      uint64 `json:"id"`
	Changer string `json:"changer"`
}

func (q *Queries) InvalidateDocument(ctx context.Context, arg InvalidateDocumentParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, invalidateDocument, arg.ID, arg.Changer)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const listDocuments = `-- name: ListDocuments :many
SELECT id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed FROM documents
ORDER BY "valid", "type", "name"
LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListDocuments(ctx context.Context, arg ListDocumentsParams) ([]Document, error) {
	rows, err := q.db.QueryContext(ctx, listDocuments, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Document{}
	for rows.Next() {
		var i Document
		if err := rows.Scan(
			&i.ID,
			&i.PersonID,
			&i.Name,
			&i.Type,
			&i.Path,
			&i.Hash,
			&i.Valid,
			&i.ValidDate,
			&i.ValidatedBy,
			&i.MailID,
			&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
}

const updateDocument = `-- name: UpdateDocument :one
UPDATE documents
SET
    "name" = COALESCE($3, "name"),
    "type" = COALESCE($4, "type"),
    "path" = COALESCE($5, "path"),
    "hash" = COALESCE($6, "hash"),
    changer = $2,
    changed = now()
WHERE "id" = $1
RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type UpdateDocumentParams struct {
	ID      uint64         `json:"id"`
	Changer string         `json:"changer"`
	Name    sql.NullString `json:"name"`
	Type    sql.NullString `json:"type"`
	Path    sql.NullString `json:"path"`
	Hash    sql.NullString `json:"hash"`
}

func (q *Queries) UpdateDocument(ctx context.Context, arg UpdateDocumentParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, updateDocument,
		arg.ID,
		arg.Changer,
		arg.Name,
		arg.Type,
		arg.Path,
		arg.Hash,
	)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}

const validateDocument = `-- name: ValidateDocument :one
UPDATE documents
SET
    "valid" = true,
    "valid_date" = now(),
    "validated_by" = $2,    
    "changer" = $2,
    "changed" = now()
WHERE "id" = $1
RETURNING id, person_id, name, type, path, hash, valid, valid_date, validated_by, mail_id, creator, created, changer, changed
`

type ValidateDocumentParams struct {
	ID          uint64         `json:"id"`
	ValidatedBy sql.NullString `json:"validated_by"`
}

func (q *Queries) ValidateDocument(ctx context.Context, arg ValidateDocumentParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, validateDocument, arg.ID, arg.ValidatedBy)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.PersonID,
		&i.Name,
		&i.Type,
		&i.Path,
		&i.Hash,
		&i.Valid,
		&i.ValidDate,
		&i.ValidatedBy,
		&i.MailID,
		&i.Creator,
		&i.Created,
		&i.Changer,
		&i.Changed,
	)
	return i, err
}
