// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: product_variant.sql

package product

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const createProductVariant = `-- name: CreateProductVariant :one
INSERT INTO product_variants (
    product_id, sku, qty, is_custom_priced, image_identifiers
) VALUES (
    $1, $2, $3, $4, $5
) RETURNING id
`

func (q *Queries) CreateProductVariant(ctx context.Context, productID int64, sku string, qty int32, isCustomPriced bool, imageIdentifiers []int16) (int64, error) {
	row := q.db.QueryRow(ctx, createProductVariant,
		productID,
		sku,
		qty,
		isCustomPriced,
		imageIdentifiers,
	)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const getProductVariantByID = `-- name: GetProductVariantByID :one
SELECT id, product_id, sku, qty, is_custom_priced, image_identifiers, created_at, updated_at FROM product_variants
WHERE id = $1
`

func (q *Queries) GetProductVariantByID(ctx context.Context, id int64) (*ProductVariant, error) {
	row := q.db.QueryRow(ctx, getProductVariantByID, id)
	var i ProductVariant
	err := row.Scan(
		&i.ID,
		&i.ProductID,
		&i.Sku,
		&i.Qty,
		&i.IsCustomPriced,
		&i.ImageIdentifiers,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const getProductVariantQtyInBulk = `-- name: GetProductVariantQtyInBulk :many
SELECT
    id,
    qty
FROM
    product_variants
WHERE
    id = ANY($1::BIGINT[])
`

type GetProductVariantQtyInBulkRow struct {
	ID  int64
	Qty int32
}

func (q *Queries) GetProductVariantQtyInBulk(ctx context.Context, variantids []int64) ([]*GetProductVariantQtyInBulkRow, error) {
	rows, err := q.db.Query(ctx, getProductVariantQtyInBulk, variantids)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*GetProductVariantQtyInBulkRow
	for rows.Next() {
		var i GetProductVariantQtyInBulkRow
		if err := rows.Scan(&i.ID, &i.Qty); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getProductVariantsByProductID = `-- name: GetProductVariantsByProductID :many
SELECT
    pv.id AS variant_id,
    pv.product_id,
    pv.sku,
    pv.qty,
    pv.is_custom_priced,
    pv.image_identifiers,
    pv.created_at AS created_at,
    pv.updated_at AS updated_at,
    jsonb_object_agg(pa.attribute, pva.attribute_value) FILTER (WHERE pa.attribute IS NOT NULL) AS attributes
FROM
    product_variants pv
        LEFT JOIN
    product_variant_attributes pva ON pv.id = pva.variant_id
        LEFT JOIN
    product_attributes pa ON pva.product_attribute_id = pa.id
WHERE
    pv.product_id = $1
GROUP BY
    pv.id, pv.product_id, pv.sku, pv.qty, pv.is_custom_priced, pv.image_identifiers, pv.created_at, pv.updated_at
`

type GetProductVariantsByProductIDRow struct {
	VariantID        int64
	ProductID        int64
	Sku              string
	Qty              int32
	IsCustomPriced   bool
	ImageIdentifiers []int16
	CreatedAt        pgtype.Timestamptz
	UpdatedAt        pgtype.Timestamptz
	Attributes       []byte
}

func (q *Queries) GetProductVariantsByProductID(ctx context.Context, productID int64) ([]*GetProductVariantsByProductIDRow, error) {
	rows, err := q.db.Query(ctx, getProductVariantsByProductID, productID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*GetProductVariantsByProductIDRow
	for rows.Next() {
		var i GetProductVariantsByProductIDRow
		if err := rows.Scan(
			&i.VariantID,
			&i.ProductID,
			&i.Sku,
			&i.Qty,
			&i.IsCustomPriced,
			&i.ImageIdentifiers,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Attributes,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
