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

package sqlc

import (
	"context"

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

const createChatbotHistory = `-- name: CreateChatbotHistory :one
INSERT INTO chatbot_histories (user_id, question, answer)
VALUES ($1, $2, $3)
RETURNING id, user_id, question, answer, created_at
`

type CreateChatbotHistoryParams struct {
	UserID   pgtype.UUID `json:"user_id"`
	Question string      `json:"question"`
	Answer   string      `json:"answer"`
}

func (q *Queries) CreateChatbotHistory(ctx context.Context, arg CreateChatbotHistoryParams) (ChatbotHistory, error) {
	row := q.db.QueryRow(ctx, createChatbotHistory, arg.UserID, arg.Question, arg.Answer)
	var i ChatbotHistory
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Question,
		&i.Answer,
		&i.CreatedAt,
	)
	return i, err
}

const createConversation = `-- name: CreateConversation :one
INSERT INTO conversations (user_id, status)
VALUES ($1, $2)
RETURNING id, user_id, mod_id, status, closed_at, created_at, updated_at
`

type CreateConversationParams struct {
	UserID pgtype.UUID `json:"user_id"`
	Status int16       `json:"status"`
}

func (q *Queries) CreateConversation(ctx context.Context, arg CreateConversationParams) (Conversation, error) {
	row := q.db.QueryRow(ctx, createConversation, arg.UserID, arg.Status)
	var i Conversation
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.ModID,
		&i.Status,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createMessage = `-- name: CreateMessage :one
INSERT INTO messages (conversation_id, sender_id, content)
VALUES ($1, $2, $3)
RETURNING id, conversation_id, sender_id, content, created_at
`

type CreateMessageParams struct {
	ConversationID pgtype.UUID `json:"conversation_id"`
	SenderID       pgtype.UUID `json:"sender_id"`
	Content        string      `json:"content"`
}

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

const getChatbotHistoriesByIDs = `-- name: GetChatbotHistoriesByIDs :many
SELECT id, user_id, question, answer, created_at FROM chatbot_histories WHERE id = ANY($1::uuid[])
`

func (q *Queries) GetChatbotHistoriesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]ChatbotHistory, error) {
	rows, err := q.db.Query(ctx, getChatbotHistoriesByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ChatbotHistory{}
	for rows.Next() {
		var i ChatbotHistory
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Question,
			&i.Answer,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getChatbotHistory = `-- name: GetChatbotHistory :many
SELECT id, user_id, question, answer, created_at FROM (
    SELECT id, user_id, question, answer, created_at FROM chatbot_histories
    WHERE user_id = $1
      AND ($2::uuid IS NULL OR id < $2::uuid)
    ORDER BY created_at DESC
    LIMIT $3
) sub
ORDER BY created_at ASC
`

type GetChatbotHistoryParams struct {
	UserID   pgtype.UUID `json:"user_id"`
	CursorID pgtype.UUID `json:"cursor_id"`
	Limit    int32       `json:"limit"`
}

func (q *Queries) GetChatbotHistory(ctx context.Context, arg GetChatbotHistoryParams) ([]ChatbotHistory, error) {
	rows, err := q.db.Query(ctx, getChatbotHistory, arg.UserID, arg.CursorID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ChatbotHistory{}
	for rows.Next() {
		var i ChatbotHistory
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Question,
			&i.Answer,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getConversationsByIDs = `-- name: GetConversationsByIDs :many
SELECT id, user_id, mod_id, status, closed_at, created_at, updated_at FROM conversations WHERE id = ANY($1::uuid[])
`

func (q *Queries) GetConversationsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Conversation, error) {
	rows, err := q.db.Query(ctx, getConversationsByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Conversation{}
	for rows.Next() {
		var i Conversation
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.ModID,
			&i.Status,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMessagesByConversation = `-- name: GetMessagesByConversation :many
SELECT id, conversation_id, sender_id, content, created_at FROM messages
WHERE conversation_id = $1
  AND ($2::uuid IS NULL OR id < $2::uuid)
ORDER BY created_at DESC
LIMIT $3
`

type GetMessagesByConversationParams struct {
	ConversationID pgtype.UUID `json:"conversation_id"`
	CursorID       pgtype.UUID `json:"cursor_id"`
	Limit          int32       `json:"limit"`
}

func (q *Queries) GetMessagesByConversation(ctx context.Context, arg GetMessagesByConversationParams) ([]Message, error) {
	rows, err := q.db.Query(ctx, getMessagesByConversation, arg.ConversationID, arg.CursorID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Message{}
	for rows.Next() {
		var i Message
		if err := rows.Scan(
			&i.ID,
			&i.ConversationID,
			&i.SenderID,
			&i.Content,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMessagesByIDs = `-- name: GetMessagesByIDs :many
SELECT id, conversation_id, sender_id, content, created_at FROM messages WHERE id = ANY($1::uuid[])
`

func (q *Queries) GetMessagesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Message, error) {
	rows, err := q.db.Query(ctx, getMessagesByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Message{}
	for rows.Next() {
		var i Message
		if err := rows.Scan(
			&i.ID,
			&i.ConversationID,
			&i.SenderID,
			&i.Content,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateConversationStatus = `-- name: UpdateConversationStatus :one
UPDATE conversations
SET status = $2, mod_id = COALESCE($3, mod_id), closed_at = $4, updated_at = NOW()
WHERE id = $1
RETURNING id, user_id, mod_id, status, closed_at, created_at, updated_at
`

type UpdateConversationStatusParams struct {
	ID       pgtype.UUID        `json:"id"`
	Status   int16              `json:"status"`
	ModID    pgtype.UUID        `json:"mod_id"`
	ClosedAt pgtype.Timestamptz `json:"closed_at"`
}

func (q *Queries) UpdateConversationStatus(ctx context.Context, arg UpdateConversationStatusParams) (Conversation, error) {
	row := q.db.QueryRow(ctx, updateConversationStatus,
		arg.ID,
		arg.Status,
		arg.ModID,
		arg.ClosedAt,
	)
	var i Conversation
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.ModID,
		&i.Status,
		&i.ClosedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
