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

package dbgen

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const CreateDevice = `-- name: CreateDevice :one
INSERT INTO devices (
    user_id,
    device_token,
    platform
)
VALUES (
    $1, $2, $3
)
ON CONFLICT (user_id, device_token)
DO UPDATE SET
    is_active = true,
    last_seen = CURRENT_TIMESTAMP
RETURNING id, user_id, device_token, platform, is_active, last_seen, created_at
`

type CreateDeviceParams struct {
	UserID      int64       `json:"user_id"`
	DeviceToken string      `json:"device_token"`
	Platform    pgtype.Text `json:"platform"`
}

func (q *Queries) CreateDevice(ctx context.Context, arg CreateDeviceParams) (Device, error) {
	row := q.db.QueryRow(ctx, CreateDevice, arg.UserID, arg.DeviceToken, arg.Platform)
	var i Device
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.DeviceToken,
		&i.Platform,
		&i.IsActive,
		&i.LastSeen,
		&i.CreatedAt,
	)
	return i, err
}

const DeactivateDevice = `-- name: DeactivateDevice :exec
UPDATE devices
SET is_active = false
WHERE id = $1
`

func (q *Queries) DeactivateDevice(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, DeactivateDevice, id)
	return err
}

const DeactivateDeviceByToken = `-- name: DeactivateDeviceByToken :exec
UPDATE devices
SET is_active = false
WHERE user_id = $1 AND device_token = $2
`

type DeactivateDeviceByTokenParams struct {
	UserID      int64  `json:"user_id"`
	DeviceToken string `json:"device_token"`
}

func (q *Queries) DeactivateDeviceByToken(ctx context.Context, arg DeactivateDeviceByTokenParams) error {
	_, err := q.db.Exec(ctx, DeactivateDeviceByToken, arg.UserID, arg.DeviceToken)
	return err
}

const DeactivateUserDevices = `-- name: DeactivateUserDevices :exec
UPDATE devices
SET is_active = false
WHERE user_id = $1
`

func (q *Queries) DeactivateUserDevices(ctx context.Context, userID int64) error {
	_, err := q.db.Exec(ctx, DeactivateUserDevices, userID)
	return err
}

const GetActiveDeviceTokens = `-- name: GetActiveDeviceTokens :many
SELECT device_token
FROM devices
WHERE user_id = $1 AND is_active = true AND platform IN ('android', 'ios')
`

func (q *Queries) GetActiveDeviceTokens(ctx context.Context, userID int64) ([]string, error) {
	rows, err := q.db.Query(ctx, GetActiveDeviceTokens, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []string
	for rows.Next() {
		var device_token string
		if err := rows.Scan(&device_token); err != nil {
			return nil, err
		}
		items = append(items, device_token)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetUserDevices = `-- name: GetUserDevices :many
SELECT id, user_id, device_token, platform, is_active, last_seen, created_at
FROM devices
WHERE user_id = $1 AND is_active = true
`

func (q *Queries) GetUserDevices(ctx context.Context, userID int64) ([]Device, error) {
	rows, err := q.db.Query(ctx, GetUserDevices, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Device
	for rows.Next() {
		var i Device
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.DeviceToken,
			&i.Platform,
			&i.IsActive,
			&i.LastSeen,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateDeviceLastSeen = `-- name: UpdateDeviceLastSeen :exec
UPDATE devices
SET last_seen = CURRENT_TIMESTAMP
WHERE id = $1
`

func (q *Queries) UpdateDeviceLastSeen(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, UpdateDeviceLastSeen, id)
	return err
}
