// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.25.0
// source: warehouse_asset.sql

package db

import (
	"context"
)

const addWarehouseAssetQuantity = `-- name: AddWarehouseAssetQuantity :execrows
UPDATE warehouse_asset SET quantity = quantity + $1 WHERE warehouse_id = $2 AND asset_id = $3
`

type AddWarehouseAssetQuantityParams struct {
	Quantity    int64 `json:"quantity"`
	WarehouseID int64 `json:"warehouse_id"`
	AssetID     int64 `json:"asset_id"`
}

func (q *Queries) AddWarehouseAssetQuantity(ctx context.Context, arg AddWarehouseAssetQuantityParams) (int64, error) {
	result, err := q.db.Exec(ctx, addWarehouseAssetQuantity, arg.Quantity, arg.WarehouseID, arg.AssetID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const createWarehouseAsset = `-- name: CreateWarehouseAsset :exec
INSERT INTO warehouse_asset (warehouse_id, asset_id, quantity) VALUES ($1, $2, $3)
`

type CreateWarehouseAssetParams struct {
	WarehouseID int64 `json:"warehouse_id"`
	AssetID     int64 `json:"asset_id"`
	Quantity    int64 `json:"quantity"`
}

func (q *Queries) CreateWarehouseAsset(ctx context.Context, arg CreateWarehouseAssetParams) error {
	_, err := q.db.Exec(ctx, createWarehouseAsset, arg.WarehouseID, arg.AssetID, arg.Quantity)
	return err
}

const getWarehouseAssets = `-- name: GetWarehouseAssets :many
SELECT a.id, a.name, a.value, wa.quantity
FROM warehouse_asset wa
JOIN asset a ON a.id = wa.asset_id
WHERE wa.warehouse_id = $1
`

type GetWarehouseAssetsRow struct {
	ID       int64  `json:"id"`
	Name     string `json:"name"`
	Value    int64  `json:"value"`
	Quantity int64  `json:"quantity"`
}

func (q *Queries) GetWarehouseAssets(ctx context.Context, warehouseID int64) ([]GetWarehouseAssetsRow, error) {
	rows, err := q.db.Query(ctx, getWarehouseAssets, warehouseID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetWarehouseAssetsRow
	for rows.Next() {
		var i GetWarehouseAssetsRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Value,
			&i.Quantity,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const subtractWarehouseAssetQuantity = `-- name: SubtractWarehouseAssetQuantity :execrows
UPDATE warehouse_asset SET quantity = quantity - $1 WHERE warehouse_id = $2 AND quantity >= $1 AND asset_id = $3
`

type SubtractWarehouseAssetQuantityParams struct {
	Quantity    int64 `json:"quantity"`
	WarehouseID int64 `json:"warehouse_id"`
	AssetID     int64 `json:"asset_id"`
}

func (q *Queries) SubtractWarehouseAssetQuantity(ctx context.Context, arg SubtractWarehouseAssetQuantityParams) (int64, error) {
	result, err := q.db.Exec(ctx, subtractWarehouseAssetQuantity, arg.Quantity, arg.WarehouseID, arg.AssetID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
