-- 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,
    quality_level,
    scarcity_level,
    base_value_score,
    base_price_min,
    base_price_max,
    asset_status
)
VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
    $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
)
RETURNING id, 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, quality_level, scarcity_level, base_value_score, base_price_min, base_price_max, asset_status, created_at, updated_at;

-- name: CountDataAssets :one
SELECT COUNT(*)
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
    );

-- name: GetDataAsset :one
SELECT id, 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, quality_level, scarcity_level, base_value_score, base_price_min, base_price_max, asset_status, created_at, updated_at
FROM data_assets
WHERE id = $1;

-- name: ListDataAssets :many
SELECT id, 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, quality_level, scarcity_level, base_value_score, base_price_min, base_price_max, asset_status, created_at, updated_at
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: UpdateDataAssetStatus :one
UPDATE data_assets
SET asset_status = $2,
    updated_at = now()
WHERE id = $1
RETURNING id, 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, quality_level, scarcity_level, base_value_score, base_price_min, base_price_max, asset_status, created_at, updated_at;

-- name: CreateBuyerRequest :one
INSERT INTO buyer_requests (
    asset_id,
    task_type,
    model_type,
    buyer_budget_min,
    buyer_budget_max,
    privacy_requirement,
    usage_purpose,
    request_note,
    request_status
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, asset_id, task_type, model_type, buyer_budget_min, buyer_budget_max, privacy_requirement, usage_purpose, request_note, request_status, created_at, updated_at;

-- name: GetBuyerRequest :one
SELECT id, asset_id, task_type, model_type, buyer_budget_min, buyer_budget_max, privacy_requirement, usage_purpose, request_note, request_status, created_at, updated_at
FROM buyer_requests
WHERE id = $1;

-- name: ListBuyerRequests :many
SELECT id, asset_id, task_type, model_type, buyer_budget_min, buyer_budget_max, privacy_requirement, usage_purpose, request_note, request_status, created_at, updated_at
FROM buyer_requests
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: CreatePricingResult :one
INSERT INTO pricing_results (
    asset_id,
    request_id,
    scenario_value_score,
    scenario_price_min,
    scenario_price_max,
    suggested_price,
    success_probability,
    pricing_reason_1,
    pricing_reason_2,
    pricing_reason_3,
    verification_suggestion,
    pricing_status
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING id, asset_id, request_id, scenario_value_score, scenario_price_min, scenario_price_max, suggested_price, success_probability, pricing_reason_1, pricing_reason_2, pricing_reason_3, verification_suggestion, pricing_status, created_at, updated_at;

-- name: GetPricingResult :one
SELECT id, asset_id, request_id, scenario_value_score, scenario_price_min, scenario_price_max, suggested_price, success_probability, pricing_reason_1, pricing_reason_2, pricing_reason_3, verification_suggestion, pricing_status, created_at, updated_at
FROM pricing_results
WHERE id = $1;

-- name: ListPricingResults :many
SELECT id, asset_id, request_id, scenario_value_score, scenario_price_min, scenario_price_max, suggested_price, success_probability, pricing_reason_1, pricing_reason_2, pricing_reason_3, verification_suggestion, pricing_status, created_at, updated_at
FROM pricing_results
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: CreateValidation :one
INSERT INTO validations (
    asset_id,
    request_id,
    validation_type,
    validation_requested,
    validation_status,
    validation_signal,
    validation_score,
    risk_warning,
    continue_recommendation,
    validation_finished_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, asset_id, request_id, validation_type, validation_requested, validation_status, validation_signal, validation_score, risk_warning, continue_recommendation, validation_created_at, validation_finished_at;

-- name: UpdateValidationResult :one
UPDATE validations
SET validation_status = $2,
    validation_signal = $3,
    validation_score = $4,
    risk_warning = $5,
    continue_recommendation = $6,
    validation_finished_at = $7
WHERE id = $1
RETURNING id, asset_id, request_id, validation_type, validation_requested, validation_status, validation_signal, validation_score, risk_warning, continue_recommendation, validation_created_at, validation_finished_at;

-- name: GetValidation :one
SELECT id, asset_id, request_id, validation_type, validation_requested, validation_status, validation_signal, validation_score, risk_warning, continue_recommendation, validation_created_at, validation_finished_at
FROM validations
WHERE id = $1;

-- name: ListValidations :many
SELECT id, asset_id, request_id, validation_type, validation_requested, validation_status, validation_signal, validation_score, risk_warning, continue_recommendation, validation_created_at, validation_finished_at
FROM validations
ORDER BY validation_created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: CountValidations :one
SELECT COUNT(*)
FROM validations;

-- name: CreateOrder :one
INSERT INTO orders (
    asset_id,
    request_id,
    pricing_id,
    validation_id,
    asset_name,
    current_price,
    negotiation_min,
    negotiation_max,
    validation_used,
    delivery_mode,
    order_status
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
RETURNING id, asset_id, request_id, pricing_id, validation_id, asset_name, current_price, negotiation_min, negotiation_max, validation_used, delivery_mode, order_status, order_created_at, order_updated_at;

-- name: GetOrder :one
SELECT id, asset_id, request_id, pricing_id, validation_id, asset_name, current_price, negotiation_min, negotiation_max, validation_used, delivery_mode, order_status, order_created_at, order_updated_at
FROM orders
WHERE id = $1;

-- name: ListOrders :many
SELECT id, asset_id, request_id, pricing_id, validation_id, asset_name, current_price, negotiation_min, negotiation_max, validation_used, delivery_mode, order_status, order_created_at, order_updated_at
FROM orders
WHERE NULLIF(sqlc.narg(order_status)::text, '') IS NULL
   OR order_status = sqlc.narg(order_status)::text
ORDER BY order_created_at DESC, id DESC
LIMIT $1 OFFSET $2;

-- name: CountOrders :one
SELECT COUNT(*)
FROM orders
WHERE NULLIF(sqlc.narg(order_status)::text, '') IS NULL
   OR order_status = sqlc.narg(order_status)::text;

-- name: UpdateOrderStatus :one
UPDATE orders
SET order_status = $2,
    order_updated_at = now()
WHERE id = $1
RETURNING id, asset_id, request_id, pricing_id, validation_id, asset_name, current_price, negotiation_min, negotiation_max, validation_used, delivery_mode, order_status, order_created_at, order_updated_at;
