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

package dbgen

import (
	"context"
	"database/sql"
)

const applyVoiceServerDeafen = `-- name: ApplyVoiceServerDeafen :execresult
UPDATE voice_states SET server_deafened = 1, deafened = 1 WHERE user_id = ? AND channel_id = ?
`

type ApplyVoiceServerDeafenParams struct {
	UserID    int64 `json:"userId"`
	ChannelID int64 `json:"channelId"`
}

func (q *Queries) ApplyVoiceServerDeafen(ctx context.Context, arg ApplyVoiceServerDeafenParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, applyVoiceServerDeafen, arg.UserID, arg.ChannelID)
}

const applyVoiceServerMute = `-- name: ApplyVoiceServerMute :execresult

UPDATE voice_states SET server_muted = 1, muted = 1 WHERE user_id = ? AND channel_id = ?
`

type ApplyVoiceServerMuteParams struct {
	UserID    int64 `json:"userId"`
	ChannelID int64 `json:"channelId"`
}

// Scoped to channel_id as well as user_id: the moderator's authorization is
// checked against a channel snapshot several round trips before this write
// lands, so an unscoped `WHERE user_id = ?` would follow the target onto
// whatever channel their row points at by then -- including a DM call the
// moderator was never authorized against (OC-0005). :execresult so the
// caller can tell a real no-op (target moved) from a normal apply.
func (q *Queries) ApplyVoiceServerMute(ctx context.Context, arg ApplyVoiceServerMuteParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, applyVoiceServerMute, arg.UserID, arg.ChannelID)
}

const clearAllVoiceStates = `-- name: ClearAllVoiceStates :exec
DELETE FROM voice_states
`

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

const clearVoiceServerDeafen = `-- name: ClearVoiceServerDeafen :execresult
UPDATE voice_states SET server_deafened = 0 WHERE user_id = ? AND channel_id = ?
`

type ClearVoiceServerDeafenParams struct {
	UserID    int64 `json:"userId"`
	ChannelID int64 `json:"channelId"`
}

func (q *Queries) ClearVoiceServerDeafen(ctx context.Context, arg ClearVoiceServerDeafenParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, clearVoiceServerDeafen, arg.UserID, arg.ChannelID)
}

const clearVoiceServerMute = `-- name: ClearVoiceServerMute :execresult
UPDATE voice_states SET server_muted = 0 WHERE user_id = ? AND channel_id = ?
`

type ClearVoiceServerMuteParams struct {
	UserID    int64 `json:"userId"`
	ChannelID int64 `json:"channelId"`
}

func (q *Queries) ClearVoiceServerMute(ctx context.Context, arg ClearVoiceServerMuteParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, clearVoiceServerMute, arg.UserID, arg.ChannelID)
}

const clearVoiceState = `-- name: ClearVoiceState :exec
DELETE FROM voice_states WHERE user_id = ?
`

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

const countActiveCameras = `-- name: CountActiveCameras :one
SELECT COUNT(*) FROM voice_states WHERE channel_id = ? AND camera = 1
`

func (q *Queries) CountActiveCameras(ctx context.Context, channelID int64) (int64, error) {
	row := q.db.QueryRowContext(ctx, countActiveCameras, channelID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const enableCameraIfUnderLimit = `-- name: EnableCameraIfUnderLimit :execresult

UPDATE voice_states SET camera = 1
WHERE voice_states.user_id = ? AND voice_states.channel_id = ?
  AND (SELECT COALESCE(SUM(vs2.camera), 0) + COALESCE(SUM(vs2.screenshare), 0) FROM voice_states AS vs2 WHERE vs2.channel_id = ?) < ?4
`

type EnableCameraIfUnderLimitParams struct {
	UserID      int64 `json:"userId"`
	ChannelID   int64 `json:"channelId"`
	ChannelID_2 int64 `json:"channelId2"`
	MaxVideo    int64 `json:"maxVideo"`
}

// Camera and screenshare share one voice_max_video budget, counted in
// STREAMS, not rows: a channel capped at N simultaneous video streams must
// not let a camera publish ignore screenshare occupants (or vice versa,
// OC-0023), and a single user with both flags set must consume two of the N
// slots, not one (OC-0006) -- so both gates sum `vs2.camera + vs2.screenshare`
// across the channel's rows rather than counting rows where either is set.
// The enabling user's own bit is still 0 at gate time, so no self-exclusion
// term is needed.
func (q *Queries) EnableCameraIfUnderLimit(ctx context.Context, arg EnableCameraIfUnderLimitParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, enableCameraIfUnderLimit,
		arg.UserID,
		arg.ChannelID,
		arg.ChannelID_2,
		arg.MaxVideo,
	)
}

const enableScreenshareIfUnderLimit = `-- name: EnableScreenshareIfUnderLimit :execresult
UPDATE voice_states SET screenshare = 1
WHERE voice_states.user_id = ? AND voice_states.channel_id = ?
  AND (SELECT COALESCE(SUM(vs2.camera), 0) + COALESCE(SUM(vs2.screenshare), 0) FROM voice_states AS vs2 WHERE vs2.channel_id = ?) < ?4
`

type EnableScreenshareIfUnderLimitParams struct {
	UserID      int64 `json:"userId"`
	ChannelID   int64 `json:"channelId"`
	ChannelID_2 int64 `json:"channelId2"`
	MaxVideo    int64 `json:"maxVideo"`
}

func (q *Queries) EnableScreenshareIfUnderLimit(ctx context.Context, arg EnableScreenshareIfUnderLimitParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, enableScreenshareIfUnderLimit,
		arg.UserID,
		arg.ChannelID,
		arg.ChannelID_2,
		arg.MaxVideo,
	)
}

const getAllVoiceStates = `-- name: GetAllVoiceStates :many
SELECT vs.user_id, vs.channel_id, u.username,
       vs.muted, vs.deafened, vs.speaking,
       vs.camera, vs.screenshare,
       vs.server_muted, vs.server_deafened, vs.joined_at
FROM voice_states vs
JOIN users u ON u.id = vs.user_id
ORDER BY vs.channel_id, vs.joined_at ASC
`

type GetAllVoiceStatesRow struct {
	UserID         int64  `json:"userId"`
	ChannelID      int64  `json:"channelId"`
	Username       string `json:"username"`
	Muted          int64  `json:"muted"`
	Deafened       int64  `json:"deafened"`
	Speaking       int64  `json:"speaking"`
	Camera         int64  `json:"camera"`
	Screenshare    int64  `json:"screenshare"`
	ServerMuted    int64  `json:"serverMuted"`
	ServerDeafened int64  `json:"serverDeafened"`
	JoinedAt       string `json:"joinedAt"`
}

func (q *Queries) GetAllVoiceStates(ctx context.Context) ([]GetAllVoiceStatesRow, error) {
	rows, err := q.db.QueryContext(ctx, getAllVoiceStates)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetAllVoiceStatesRow{}
	for rows.Next() {
		var i GetAllVoiceStatesRow
		if err := rows.Scan(
			&i.UserID,
			&i.ChannelID,
			&i.Username,
			&i.Muted,
			&i.Deafened,
			&i.Speaking,
			&i.Camera,
			&i.Screenshare,
			&i.ServerMuted,
			&i.ServerDeafened,
			&i.JoinedAt,
		); 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 getChannelVoiceStates = `-- name: GetChannelVoiceStates :many
SELECT vs.user_id, vs.channel_id, u.username,
       vs.muted, vs.deafened, vs.speaking,
       vs.camera, vs.screenshare,
       vs.server_muted, vs.server_deafened, vs.joined_at
FROM voice_states vs
JOIN users u ON u.id = vs.user_id
WHERE vs.channel_id = ?
ORDER BY vs.joined_at ASC
`

type GetChannelVoiceStatesRow struct {
	UserID         int64  `json:"userId"`
	ChannelID      int64  `json:"channelId"`
	Username       string `json:"username"`
	Muted          int64  `json:"muted"`
	Deafened       int64  `json:"deafened"`
	Speaking       int64  `json:"speaking"`
	Camera         int64  `json:"camera"`
	Screenshare    int64  `json:"screenshare"`
	ServerMuted    int64  `json:"serverMuted"`
	ServerDeafened int64  `json:"serverDeafened"`
	JoinedAt       string `json:"joinedAt"`
}

func (q *Queries) GetChannelVoiceStates(ctx context.Context, channelID int64) ([]GetChannelVoiceStatesRow, error) {
	rows, err := q.db.QueryContext(ctx, getChannelVoiceStates, channelID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetChannelVoiceStatesRow{}
	for rows.Next() {
		var i GetChannelVoiceStatesRow
		if err := rows.Scan(
			&i.UserID,
			&i.ChannelID,
			&i.Username,
			&i.Muted,
			&i.Deafened,
			&i.Speaking,
			&i.Camera,
			&i.Screenshare,
			&i.ServerMuted,
			&i.ServerDeafened,
			&i.JoinedAt,
		); 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 getUserVoiceState = `-- name: GetUserVoiceState :one
SELECT vs.user_id, vs.channel_id, u.username,
       vs.muted, vs.deafened, vs.speaking,
       vs.camera, vs.screenshare,
       vs.server_muted, vs.server_deafened, vs.joined_at
FROM voice_states vs
JOIN users u ON u.id = vs.user_id
WHERE vs.user_id = ?
`

type GetUserVoiceStateRow struct {
	UserID         int64  `json:"userId"`
	ChannelID      int64  `json:"channelId"`
	Username       string `json:"username"`
	Muted          int64  `json:"muted"`
	Deafened       int64  `json:"deafened"`
	Speaking       int64  `json:"speaking"`
	Camera         int64  `json:"camera"`
	Screenshare    int64  `json:"screenshare"`
	ServerMuted    int64  `json:"serverMuted"`
	ServerDeafened int64  `json:"serverDeafened"`
	JoinedAt       string `json:"joinedAt"`
}

func (q *Queries) GetUserVoiceState(ctx context.Context, userID int64) (GetUserVoiceStateRow, error) {
	row := q.db.QueryRowContext(ctx, getUserVoiceState, userID)
	var i GetUserVoiceStateRow
	err := row.Scan(
		&i.UserID,
		&i.ChannelID,
		&i.Username,
		&i.Muted,
		&i.Deafened,
		&i.Speaking,
		&i.Camera,
		&i.Screenshare,
		&i.ServerMuted,
		&i.ServerDeafened,
		&i.JoinedAt,
	)
	return i, err
}

const joinVoiceChannel = `-- name: JoinVoiceChannel :exec

INSERT INTO voice_states (user_id, channel_id, muted, deafened, speaking, camera, screenshare, joined_at)
VALUES (?, ?, 0, 0, 0, 0, 0, ?)
ON CONFLICT(user_id) DO UPDATE SET
    channel_id  = excluded.channel_id,
    muted       = 0,
    deafened    = 0,
    speaking    = 0,
    camera      = 0,
    screenshare = 0,
    joined_at   = excluded.joined_at
`

type JoinVoiceChannelParams struct {
	UserID    int64  `json:"userId"`
	ChannelID int64  `json:"channelId"`
	JoinedAt  string `json:"joinedAt"`
}

// server_muted / server_deafened are deliberately absent from both upserts'
// reset lists: a moderator-imposed mute must survive a channel switch, which
// reaches the ON CONFLICT branch. It is scoped to the voice session:
// leaving voice deletes the row, so a rejoin starts clean.
func (q *Queries) JoinVoiceChannel(ctx context.Context, arg JoinVoiceChannelParams) error {
	_, err := q.db.ExecContext(ctx, joinVoiceChannel, arg.UserID, arg.ChannelID, arg.JoinedAt)
	return err
}

const joinVoiceChannelIfCapacity = `-- name: JoinVoiceChannelIfCapacity :execresult
INSERT INTO voice_states (user_id, channel_id, muted, deafened, speaking, camera, screenshare, joined_at)
SELECT ?, ?, 0, 0, 0, 0, 0, ?
WHERE (SELECT COUNT(*) FROM voice_states AS vs2 WHERE vs2.channel_id = ?) < ?
ON CONFLICT(user_id) DO UPDATE SET
    channel_id  = excluded.channel_id,
    muted       = 0,
    deafened    = 0,
    speaking    = 0,
    camera      = 0,
    screenshare = 0,
    joined_at   = excluded.joined_at
`

type JoinVoiceChannelIfCapacityParams struct {
	UserID      int64  `json:"userId"`
	ChannelID   int64  `json:"channelId"`
	JoinedAt    string `json:"joinedAt"`
	ChannelID_2 int64  `json:"channelId2"`
	ChannelID_3 int64  `json:"channelId3"`
}

func (q *Queries) JoinVoiceChannelIfCapacity(ctx context.Context, arg JoinVoiceChannelIfCapacityParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, joinVoiceChannelIfCapacity,
		arg.UserID,
		arg.ChannelID,
		arg.JoinedAt,
		arg.ChannelID_2,
		arg.ChannelID_3,
	)
}

const leaveVoiceChannel = `-- name: LeaveVoiceChannel :exec
DELETE FROM voice_states WHERE user_id = ?
`

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

const leaveVoiceChannelIfMatch = `-- name: LeaveVoiceChannelIfMatch :execresult
DELETE FROM voice_states WHERE user_id = ? AND channel_id = ? AND joined_at = ?
`

type LeaveVoiceChannelIfMatchParams struct {
	UserID    int64  `json:"userId"`
	ChannelID int64  `json:"channelId"`
	JoinedAt  string `json:"joinedAt"`
}

func (q *Queries) LeaveVoiceChannelIfMatch(ctx context.Context, arg LeaveVoiceChannelIfMatchParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, leaveVoiceChannelIfMatch, arg.UserID, arg.ChannelID, arg.JoinedAt)
}

const updateVoiceCamera = `-- name: UpdateVoiceCamera :exec
UPDATE voice_states SET camera = ? WHERE user_id = ?
`

type UpdateVoiceCameraParams struct {
	Camera int64 `json:"camera"`
	UserID int64 `json:"userId"`
}

func (q *Queries) UpdateVoiceCamera(ctx context.Context, arg UpdateVoiceCameraParams) error {
	_, err := q.db.ExecContext(ctx, updateVoiceCamera, arg.Camera, arg.UserID)
	return err
}

const updateVoiceDeafen = `-- name: UpdateVoiceDeafen :exec
UPDATE voice_states SET deafened = ? WHERE user_id = ?
`

type UpdateVoiceDeafenParams struct {
	Deafened int64 `json:"deafened"`
	UserID   int64 `json:"userId"`
}

func (q *Queries) UpdateVoiceDeafen(ctx context.Context, arg UpdateVoiceDeafenParams) error {
	_, err := q.db.ExecContext(ctx, updateVoiceDeafen, arg.Deafened, arg.UserID)
	return err
}

const updateVoiceMute = `-- name: UpdateVoiceMute :exec
UPDATE voice_states SET muted = ? WHERE user_id = ?
`

type UpdateVoiceMuteParams struct {
	Muted  int64 `json:"muted"`
	UserID int64 `json:"userId"`
}

func (q *Queries) UpdateVoiceMute(ctx context.Context, arg UpdateVoiceMuteParams) error {
	_, err := q.db.ExecContext(ctx, updateVoiceMute, arg.Muted, arg.UserID)
	return err
}

const updateVoiceScreenshare = `-- name: UpdateVoiceScreenshare :exec
UPDATE voice_states SET screenshare = ? WHERE user_id = ?
`

type UpdateVoiceScreenshareParams struct {
	Screenshare int64 `json:"screenshare"`
	UserID      int64 `json:"userId"`
}

func (q *Queries) UpdateVoiceScreenshare(ctx context.Context, arg UpdateVoiceScreenshareParams) error {
	_, err := q.db.ExecContext(ctx, updateVoiceScreenshare, arg.Screenshare, arg.UserID)
	return err
}
