// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: api_tokens.sql

package database

import (
	"context"
	"database/sql"
)

const createAPIToken = `-- name: CreateAPIToken :one
INSERT INTO api_tokens (
  token_id,
  user_id,
  role_id,
  name,
  ttl,
  status
) VALUES (
  $1, $2, $3, $4, $5, $6
)
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

type CreateAPITokenParams struct {
	TokenID string         `json:"token_id"`
	UserID  int64          `json:"user_id"`
	RoleID  int64          `json:"role_id"`
	Name    sql.NullString `json:"name"`
	Ttl     int64          `json:"ttl"`
	Status  TokenStatus    `json:"status"`
}

func (q *Queries) CreateAPIToken(ctx context.Context, arg CreateAPITokenParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, createAPIToken,
		arg.TokenID,
		arg.UserID,
		arg.RoleID,
		arg.Name,
		arg.Ttl,
		arg.Status,
	)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const deleteAPIToken = `-- name: DeleteAPIToken :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

func (q *Queries) DeleteAPIToken(ctx context.Context, id int64) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, deleteAPIToken, id)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const deleteUserAPIToken = `-- name: DeleteUserAPIToken :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $2
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

type DeleteUserAPITokenParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) DeleteUserAPIToken(ctx context.Context, arg DeleteUserAPITokenParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, deleteUserAPIToken, arg.ID, arg.UserID)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const deleteUserAPITokenByTokenID = `-- name: DeleteUserAPITokenByTokenID :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE token_id = $1 AND user_id = $2
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

type DeleteUserAPITokenByTokenIDParams struct {
	TokenID string `json:"token_id"`
	UserID  int64  `json:"user_id"`
}

func (q *Queries) DeleteUserAPITokenByTokenID(ctx context.Context, arg DeleteUserAPITokenByTokenIDParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, deleteUserAPITokenByTokenID, arg.TokenID, arg.UserID)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getAPIToken = `-- name: GetAPIToken :one
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
WHERE t.id = $1 AND t.deleted_at IS NULL
`

func (q *Queries) GetAPIToken(ctx context.Context, id int64) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, getAPIToken, id)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getAPITokenByTokenID = `-- name: GetAPITokenByTokenID :one
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
WHERE t.token_id = $1 AND t.deleted_at IS NULL
`

func (q *Queries) GetAPITokenByTokenID(ctx context.Context, tokenID string) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, getAPITokenByTokenID, tokenID)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getAPITokens = `-- name: GetAPITokens :many
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
WHERE t.deleted_at IS NULL
ORDER BY t.created_at DESC
`

func (q *Queries) GetAPITokens(ctx context.Context) ([]ApiToken, error) {
	rows, err := q.db.QueryContext(ctx, getAPITokens)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ApiToken
	for rows.Next() {
		var i ApiToken
		if err := rows.Scan(
			&i.ID,
			&i.TokenID,
			&i.UserID,
			&i.RoleID,
			&i.Name,
			&i.Ttl,
			&i.Status,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); 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 getUserAPIToken = `-- name: GetUserAPIToken :one
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.id = $1 AND t.user_id = $2 AND t.deleted_at IS NULL
`

type GetUserAPITokenParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) GetUserAPIToken(ctx context.Context, arg GetUserAPITokenParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, getUserAPIToken, arg.ID, arg.UserID)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getUserAPITokenByTokenID = `-- name: GetUserAPITokenByTokenID :one
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.token_id = $1 AND t.user_id = $2 AND t.deleted_at IS NULL
`

type GetUserAPITokenByTokenIDParams struct {
	TokenID string `json:"token_id"`
	UserID  int64  `json:"user_id"`
}

func (q *Queries) GetUserAPITokenByTokenID(ctx context.Context, arg GetUserAPITokenByTokenIDParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, getUserAPITokenByTokenID, arg.TokenID, arg.UserID)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getUserAPITokens = `-- name: GetUserAPITokens :many
SELECT
  t.id, t.token_id, t.user_id, t.role_id, t.name, t.ttl, t.status, t.created_at, t.updated_at, t.deleted_at
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.user_id = $1 AND t.deleted_at IS NULL
ORDER BY t.created_at DESC
`

func (q *Queries) GetUserAPITokens(ctx context.Context, userID int64) ([]ApiToken, error) {
	rows, err := q.db.QueryContext(ctx, getUserAPITokens, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ApiToken
	for rows.Next() {
		var i ApiToken
		if err := rows.Scan(
			&i.ID,
			&i.TokenID,
			&i.UserID,
			&i.RoleID,
			&i.Name,
			&i.Ttl,
			&i.Status,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); 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 updateAPIToken = `-- name: UpdateAPIToken :one
UPDATE api_tokens
SET name = $2, status = $3
WHERE id = $1
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

type UpdateAPITokenParams struct {
	ID     int64          `json:"id"`
	Name   sql.NullString `json:"name"`
	Status TokenStatus    `json:"status"`
}

func (q *Queries) UpdateAPIToken(ctx context.Context, arg UpdateAPITokenParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, updateAPIToken, arg.ID, arg.Name, arg.Status)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const updateUserAPIToken = `-- name: UpdateUserAPIToken :one
UPDATE api_tokens
SET name = $3, status = $4
WHERE id = $1 AND user_id = $2
RETURNING id, token_id, user_id, role_id, name, ttl, status, created_at, updated_at, deleted_at
`

type UpdateUserAPITokenParams struct {
	ID     int64          `json:"id"`
	UserID int64          `json:"user_id"`
	Name   sql.NullString `json:"name"`
	Status TokenStatus    `json:"status"`
}

func (q *Queries) UpdateUserAPIToken(ctx context.Context, arg UpdateUserAPITokenParams) (ApiToken, error) {
	row := q.db.QueryRowContext(ctx, updateUserAPIToken,
		arg.ID,
		arg.UserID,
		arg.Name,
		arg.Status,
	)
	var i ApiToken
	err := row.Scan(
		&i.ID,
		&i.TokenID,
		&i.UserID,
		&i.RoleID,
		&i.Name,
		&i.Ttl,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}
