-- name: CreateDataAsset :one
INSERT INTO data_assets (asset_name, asset_type, domain, application_scene,
                         data_description, data_scale, collection_method, labeling_status,
                         update_frequency, privacy_level, permission_mode, supports_validation,
                         seller_expected_price_min, seller_expected_price_max, asset_status)
VALUES ($1, $2, $3, $4,
        $5, $6, $7, $8,
        $9, $10, $11, $12,
        $13, $14, $15)
RETURNING *;


-- name: GetDataAsset :one
SELECT *
FROM data_assets
WHERE id = $1;

-- name: ListDataAssets :many
SELECT *
FROM data_assets
WHERE (
    NULLIF(sqlc.narg(keyword)::text, '') IS NULL
        OR asset_name ILIKE '%' || sqlc.narg(keyword)::text || '%'
        OR data_description ILIKE '%' || sqlc.narg(keyword)::text || '%'
    )
  AND (
    NULLIF(sqlc.narg(asset_type)::text, '') IS NULL
        OR asset_type = sqlc.narg(asset_type)::text
    )
  AND (
    NULLIF(sqlc.narg(domain)::text, '') IS NULL
        OR domain = sqlc.narg(domain)::text
    )
  AND (
    NULLIF(sqlc.narg(privacy_level)::text, '') IS NULL
        OR privacy_level = sqlc.narg(privacy_level)::text
    )
  AND (
    sqlc.narg(supports_validation)::boolean IS NULL
        OR supports_validation = sqlc.narg(supports_validation)::boolean
    )
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: GetBuyerRequest :one
SELECT *
FROM buyer_requests
WHERE id = $1;

-- name: ListBuyerRequests :many
SELECT *
FROM buyer_requests
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: GetPricingResult :one
SELECT *
FROM pricing_results
WHERE id = $1;

-- name: ListPricingResults :many
SELECT *
FROM pricing_results
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: GetValidation :one
SELECT *
FROM validations
WHERE id = $1;

-- name: ListValidations :many
SELECT *
FROM validations
ORDER BY validation_created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: GetOrder :one
SELECT *
FROM orders
WHERE id = $1;

-- name: ListOrders :many
SELECT *
FROM orders
ORDER BY order_created_at DESC, id DESC
LIMIT $1 OFFSET $2;
