// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: sessions.sql

package dbgen

import (
	"context"
	"database/sql"
)

const deleteExpiredSessions = `-- name: DeleteExpiredSessions :exec
DELETE FROM sessions WHERE strftime('%s', expires_at) < strftime('%s', 'now')
`

func (q *Queries) DeleteExpiredSessions(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, deleteExpiredSessions)
	return err
}

const deleteOtherSessions = `-- name: DeleteOtherSessions :execresult
DELETE FROM sessions WHERE user_id = ? AND id != ?
`

type DeleteOtherSessionsParams struct {
	UserID int64 `json:"userId"`
	ID     int64 `json:"id"`
}

func (q *Queries) DeleteOtherSessions(ctx context.Context, arg DeleteOtherSessionsParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteOtherSessions, arg.UserID, arg.ID)
}

const deleteSessionByID = `-- name: DeleteSessionByID :execresult
DELETE FROM sessions WHERE id = ? AND user_id = ?
`

type DeleteSessionByIDParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"userId"`
}

func (q *Queries) DeleteSessionByID(ctx context.Context, arg DeleteSessionByIDParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteSessionByID, arg.ID, arg.UserID)
}

const deleteSessionByToken = `-- name: DeleteSessionByToken :exec
DELETE FROM sessions WHERE token = ?
`

func (q *Queries) DeleteSessionByToken(ctx context.Context, token string) error {
	_, err := q.db.ExecContext(ctx, deleteSessionByToken, token)
	return err
}

const evictOldestSessions = `-- name: EvictOldestSessions :exec
DELETE FROM sessions WHERE id IN (
    SELECT s2.id FROM sessions AS s2 WHERE s2.user_id = ?
    ORDER BY s2.created_at DESC
    LIMIT -1 OFFSET ?
)
`

type EvictOldestSessionsParams struct {
	UserID int64 `json:"userId"`
	Offset int64 `json:"offset"`
}

func (q *Queries) EvictOldestSessions(ctx context.Context, arg EvictOldestSessionsParams) error {
	_, err := q.db.ExecContext(ctx, evictOldestSessions, arg.UserID, arg.Offset)
	return err
}

const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one
SELECT id, user_id, token, device, ip_address, created_at, last_used, expires_at
FROM sessions WHERE token = ?
`

func (q *Queries) GetSessionByTokenHash(ctx context.Context, token string) (Session, error) {
	row := q.db.QueryRowContext(ctx, getSessionByTokenHash, token)
	var i Session
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Token,
		&i.Device,
		&i.IpAddress,
		&i.CreatedAt,
		&i.LastUsed,
		&i.ExpiresAt,
	)
	return i, err
}

const getSessionWithBanStatus = `-- name: GetSessionWithBanStatus :one
SELECT s.id, s.user_id, s.token, s.device, s.ip_address,
       s.created_at, s.last_used, s.expires_at,
       u.banned, u.ban_reason, u.ban_expires
FROM sessions s
JOIN users u ON s.user_id = u.id
WHERE s.token = ?
`

type GetSessionWithBanStatusRow struct {
	ID         int64   `json:"id"`
	UserID     int64   `json:"userId"`
	Token      string  `json:"token"`
	Device     *string `json:"device"`
	IpAddress  *string `json:"ipAddress"`
	CreatedAt  string  `json:"createdAt"`
	LastUsed   string  `json:"lastUsed"`
	ExpiresAt  string  `json:"expiresAt"`
	Banned     int64   `json:"banned"`
	BanReason  *string `json:"banReason"`
	BanExpires *string `json:"banExpires"`
}

func (q *Queries) GetSessionWithBanStatus(ctx context.Context, token string) (GetSessionWithBanStatusRow, error) {
	row := q.db.QueryRowContext(ctx, getSessionWithBanStatus, token)
	var i GetSessionWithBanStatusRow
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Token,
		&i.Device,
		&i.IpAddress,
		&i.CreatedAt,
		&i.LastUsed,
		&i.ExpiresAt,
		&i.Banned,
		&i.BanReason,
		&i.BanExpires,
	)
	return i, err
}

const insertSession = `-- name: InsertSession :execresult
INSERT INTO sessions (user_id, token, device, ip_address, expires_at)
VALUES (?, ?, ?, ?, ?)
`

type InsertSessionParams struct {
	UserID    int64   `json:"userId"`
	Token     string  `json:"token"`
	Device    *string `json:"device"`
	IpAddress *string `json:"ipAddress"`
	ExpiresAt string  `json:"expiresAt"`
}

func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, insertSession,
		arg.UserID,
		arg.Token,
		arg.Device,
		arg.IpAddress,
		arg.ExpiresAt,
	)
}

const listUserSessions = `-- name: ListUserSessions :many
SELECT id, user_id, token, device, ip_address, created_at, last_used, expires_at
FROM sessions
WHERE user_id = ?
ORDER BY created_at DESC
`

func (q *Queries) ListUserSessions(ctx context.Context, userID int64) ([]Session, error) {
	rows, err := q.db.QueryContext(ctx, listUserSessions, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Session{}
	for rows.Next() {
		var i Session
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Token,
			&i.Device,
			&i.IpAddress,
			&i.CreatedAt,
			&i.LastUsed,
			&i.ExpiresAt,
		); 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 touchSession = `-- name: TouchSession :exec
UPDATE sessions SET last_used = datetime('now') WHERE token = ?
`

func (q *Queries) TouchSession(ctx context.Context, token string) error {
	_, err := q.db.ExecContext(ctx, touchSession, token)
	return err
}
