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

package db

import (
	"context"

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

const addWarehouseCollateral = `-- name: AddWarehouseCollateral :execrows
UPDATE warehouse SET collateral = collateral + $1 WHERE id = $2
`

type AddWarehouseCollateralParams struct {
	Collateral int64 `json:"collateral"`
	ID         int64 `json:"id"`
}

func (q *Queries) AddWarehouseCollateral(ctx context.Context, arg AddWarehouseCollateralParams) (int64, error) {
	result, err := q.db.Exec(ctx, addWarehouseCollateral, arg.Collateral, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const addWarehouseLiability = `-- name: AddWarehouseLiability :exec
UPDATE warehouse SET liability = liability + $1 WHERE id = $2
`

type AddWarehouseLiabilityParams struct {
	Liability int64 `json:"liability"`
	ID        int64 `json:"id"`
}

func (q *Queries) AddWarehouseLiability(ctx context.Context, arg AddWarehouseLiabilityParams) error {
	_, err := q.db.Exec(ctx, addWarehouseLiability, arg.Liability, arg.ID)
	return err
}

const getWarehouseCollateralLiabilityAndRatio = `-- name: GetWarehouseCollateralLiabilityAndRatio :one
SELECT collateral, liability, collateral_ratio FROM warehouse WHERE id = $1
`

type GetWarehouseCollateralLiabilityAndRatioRow struct {
	Collateral      int64          `json:"collateral"`
	Liability       int64          `json:"liability"`
	CollateralRatio pgtype.Numeric `json:"collateral_ratio"`
}

func (q *Queries) GetWarehouseCollateralLiabilityAndRatio(ctx context.Context, id int64) (GetWarehouseCollateralLiabilityAndRatioRow, error) {
	row := q.db.QueryRow(ctx, getWarehouseCollateralLiabilityAndRatio, id)
	var i GetWarehouseCollateralLiabilityAndRatioRow
	err := row.Scan(&i.Collateral, &i.Liability, &i.CollateralRatio)
	return i, err
}

const getWarehouseCollateralLiabilityAndRatioLock = `-- name: GetWarehouseCollateralLiabilityAndRatioLock :one
SELECT collateral, liability, collateral_ratio FROM warehouse WHERE id = $1 FOR UPDATE
`

type GetWarehouseCollateralLiabilityAndRatioLockRow struct {
	Collateral      int64          `json:"collateral"`
	Liability       int64          `json:"liability"`
	CollateralRatio pgtype.Numeric `json:"collateral_ratio"`
}

func (q *Queries) GetWarehouseCollateralLiabilityAndRatioLock(ctx context.Context, id int64) (GetWarehouseCollateralLiabilityAndRatioLockRow, error) {
	row := q.db.QueryRow(ctx, getWarehouseCollateralLiabilityAndRatioLock, id)
	var i GetWarehouseCollateralLiabilityAndRatioLockRow
	err := row.Scan(&i.Collateral, &i.Liability, &i.CollateralRatio)
	return i, err
}

const getWarehouseUserId = `-- name: GetWarehouseUserId :one
SELECT user_id FROM warehouse WHERE id = $1
`

func (q *Queries) GetWarehouseUserId(ctx context.Context, id int64) (int64, error) {
	row := q.db.QueryRow(ctx, getWarehouseUserId, id)
	var user_id int64
	err := row.Scan(&user_id)
	return user_id, err
}

const getWarehousesCollateralTotals = `-- name: GetWarehousesCollateralTotals :one
SELECT
    COALESCE(SUM(wa.quantity * a.value), 0)::BIGINT AS warehouse_assets_total,
    COALESCE(SUM(ta.quantity * a.value), 0)::BIGINT AS transferred_assets_total
FROM
    warehouse w
JOIN
	warehouse_asset wa ON wa.warehouse_id = w.id
LEFT JOIN
    transfer t ON t.receiving_warehouse_id = w.id AND t.status = 'approved' AND t.receiving_warehouse_id = $1
LEFT JOIN
    transfer_asset ta ON ta.transfer_id = t.id
JOIN
    asset a ON a.id = wa.asset_id
WHERE
    w.id = $1
`

type GetWarehousesCollateralTotalsRow struct {
	WarehouseAssetsTotal   int64 `json:"warehouse_assets_total"`
	TransferredAssetsTotal int64 `json:"transferred_assets_total"`
}

func (q *Queries) GetWarehousesCollateralTotals(ctx context.Context, receivingWarehouseID int64) (GetWarehousesCollateralTotalsRow, error) {
	row := q.db.QueryRow(ctx, getWarehousesCollateralTotals, receivingWarehouseID)
	var i GetWarehousesCollateralTotalsRow
	err := row.Scan(&i.WarehouseAssetsTotal, &i.TransferredAssetsTotal)
	return i, err
}

const insertWarehouse = `-- name: InsertWarehouse :one
INSERT INTO warehouse (name, user_id, location) VALUES ($1, $2, $3) RETURNING id
`

type InsertWarehouseParams struct {
	Name     string      `json:"name"`
	UserID   int64       `json:"user_id"`
	Location interface{} `json:"location"`
}

func (q *Queries) InsertWarehouse(ctx context.Context, arg InsertWarehouseParams) (int64, error) {
	row := q.db.QueryRow(ctx, insertWarehouse, arg.Name, arg.UserID, arg.Location)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const subtractWarehouseCollateral = `-- name: SubtractWarehouseCollateral :execrows
UPDATE warehouse SET collateral = collateral - $1 WHERE id = $2 AND collateral >= $1
`

type SubtractWarehouseCollateralParams struct {
	Collateral int64 `json:"collateral"`
	ID         int64 `json:"id"`
}

func (q *Queries) SubtractWarehouseCollateral(ctx context.Context, arg SubtractWarehouseCollateralParams) (int64, error) {
	result, err := q.db.Exec(ctx, subtractWarehouseCollateral, arg.Collateral, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const subtractWarehouseLiability = `-- name: SubtractWarehouseLiability :exec
UPDATE warehouse SET liability = liability - $1 WHERE id = $2
`

type SubtractWarehouseLiabilityParams struct {
	Liability int64 `json:"liability"`
	ID        int64 `json:"id"`
}

func (q *Queries) SubtractWarehouseLiability(ctx context.Context, arg SubtractWarehouseLiabilityParams) error {
	_, err := q.db.Exec(ctx, subtractWarehouseLiability, arg.Liability, arg.ID)
	return err
}

const updateWarehouseLiability = `-- name: UpdateWarehouseLiability :execrows
UPDATE warehouse SET liability = $1 WHERE id = $2
`

type UpdateWarehouseLiabilityParams struct {
	Liability int64 `json:"liability"`
	ID        int64 `json:"id"`
}

func (q *Queries) UpdateWarehouseLiability(ctx context.Context, arg UpdateWarehouseLiabilityParams) (int64, error) {
	result, err := q.db.Exec(ctx, updateWarehouseLiability, arg.Liability, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const updateWarehouseUserIdByUsername = `-- name: UpdateWarehouseUserIdByUsername :exec
UPDATE warehouse SET user_id = "user".id FROM "user" WHERE warehouse.id = $1 AND "user".username = $2
`

type UpdateWarehouseUserIdByUsernameParams struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

func (q *Queries) UpdateWarehouseUserIdByUsername(ctx context.Context, arg UpdateWarehouseUserIdByUsernameParams) error {
	_, err := q.db.Exec(ctx, updateWarehouseUserIdByUsername, arg.ID, arg.Username)
	return err
}
