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

package dbgen

import (
	"context"
)

const closeDM = `-- name: CloseDM :exec
DELETE FROM dm_open_state WHERE user_id = ? AND channel_id = ?
`

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

func (q *Queries) CloseDM(ctx context.Context, arg CloseDMParams) error {
	_, err := q.db.ExecContext(ctx, closeDM, arg.UserID, arg.ChannelID)
	return err
}

const getDMParticipantIDs = `-- name: GetDMParticipantIDs :many
SELECT user_id FROM dm_participants WHERE channel_id = ?
`

func (q *Queries) GetDMParticipantIDs(ctx context.Context, channelID int64) ([]int64, error) {
	rows, err := q.db.QueryContext(ctx, getDMParticipantIDs, channelID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []int64{}
	for rows.Next() {
		var user_id int64
		if err := rows.Scan(&user_id); err != nil {
			return nil, err
		}
		items = append(items, user_id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserDMChannelIDs = `-- name: GetUserDMChannelIDs :many
SELECT channel_id FROM dm_open_state WHERE user_id = ?
`

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

const getUserDMChannels = `-- name: GetUserDMChannels :many
SELECT
    c.id                                          AS channel_id,
    u.id                                          AS recipient_id,
    u.username                                    AS recipient_username,
    COALESCE(u.avatar, '')                        AS recipient_avatar,
    u.status                                      AS recipient_status,
    lm.id                                         AS last_message_id,
    COALESCE(lm.content, '')                      AS last_message,
    COALESCE(lm.timestamp, '')                    AS last_message_at,
    (SELECT COUNT(*) FROM messages mu
      WHERE mu.channel_id = c.id AND mu.deleted = 0
        AND mu.id > COALESCE((SELECT rs.last_message_id FROM read_states rs
                               WHERE rs.channel_id = c.id AND rs.user_id = dos.user_id), 0)
    ) AS unread_count
FROM dm_open_state dos
JOIN channels c          ON c.id = dos.channel_id AND c.type = 'dm'
JOIN dm_participants dp  ON dp.channel_id = c.id AND dp.user_id != ?
JOIN users u             ON u.id = dp.user_id
LEFT JOIN messages lm    ON lm.id = (
    SELECT MAX(id) FROM messages WHERE channel_id = c.id AND deleted = 0
)
WHERE dos.user_id = ?
ORDER BY COALESCE(lm.timestamp, dos.opened_at) DESC
`

type GetUserDMChannelsParams struct {
	UserID   int64 `json:"userId"`
	UserID_2 int64 `json:"userId2"`
}

type GetUserDMChannelsRow struct {
	ChannelID         int64  `json:"channelId"`
	RecipientID       int64  `json:"recipientId"`
	RecipientUsername string `json:"recipientUsername"`
	RecipientAvatar   string `json:"recipientAvatar"`
	RecipientStatus   string `json:"recipientStatus"`
	LastMessageID     *int64 `json:"lastMessageId"`
	LastMessage       string `json:"lastMessage"`
	LastMessageAt     string `json:"lastMessageAt"`
	UnreadCount       int64  `json:"unreadCount"`
}

func (q *Queries) GetUserDMChannels(ctx context.Context, arg GetUserDMChannelsParams) ([]GetUserDMChannelsRow, error) {
	rows, err := q.db.QueryContext(ctx, getUserDMChannels, arg.UserID, arg.UserID_2)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetUserDMChannelsRow{}
	for rows.Next() {
		var i GetUserDMChannelsRow
		if err := rows.Scan(
			&i.ChannelID,
			&i.RecipientID,
			&i.RecipientUsername,
			&i.RecipientAvatar,
			&i.RecipientStatus,
			&i.LastMessageID,
			&i.LastMessage,
			&i.LastMessageAt,
			&i.UnreadCount,
		); 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 isDMParticipant = `-- name: IsDMParticipant :one
SELECT user_id FROM dm_participants WHERE user_id = ? AND channel_id = ?
`

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

func (q *Queries) IsDMParticipant(ctx context.Context, arg IsDMParticipantParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, isDMParticipant, arg.UserID, arg.ChannelID)
	var user_id int64
	err := row.Scan(&user_id)
	return user_id, err
}

const openDM = `-- name: OpenDM :exec
INSERT OR IGNORE INTO dm_open_state (user_id, channel_id) VALUES (?, ?)
`

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

func (q *Queries) OpenDM(ctx context.Context, arg OpenDMParams) error {
	_, err := q.db.ExecContext(ctx, openDM, arg.UserID, arg.ChannelID)
	return err
}
