-- name: GetLoopOutSwaps :many
SELECT
    swaps.*,
    loopout_swaps.*,
    htlc_keys.*,
    loopout_swaps_asset_info.*
FROM
    swaps
JOIN
    loopout_swaps ON swaps.swap_hash = loopout_swaps.swap_hash
JOIN
    htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
LEFT JOIN
    loopout_swaps_asset_info ON swaps.swap_hash = loopout_swaps_asset_info.swap_hash
ORDER BY
    swaps.id;

-- name: GetLoopOutSwap :one
SELECT
    swaps.*,
    loopout_swaps.*,
    htlc_keys.*,
    loopout_swaps_asset_info.*
FROM
    swaps
JOIN
    loopout_swaps ON swaps.swap_hash = loopout_swaps.swap_hash
JOIN
    htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
LEFT JOIN
    loopout_swaps_asset_info ON swaps.swap_hash = loopout_swaps_asset_info.swap_hash
WHERE
    swaps.swap_hash = $1;

-- name: GetLoopInSwaps :many
SELECT
    swaps.*,
    loopin_swaps.*,
    htlc_keys.*
FROM
    swaps
JOIN
    loopin_swaps ON swaps.swap_hash = loopin_swaps.swap_hash
JOIN
    htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
ORDER BY
    swaps.id;

-- name: GetLoopInSwap :one
SELECT
    swaps.*,
    loopin_swaps.*,
    htlc_keys.*
FROM
    swaps
JOIN
    loopin_swaps ON swaps.swap_hash = loopin_swaps.swap_hash
JOIN
    htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
WHERE
    swaps.swap_hash = $1;

-- name: GetSwapUpdates :many
SELECT
    *
FROM
    swap_updates
WHERE
    swap_hash = $1
ORDER BY
    id ASC;

-- name: InsertSwap :exec
INSERT INTO swaps (
    swap_hash,
    preimage,
    initiation_time,
    amount_requested,
    cltv_expiry,
    max_miner_fee,
    max_swap_fee,
    initiation_height,
    protocol_version,
    label
) VALUES (
     $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
);

-- name: InsertSwapUpdate :exec
INSERT INTO swap_updates (
    swap_hash,
    update_timestamp,
    update_state,
    htlc_txhash,
    server_cost,
    onchain_cost,
    offchain_cost
) VALUES (
    $1, $2, $3, $4, $5, $6, $7
);

-- name: InsertLoopOut :exec
INSERT INTO loopout_swaps (
    swap_hash,
    dest_address,
    swap_invoice,
    max_swap_routing_fee,
    sweep_conf_target,
    htlc_confirmations,
    outgoing_chan_set,
    prepay_invoice,
    max_prepay_routing_fee,
    publication_deadline,
    single_sweep,
    payment_timeout
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
);

-- name: InsertLoopOutAsset :exec
INSERT INTO loopout_swaps_asset_info (
    swap_hash,
    asset_id,
    swap_rfq_id,
    prepay_rfq_id
) VALUES (
    $1, $2, $3, $4
);

-- name: UpdateLoopOutAssetOffchainPayments :exec
UPDATE loopout_swaps_asset_info
SET
    asset_amt_paid_swap = $2,
    asset_amt_paid_prepay = $3
WHERE swap_hash = $1;

-- name: InsertLoopIn :exec
INSERT INTO loopin_swaps (
    swap_hash,
    htlc_conf_target,
    last_hop,
    external_htlc
) VALUES (
    $1, $2, $3, $4
);

-- name: InsertHtlcKeys :exec
INSERT INTO htlc_keys(
    swap_hash,
    sender_script_pubkey,
    receiver_script_pubkey,
    sender_internal_pubkey,
    receiver_internal_pubkey,
    client_key_family,
    client_key_index
) VALUES (
    $1, $2, $3, $4, $5, $6, $7
);

-- name: GetLastUpdateID :one
SELECT id
FROM swap_updates
WHERE swap_hash = $1
ORDER BY update_timestamp DESC
LIMIT 1;

-- name: OverrideSwapCosts :exec
UPDATE swap_updates
SET 
    server_cost = $2,
    onchain_cost = $3,
    offchain_cost = $4
WHERE id = $1;

