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

package db

import (
	"context"
	"time"
)

const createBoxes = `-- name: CreateBoxes :one
INSERT INTO boxes (           
    packinglist_id,
    traceability_code,
    product_id,
    qrcode,
    status,
    user_id,
    exportcompany_id
 ) VALUES (
    $1, 
    $2, 
    $3, 
    $4, 
    $5, 
    $6, 
    $7
) 
RETURNING id, packinglist_id, traceability_code, product_id, qrcode, status, user_id, exportcompany_id, created_at
`

type CreateBoxesParams struct {
	PackinglistID    int32  `json:"packinglist_id"`
	TraceabilityCode string `json:"traceability_code"`
	ProductID        int32  `json:"product_id"`
	Qrcode           string `json:"qrcode"`
	Status           string `json:"status"`
	UserID           int32  `json:"user_id"`
	ExportcompanyID  int32  `json:"exportcompany_id"`
}

func (q *Queries) CreateBoxes(ctx context.Context, arg CreateBoxesParams) (Box, error) {
	row := q.db.QueryRowContext(ctx, createBoxes,
		arg.PackinglistID,
		arg.TraceabilityCode,
		arg.ProductID,
		arg.Qrcode,
		arg.Status,
		arg.UserID,
		arg.ExportcompanyID,
	)
	var i Box
	err := row.Scan(
		&i.ID,
		&i.PackinglistID,
		&i.TraceabilityCode,
		&i.ProductID,
		&i.Qrcode,
		&i.Status,
		&i.UserID,
		&i.ExportcompanyID,
		&i.CreatedAt,
	)
	return i, err
}

const getBoxesPerPackList = `-- name: GetBoxesPerPackList :many
SELECT boxes.packinglist_id,
boxes.traceability_code,boxes.product_id,
boxes.qrcode,boxes.status,boxes.user_id,
boxes.exportcompany_id,crops.cropname, 
exportcompanies.expname as exportcompany, 
packinglist.created_at  as date_created FROM boxes
JOIN crops on crops.id = boxes.product_id
JOIN exportcompanies on exportcompanies.id = boxes.exportcompany_id
JOIN packinglist on packinglist.id = boxes.packinglist_id
WHERE packinglist_id = $1
`

type GetBoxesPerPackListRow struct {
	PackinglistID    int32     `json:"packinglist_id"`
	TraceabilityCode string    `json:"traceability_code"`
	ProductID        int32     `json:"product_id"`
	Qrcode           string    `json:"qrcode"`
	Status           string    `json:"status"`
	UserID           int32     `json:"user_id"`
	ExportcompanyID  int32     `json:"exportcompany_id"`
	Cropname         string    `json:"cropname"`
	Exportcompany    string    `json:"exportcompany"`
	DateCreated      time.Time `json:"date_created"`
}

func (q *Queries) GetBoxesPerPackList(ctx context.Context, packinglistID int32) ([]GetBoxesPerPackListRow, error) {
	rows, err := q.db.QueryContext(ctx, getBoxesPerPackList, packinglistID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetBoxesPerPackListRow{}
	for rows.Next() {
		var i GetBoxesPerPackListRow
		if err := rows.Scan(
			&i.PackinglistID,
			&i.TraceabilityCode,
			&i.ProductID,
			&i.Qrcode,
			&i.Status,
			&i.UserID,
			&i.ExportcompanyID,
			&i.Cropname,
			&i.Exportcompany,
			&i.DateCreated,
		); 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 listBoxes = `-- name: ListBoxes :many
SELECT id, packinglist_id, traceability_code, product_id, qrcode, status, user_id, exportcompany_id, created_at FROM boxes
WHERE exportcompany_id = $3
LIMIT $1
OFFSET $2
`

type ListBoxesParams struct {
	Limit           int32 `json:"limit"`
	Offset          int32 `json:"offset"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) ListBoxes(ctx context.Context, arg ListBoxesParams) ([]Box, error) {
	rows, err := q.db.QueryContext(ctx, listBoxes, arg.Limit, arg.Offset, arg.ExportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Box{}
	for rows.Next() {
		var i Box
		if err := rows.Scan(
			&i.ID,
			&i.PackinglistID,
			&i.TraceabilityCode,
			&i.ProductID,
			&i.Qrcode,
			&i.Status,
			&i.UserID,
			&i.ExportcompanyID,
			&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
}
