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

package cart

import (
	"context"
)

const deleteCartItem = `-- name: DeleteCartItem :execrows
UPDATE cart_items
SET is_removed = TRUE
WHERE id = $1
`

func (q *Queries) DeleteCartItem(ctx context.Context, id int64) (int64, error) {
	result, err := q.db.Exec(ctx, deleteCartItem, id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const incrementCartItemQty = `-- name: IncrementCartItemQty :one
UPDATE cart_items
SET qty = CASE
              WHEN is_removed = TRUE THEN $3
              ELSE qty + $3
    END,
    is_removed = FALSE,
    updated_at = NOW()
WHERE cart_id = $1
  AND variant_id = $2
RETURNING id, cart_id, variant_id, qty, price, currency, thumbnail, product_name, product_path, variant_attributes, is_removed, created_at, updated_at
`

func (q *Queries) IncrementCartItemQty(ctx context.Context, cartID int64, variantID int64, qty int32) (*CartItem, error) {
	row := q.db.QueryRow(ctx, incrementCartItemQty, cartID, variantID, qty)
	var i CartItem
	err := row.Scan(
		&i.ID,
		&i.CartID,
		&i.VariantID,
		&i.Qty,
		&i.Price,
		&i.Currency,
		&i.Thumbnail,
		&i.ProductName,
		&i.ProductPath,
		&i.VariantAttributes,
		&i.IsRemoved,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const updateCartItemQty = `-- name: UpdateCartItemQty :execrows
UPDATE cart_items
SET qty = $2,
    updated_at = NOW()
WHERE id = $1 AND is_removed IS FALSE
`

func (q *Queries) UpdateCartItemQty(ctx context.Context, iD int64, qty int32) (int64, error) {
	result, err := q.db.Exec(ctx, updateCartItemQty, iD, qty)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const updateCartStatus = `-- name: UpdateCartStatus :execrows
UPDATE carts
SET status = $2
WHERE id = $1
`

func (q *Queries) UpdateCartStatus(ctx context.Context, iD int64, status CartStatus) (int64, error) {
	result, err := q.db.Exec(ctx, updateCartStatus, iD, status)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
