// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: messages.sql

package database

import (
	"context"
	"time"

	"github.com/google/uuid"
)

const createMessage = `-- name: CreateMessage :one
INSERT INTO messages (id, content, created_at, updated_at, sender_id, room_id)
VALUES ($1, $2, timezone('utc', NOW()), timezone('utc', NOW()), $3, $4)
RETURNING id, content, created_at, updated_at, sender_id, room_id
`

type CreateMessageParams struct {
	ID       uuid.UUID
	Content  string
	SenderID uuid.UUID
	RoomID   uuid.UUID
}

func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
	row := q.db.QueryRowContext(ctx, createMessage,
		arg.ID,
		arg.Content,
		arg.SenderID,
		arg.RoomID,
	)
	var i Message
	err := row.Scan(
		&i.ID,
		&i.Content,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.SenderID,
		&i.RoomID,
	)
	return i, err
}

const getMessagesByRoom = `-- name: GetMessagesByRoom :many
SELECT messages.id, messages.content, messages.created_at, messages.updated_at, messages.sender_id, messages.room_id, users.username 
FROM messages 
JOIN users ON users.id = messages.sender_id
WHERE messages.room_id = $1
`

type GetMessagesByRoomRow struct {
	ID        uuid.UUID
	Content   string
	CreatedAt time.Time
	UpdatedAt time.Time
	SenderID  uuid.UUID
	RoomID    uuid.UUID
	Username  string
}

func (q *Queries) GetMessagesByRoom(ctx context.Context, roomID uuid.UUID) ([]GetMessagesByRoomRow, error) {
	rows, err := q.db.QueryContext(ctx, getMessagesByRoom, roomID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetMessagesByRoomRow
	for rows.Next() {
		var i GetMessagesByRoomRow
		if err := rows.Scan(
			&i.ID,
			&i.Content,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.SenderID,
			&i.RoomID,
			&i.Username,
		); 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 getMessagesByRoomAndUser = `-- name: GetMessagesByRoomAndUser :many
SELECT id, content, created_at, updated_at, sender_id, room_id FROM messages WHERE room_id = $1 AND sender_id = $2
`

type GetMessagesByRoomAndUserParams struct {
	RoomID   uuid.UUID
	SenderID uuid.UUID
}

func (q *Queries) GetMessagesByRoomAndUser(ctx context.Context, arg GetMessagesByRoomAndUserParams) ([]Message, error) {
	rows, err := q.db.QueryContext(ctx, getMessagesByRoomAndUser, arg.RoomID, arg.SenderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Message
	for rows.Next() {
		var i Message
		if err := rows.Scan(
			&i.ID,
			&i.Content,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.SenderID,
			&i.RoomID,
		); 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 getMessagesByUser = `-- name: GetMessagesByUser :many
SELECT messages.id, messages.content, messages.created_at, messages.updated_at, messages.sender_id, messages.room_id, users.username
FROM messages
JOIN users ON users.id = messages.sender_id
WHERE messages.sender_id = $1
`

type GetMessagesByUserRow struct {
	ID        uuid.UUID
	Content   string
	CreatedAt time.Time
	UpdatedAt time.Time
	SenderID  uuid.UUID
	RoomID    uuid.UUID
	Username  string
}

func (q *Queries) GetMessagesByUser(ctx context.Context, senderID uuid.UUID) ([]GetMessagesByUserRow, error) {
	rows, err := q.db.QueryContext(ctx, getMessagesByUser, senderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetMessagesByUserRow
	for rows.Next() {
		var i GetMessagesByUserRow
		if err := rows.Scan(
			&i.ID,
			&i.Content,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.SenderID,
			&i.RoomID,
			&i.Username,
		); 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 getPreviousRoomMessages = `-- name: GetPreviousRoomMessages :many
SELECT messages.id, messages.content, messages.created_at, messages.updated_at, messages.sender_id, messages.room_id, users.username FROM messages 
JOIN users ON users.id = messages.sender_id
WHERE messages.room_id = $1 
ORDER BY messages.created_at ASC LIMIT $2
`

type GetPreviousRoomMessagesParams struct {
	RoomID uuid.UUID
	Limit  int32
}

type GetPreviousRoomMessagesRow struct {
	ID        uuid.UUID
	Content   string
	CreatedAt time.Time
	UpdatedAt time.Time
	SenderID  uuid.UUID
	RoomID    uuid.UUID
	Username  string
}

func (q *Queries) GetPreviousRoomMessages(ctx context.Context, arg GetPreviousRoomMessagesParams) ([]GetPreviousRoomMessagesRow, error) {
	rows, err := q.db.QueryContext(ctx, getPreviousRoomMessages, arg.RoomID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetPreviousRoomMessagesRow
	for rows.Next() {
		var i GetPreviousRoomMessagesRow
		if err := rows.Scan(
			&i.ID,
			&i.Content,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.SenderID,
			&i.RoomID,
			&i.Username,
		); 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
}
