// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: documents.sql

package db

import (
	"context"
)

const allDocumentDetails = `-- name: AllDocumentDetails :many
SELECT name,description,documentusage,iscrop,isglobal,crops.cropname, countries.countryname FROM documents
INNER JOIN document_crops on document_crops.document_id = documents.id
INNER JOIN document_countries on document_countries.document_id = documents.id
INNER JOIN countries on countries.id = document_countries.country_id
INNER JOIN crops     on crops.id     = document_crops.crop_id
WHERE documents.id = $1
`

type AllDocumentDetailsRow struct {
	Name          string `json:"name"`
	Description   string `json:"description"`
	Documentusage string `json:"documentusage"`
	Iscrop        bool   `json:"iscrop"`
	Isglobal      bool   `json:"isglobal"`
	Cropname      string `json:"cropname"`
	Countryname   string `json:"countryname"`
}

func (q *Queries) AllDocumentDetails(ctx context.Context, id int64) ([]AllDocumentDetailsRow, error) {
	rows, err := q.db.QueryContext(ctx, allDocumentDetails, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []AllDocumentDetailsRow{}
	for rows.Next() {
		var i AllDocumentDetailsRow
		if err := rows.Scan(
			&i.Name,
			&i.Description,
			&i.Documentusage,
			&i.Iscrop,
			&i.Isglobal,
			&i.Cropname,
			&i.Countryname,
		); 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 checkDocumentIDReceptionDocs = `-- name: CheckDocumentIDReceptionDocs :one
SELECT EXISTS (
    select 1 from reception_documents
    where document_id = $1
    AND   receptionform_id = $2
)
`

type CheckDocumentIDReceptionDocsParams struct {
	DocumentID      int32 `json:"document_id"`
	ReceptionformID int32 `json:"receptionform_id"`
}

func (q *Queries) CheckDocumentIDReceptionDocs(ctx context.Context, arg CheckDocumentIDReceptionDocsParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkDocumentIDReceptionDocs, arg.DocumentID, arg.ReceptionformID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const createDocumentCountries = `-- name: CreateDocumentCountries :one
INSERT INTO document_countries (
    country_id,
    document_id
 ) VALUES (
    $1,
    $2
 ) RETURNING id, country_id, document_id, created_at
`

type CreateDocumentCountriesParams struct {
	CountryID  int32 `json:"country_id"`
	DocumentID int32 `json:"document_id"`
}

func (q *Queries) CreateDocumentCountries(ctx context.Context, arg CreateDocumentCountriesParams) (DocumentCountry, error) {
	row := q.db.QueryRowContext(ctx, createDocumentCountries, arg.CountryID, arg.DocumentID)
	var i DocumentCountry
	err := row.Scan(
		&i.ID,
		&i.CountryID,
		&i.DocumentID,
		&i.CreatedAt,
	)
	return i, err
}

const createDocumentCrops = `-- name: CreateDocumentCrops :one
INSERT INTO document_crops (
    crop_id,
    document_id
 ) VALUES (
    $1,
    $2
 ) RETURNING id, crop_id, document_id, created_at
`

type CreateDocumentCropsParams struct {
	CropID     int32 `json:"crop_id"`
	DocumentID int32 `json:"document_id"`
}

func (q *Queries) CreateDocumentCrops(ctx context.Context, arg CreateDocumentCropsParams) (DocumentCrop, error) {
	row := q.db.QueryRowContext(ctx, createDocumentCrops, arg.CropID, arg.DocumentID)
	var i DocumentCrop
	err := row.Scan(
		&i.ID,
		&i.CropID,
		&i.DocumentID,
		&i.CreatedAt,
	)
	return i, err
}

const createDocuments = `-- name: CreateDocuments :one
INSERT INTO documents (
    name,
    description,
    documentusage,
    iscrop,
    isglobal
 ) VALUES (
    $1,
    $2,
    $3,
    $4,
    $5
 ) RETURNING id, name, description, documentusage, iscrop, isglobal, created_at
`

type CreateDocumentsParams struct {
	Name          string `json:"name"`
	Description   string `json:"description"`
	Documentusage string `json:"documentusage"`
	Iscrop        bool   `json:"iscrop"`
	Isglobal      bool   `json:"isglobal"`
}

func (q *Queries) CreateDocuments(ctx context.Context, arg CreateDocumentsParams) (Document, error) {
	row := q.db.QueryRowContext(ctx, createDocuments,
		arg.Name,
		arg.Description,
		arg.Documentusage,
		arg.Iscrop,
		arg.Isglobal,
	)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Documentusage,
		&i.Iscrop,
		&i.Isglobal,
		&i.CreatedAt,
	)
	return i, err
}

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

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

const getDocument = `-- name: GetDocument :one
SELECT id, name, description, documentusage, iscrop, isglobal, created_at FROM documents
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetDocument(ctx context.Context, id int64) (Document, error) {
	row := q.db.QueryRowContext(ctx, getDocument, id)
	var i Document
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Documentusage,
		&i.Iscrop,
		&i.Isglobal,
		&i.CreatedAt,
	)
	return i, err
}

const listCropDocuments = `-- name: ListCropDocuments :many
SELECT  documents.id, crops.cropname, documents.name,documents.description,
documents.documentusage,documents.iscrop,documents.isglobal
from    document_crops
JOIN    documents on documents.id = document_crops.document_id
JOIN    crops     on crops.id     = document_crops.crop_id
where document_crops.crop_id      = $1
`

type ListCropDocumentsRow struct {
	ID            int64  `json:"id"`
	Cropname      string `json:"cropname"`
	Name          string `json:"name"`
	Description   string `json:"description"`
	Documentusage string `json:"documentusage"`
	Iscrop        bool   `json:"iscrop"`
	Isglobal      bool   `json:"isglobal"`
}

func (q *Queries) ListCropDocuments(ctx context.Context, cropID int32) ([]ListCropDocumentsRow, error) {
	rows, err := q.db.QueryContext(ctx, listCropDocuments, cropID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListCropDocumentsRow{}
	for rows.Next() {
		var i ListCropDocumentsRow
		if err := rows.Scan(
			&i.ID,
			&i.Cropname,
			&i.Name,
			&i.Description,
			&i.Documentusage,
			&i.Iscrop,
			&i.Isglobal,
		); 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 listDocuments = `-- name: ListDocuments :many
SELECT id, name, description, documentusage, iscrop, isglobal, created_at FROM documents
`

func (q *Queries) ListDocuments(ctx context.Context) ([]Document, error) {
	rows, err := q.db.QueryContext(ctx, listDocuments)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Document{}
	for rows.Next() {
		var i Document
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Description,
			&i.Documentusage,
			&i.Iscrop,
			&i.Isglobal,
			&i.CreatedAt,
		); 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 updateReceptionDocumentsBasedDocumentID = `-- name: UpdateReceptionDocumentsBasedDocumentID :one
UPDATE reception_documents
  set
    document = $2
WHERE receptionform_id = $1
AND   document_id  = $3
RETURNING id, receptionform_id, description, created_at, document_id, document
`

type UpdateReceptionDocumentsBasedDocumentIDParams struct {
	ReceptionformID int32  `json:"receptionform_id"`
	Document        string `json:"document"`
	DocumentID      int32  `json:"document_id"`
}

func (q *Queries) UpdateReceptionDocumentsBasedDocumentID(ctx context.Context, arg UpdateReceptionDocumentsBasedDocumentIDParams) (ReceptionDocument, error) {
	row := q.db.QueryRowContext(ctx, updateReceptionDocumentsBasedDocumentID, arg.ReceptionformID, arg.Document, arg.DocumentID)
	var i ReceptionDocument
	err := row.Scan(
		&i.ID,
		&i.ReceptionformID,
		&i.Description,
		&i.CreatedAt,
		&i.DocumentID,
		&i.Document,
	)
	return i, err
}
