// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: transfers.sql

package db

import (
	"context"
)

const createTransferSession = `-- 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', ?, ?, ?, ?)
`

type CreateTransferSessionParams struct {
	ID           string  `json:"id"`
	UserID       string  `json:"user_id"`
	SenderName   string  `json:"sender_name"`
	ReceiverName string  `json:"receiver_name"`
	FileName     string  `json:"file_name"`
	FileSize     int64   `json:"file_size"`
	FileSha256   *string `json:"file_sha256"`
	CreatedAt    int64   `json:"created_at"`
}

func (q *Queries) CreateTransferSession(ctx context.Context, arg CreateTransferSessionParams) error {
	_, err := q.db.ExecContext(ctx, createTransferSession,
		arg.ID,
		arg.UserID,
		arg.SenderName,
		arg.ReceiverName,
		arg.FileName,
		arg.FileSize,
		arg.FileSha256,
		arg.CreatedAt,
	)
	return err
}

const expirePendingSessions = `-- name: ExpirePendingSessions :many
UPDATE transfer_sessions
SET state       = 'CANCELLED',
    fail_reason = 'pending_timeout',
    finished_at = ?1
WHERE state = 'PENDING' AND created_at < ?2
RETURNING id, user_id, sender_name, receiver_name
`

type ExpirePendingSessionsParams struct {
	FinishedAt *int64 `json:"finished_at"`
	Cutoff     int64  `json:"cutoff"`
}

type ExpirePendingSessionsRow struct {
	ID           string `json:"id"`
	UserID       string `json:"user_id"`
	SenderName   string `json:"sender_name"`
	ReceiverName string `json:"receiver_name"`
}

func (q *Queries) ExpirePendingSessions(ctx context.Context, arg ExpirePendingSessionsParams) ([]ExpirePendingSessionsRow, error) {
	rows, err := q.db.QueryContext(ctx, expirePendingSessions, arg.FinishedAt, arg.Cutoff)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ExpirePendingSessionsRow
	for rows.Next() {
		var i ExpirePendingSessionsRow
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.SenderName,
			&i.ReceiverName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getTransferSession = `-- 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 = ?
`

func (q *Queries) GetTransferSession(ctx context.Context, id string) (TransferSession, error) {
	row := q.db.QueryRowContext(ctx, getTransferSession, id)
	var i TransferSession
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.SenderName,
		&i.ReceiverName,
		&i.State,
		&i.Mode,
		&i.FileName,
		&i.FileSize,
		&i.FileSha256,
		&i.BytesTransferred,
		&i.CreatedAt,
		&i.FinishedAt,
		&i.FailReason,
	)
	return i, err
}

const updateTransferState = `-- name: UpdateTransferState :execrows
UPDATE transfer_sessions
SET state             = ?1,
    mode              = COALESCE(?2,              mode),
    fail_reason       = COALESCE(?3,       fail_reason),
    finished_at       = COALESCE(?4,       finished_at),
    bytes_transferred = COALESCE(?5, bytes_transferred)
WHERE id = ?6
`

type UpdateTransferStateParams struct {
	State            string  `json:"state"`
	Mode             *string `json:"mode"`
	FailReason       *string `json:"fail_reason"`
	FinishedAt       *int64  `json:"finished_at"`
	BytesTransferred *int64  `json:"bytes_transferred"`
	ID               string  `json:"id"`
}

func (q *Queries) UpdateTransferState(ctx context.Context, arg UpdateTransferStateParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, updateTransferState,
		arg.State,
		arg.Mode,
		arg.FailReason,
		arg.FinishedAt,
		arg.BytesTransferred,
		arg.ID,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
