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

package db

import (
	"context"
)

const createDevice = `-- name: CreateDevice :exec

INSERT INTO devices (device_id, user_id, name, type, tier, broker_sid, created_at, last_seen)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (device_id) DO UPDATE SET
    name = excluded.name,
    type = excluded.type,
    tier = excluded.tier,
    broker_sid = excluded.broker_sid,
    last_seen = excluded.last_seen
WHERE devices.user_id = excluded.user_id
`

type CreateDeviceParams struct {
	DeviceID  string `json:"device_id"`
	UserID    string `json:"user_id"`
	Name      string `json:"name"`
	Type      string `json:"type"`
	Tier      string `json:"tier"`
	BrokerSid string `json:"broker_sid"`
	CreatedAt int64  `json:"created_at"`
	LastSeen  int64  `json:"last_seen"`
}

// devices: cdrop-managed devices (scan-login / native pairing). device_id is a
// stable opaque cdrop-generated id that doubles as the session<->device join key
// (passed to the broker as meta, echoed back as X-Auth-Meta on /verify). broker_sid
// is the broker's session id, stored privately to revoke on device removal. tier is
// a redundant cache of the broker scope (full/guest). name is human-readable and can
// change without changing the device's identity (device_id is the key).
//
// ASCII only: sqlc 1.31.x miscomputes byte offsets on multibyte comments and corrupts
// every generated SQL const in the file. Keep this file pure ASCII.
// CreateDevice records a device on scan-login collect / proxy-mint (or native pairing). Keyed
// on device_id, so a re-pair with the same id refreshes the row (incl. the new broker_sid).
// The WHERE on the upsert scopes the update to the owning user so a (cryptographically
// impossible) cross-user device_id collision can never reassign the row owner; user_id is
// immutable for a given device_id.
func (q *Queries) CreateDevice(ctx context.Context, arg CreateDeviceParams) error {
	_, err := q.db.ExecContext(ctx, createDevice,
		arg.DeviceID,
		arg.UserID,
		arg.Name,
		arg.Type,
		arg.Tier,
		arg.BrokerSid,
		arg.CreatedAt,
		arg.LastSeen,
	)
	return err
}

const deleteDevice = `-- name: DeleteDevice :execrows
DELETE FROM devices
WHERE device_id = ? AND user_id = ?
`

type DeleteDeviceParams struct {
	DeviceID string `json:"device_id"`
	UserID   string `json:"user_id"`
}

func (q *Queries) DeleteDevice(ctx context.Context, arg DeleteDeviceParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, deleteDevice, arg.DeviceID, arg.UserID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const deleteStaleDevices = `-- name: DeleteStaleDevices :exec
DELETE FROM devices
WHERE last_seen < ?
`

func (q *Queries) DeleteStaleDevices(ctx context.Context, lastSeen int64) error {
	_, err := q.db.ExecContext(ctx, deleteStaleDevices, lastSeen)
	return err
}

const getDevice = `-- name: GetDevice :one
SELECT device_id, user_id, name, type, tier, broker_sid, created_at, last_seen
FROM devices
WHERE device_id = ?
`

func (q *Queries) GetDevice(ctx context.Context, deviceID string) (Device, error) {
	row := q.db.QueryRowContext(ctx, getDevice, deviceID)
	var i Device
	err := row.Scan(
		&i.DeviceID,
		&i.UserID,
		&i.Name,
		&i.Type,
		&i.Tier,
		&i.BrokerSid,
		&i.CreatedAt,
		&i.LastSeen,
	)
	return i, err
}

const listDevicesByUser = `-- name: ListDevicesByUser :many
SELECT device_id, user_id, name, type, tier, broker_sid, created_at, last_seen
FROM devices
WHERE user_id = ?
ORDER BY name
`

func (q *Queries) ListDevicesByUser(ctx context.Context, userID string) ([]Device, error) {
	rows, err := q.db.QueryContext(ctx, listDevicesByUser, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Device
	for rows.Next() {
		var i Device
		if err := rows.Scan(
			&i.DeviceID,
			&i.UserID,
			&i.Name,
			&i.Type,
			&i.Tier,
			&i.BrokerSid,
			&i.CreatedAt,
			&i.LastSeen,
		); 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 renameDevice = `-- name: RenameDevice :execrows
UPDATE devices SET name = ?
WHERE device_id = ? AND user_id = ?
`

type RenameDeviceParams struct {
	Name     string `json:"name"`
	DeviceID string `json:"device_id"`
	UserID   string `json:"user_id"`
}

func (q *Queries) RenameDevice(ctx context.Context, arg RenameDeviceParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, renameDevice, arg.Name, arg.DeviceID, arg.UserID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const touchDevice = `-- name: TouchDevice :exec
UPDATE devices SET last_seen = ?, tier = ? WHERE device_id = ? AND user_id = ?
`

type TouchDeviceParams struct {
	LastSeen int64  `json:"last_seen"`
	Tier     string `json:"tier"`
	DeviceID string `json:"device_id"`
	UserID   string `json:"user_id"`
}

// TouchDevice refreshes last_seen + tier on each authenticated request. Update-only:
// the row is created at collect time, so a missing row (e.g. a device authorized via
// the broker's own device flow, not cdrop's) simply isn't cdrop-managed and no-ops.
func (q *Queries) TouchDevice(ctx context.Context, arg TouchDeviceParams) error {
	_, err := q.db.ExecContext(ctx, touchDevice,
		arg.LastSeen,
		arg.Tier,
		arg.DeviceID,
		arg.UserID,
	)
	return err
}
