-- name: CreateTransferSession :exec
INSERT INTO transfer_sessions (
    id, user_id, sender_name, receiver_name, state,
    file_name, file_size, file_sha256, created_at
)
VALUES (?, ?, ?, ?, 'PENDING', ?, ?, ?, ?);

-- name: GetTransferSession :one
SELECT id, user_id, sender_name, receiver_name, state, mode,
       file_name, file_size, file_sha256, bytes_transferred,
       created_at, finished_at, fail_reason
FROM transfer_sessions
WHERE id = ?;

-- name: UpdateTransferState :execrows
UPDATE transfer_sessions
SET state             = sqlc.arg('state'),
    mode              = COALESCE(sqlc.narg('mode'),              mode),
    fail_reason       = COALESCE(sqlc.narg('fail_reason'),       fail_reason),
    finished_at       = COALESCE(sqlc.narg('finished_at'),       finished_at),
    bytes_transferred = COALESCE(sqlc.narg('bytes_transferred'), bytes_transferred)
WHERE id = sqlc.arg('id');

-- name: ExpirePendingSessions :many
UPDATE transfer_sessions
SET state       = 'CANCELLED',
    fail_reason = 'pending_timeout',
    finished_at = sqlc.arg('finished_at')
WHERE state = 'PENDING' AND created_at < sqlc.arg('cutoff')
RETURNING id, user_id, sender_name, receiver_name;
