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

package db

import (
	"context"
)

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

type DeleteDeviceParams struct {
	UserID string `json:"user_id"`
	Name   string `json:"name"`
}

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

const deleteOrphanBrowserDevices = `-- name: DeleteOrphanBrowserDevices :execrows
DELETE FROM devices
WHERE type = 'browser'
  AND NOT EXISTS (
    SELECT 1 FROM web_sessions ws
    WHERE ws.user_id = devices.user_id
      AND ws.device_name = devices.name
      AND ws.expires_at > ?
  )
`

// DeleteOrphanBrowserDevices removes browser device rows with no live web_session
// (the session was revoked or expired), keeping the device list aligned with the
// session list. The ? is the current epoch second. Native (macos/windows/linux/ios)
// and shortcut devices are left alone: they legitimately keep no web_session.
func (q *Queries) DeleteOrphanBrowserDevices(ctx context.Context, expiresAt int64) (int64, error) {
	result, err := q.db.ExecContext(ctx, deleteOrphanBrowserDevices, expiresAt)
	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 listDevicesByUser = `-- name: ListDevicesByUser :many
SELECT user_id, name, type, 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.UserID,
			&i.Name,
			&i.Type,
			&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 upsertDevice = `-- name: UpsertDevice :exec
INSERT INTO devices (user_id, name, type, last_seen)
VALUES (?, ?, ?, ?)
ON CONFLICT (user_id, name) DO UPDATE SET
    type = excluded.type,
    last_seen = excluded.last_seen
`

type UpsertDeviceParams struct {
	UserID   string `json:"user_id"`
	Name     string `json:"name"`
	Type     string `json:"type"`
	LastSeen int64  `json:"last_seen"`
}

func (q *Queries) UpsertDevice(ctx context.Context, arg UpsertDeviceParams) error {
	_, err := q.db.ExecContext(ctx, upsertDevice,
		arg.UserID,
		arg.Name,
		arg.Type,
		arg.LastSeen,
	)
	return err
}
