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

package sqlc

import (
	"context"

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

const createChatRoute = `-- name: CreateChatRoute :one
INSERT INTO bot_channel_routes (
  bot_id, channel_type, channel_config_id, external_conversation_id, external_thread_id, conversation_type, default_reply_target, metadata
)
VALUES (
  $1,
  $2,
  $3::uuid,
  $4,
  $5::text,
  $6::text,
  $7::text,
  $8
)
RETURNING
  id,
  $9::uuid AS chat_id,
  bot_id,
  channel_type AS platform,
  channel_config_id,
  external_conversation_id AS conversation_id,
  external_thread_id AS thread_id,
  conversation_type,
  default_reply_target AS reply_target,
  metadata,
  created_at,
  updated_at
`

type CreateChatRouteParams struct {
	BotID            pgtype.UUID `json:"bot_id"`
	Platform         string      `json:"platform"`
	ChannelConfigID  pgtype.UUID `json:"channel_config_id"`
	ConversationID   string      `json:"conversation_id"`
	ThreadID         pgtype.Text `json:"thread_id"`
	ConversationType pgtype.Text `json:"conversation_type"`
	ReplyTarget      pgtype.Text `json:"reply_target"`
	Metadata         []byte      `json:"metadata"`
	ChatID           pgtype.UUID `json:"chat_id"`
}

type CreateChatRouteRow struct {
	ID               pgtype.UUID        `json:"id"`
	ChatID           pgtype.UUID        `json:"chat_id"`
	BotID            pgtype.UUID        `json:"bot_id"`
	Platform         string             `json:"platform"`
	ChannelConfigID  pgtype.UUID        `json:"channel_config_id"`
	ConversationID   string             `json:"conversation_id"`
	ThreadID         pgtype.Text        `json:"thread_id"`
	ConversationType pgtype.Text        `json:"conversation_type"`
	ReplyTarget      pgtype.Text        `json:"reply_target"`
	Metadata         []byte             `json:"metadata"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
	UpdatedAt        pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) CreateChatRoute(ctx context.Context, arg CreateChatRouteParams) (CreateChatRouteRow, error) {
	row := q.db.QueryRow(ctx, createChatRoute,
		arg.BotID,
		arg.Platform,
		arg.ChannelConfigID,
		arg.ConversationID,
		arg.ThreadID,
		arg.ConversationType,
		arg.ReplyTarget,
		arg.Metadata,
		arg.ChatID,
	)
	var i CreateChatRouteRow
	err := row.Scan(
		&i.ID,
		&i.ChatID,
		&i.BotID,
		&i.Platform,
		&i.ChannelConfigID,
		&i.ConversationID,
		&i.ThreadID,
		&i.ConversationType,
		&i.ReplyTarget,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteChatRoute = `-- name: DeleteChatRoute :exec
DELETE FROM bot_channel_routes
WHERE id = $1
`

func (q *Queries) DeleteChatRoute(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteChatRoute, id)
	return err
}

const findChatRoute = `-- name: FindChatRoute :one
SELECT
  id,
  bot_id AS chat_id,
  bot_id,
  channel_type AS platform,
  channel_config_id,
  external_conversation_id AS conversation_id,
  external_thread_id AS thread_id,
  conversation_type,
  default_reply_target AS reply_target,
  metadata,
  created_at,
  updated_at
FROM bot_channel_routes
WHERE bot_id = $1
  AND channel_type = $2
  AND external_conversation_id = $3
  AND COALESCE(external_thread_id, '') = COALESCE($4, '')
LIMIT 1
`

type FindChatRouteParams struct {
	BotID          pgtype.UUID `json:"bot_id"`
	Platform       string      `json:"platform"`
	ConversationID string      `json:"conversation_id"`
	ThreadID       pgtype.Text `json:"thread_id"`
}

type FindChatRouteRow struct {
	ID               pgtype.UUID        `json:"id"`
	ChatID           pgtype.UUID        `json:"chat_id"`
	BotID            pgtype.UUID        `json:"bot_id"`
	Platform         string             `json:"platform"`
	ChannelConfigID  pgtype.UUID        `json:"channel_config_id"`
	ConversationID   string             `json:"conversation_id"`
	ThreadID         pgtype.Text        `json:"thread_id"`
	ConversationType pgtype.Text        `json:"conversation_type"`
	ReplyTarget      pgtype.Text        `json:"reply_target"`
	Metadata         []byte             `json:"metadata"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
	UpdatedAt        pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) FindChatRoute(ctx context.Context, arg FindChatRouteParams) (FindChatRouteRow, error) {
	row := q.db.QueryRow(ctx, findChatRoute,
		arg.BotID,
		arg.Platform,
		arg.ConversationID,
		arg.ThreadID,
	)
	var i FindChatRouteRow
	err := row.Scan(
		&i.ID,
		&i.ChatID,
		&i.BotID,
		&i.Platform,
		&i.ChannelConfigID,
		&i.ConversationID,
		&i.ThreadID,
		&i.ConversationType,
		&i.ReplyTarget,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getChatRouteByID = `-- name: GetChatRouteByID :one
SELECT
  id,
  bot_id AS chat_id,
  bot_id,
  channel_type AS platform,
  channel_config_id,
  external_conversation_id AS conversation_id,
  external_thread_id AS thread_id,
  conversation_type,
  default_reply_target AS reply_target,
  metadata,
  created_at,
  updated_at
FROM bot_channel_routes
WHERE id = $1
`

type GetChatRouteByIDRow struct {
	ID               pgtype.UUID        `json:"id"`
	ChatID           pgtype.UUID        `json:"chat_id"`
	BotID            pgtype.UUID        `json:"bot_id"`
	Platform         string             `json:"platform"`
	ChannelConfigID  pgtype.UUID        `json:"channel_config_id"`
	ConversationID   string             `json:"conversation_id"`
	ThreadID         pgtype.Text        `json:"thread_id"`
	ConversationType pgtype.Text        `json:"conversation_type"`
	ReplyTarget      pgtype.Text        `json:"reply_target"`
	Metadata         []byte             `json:"metadata"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
	UpdatedAt        pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) GetChatRouteByID(ctx context.Context, id pgtype.UUID) (GetChatRouteByIDRow, error) {
	row := q.db.QueryRow(ctx, getChatRouteByID, id)
	var i GetChatRouteByIDRow
	err := row.Scan(
		&i.ID,
		&i.ChatID,
		&i.BotID,
		&i.Platform,
		&i.ChannelConfigID,
		&i.ConversationID,
		&i.ThreadID,
		&i.ConversationType,
		&i.ReplyTarget,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listChatRoutes = `-- name: ListChatRoutes :many
SELECT
  id,
  bot_id AS chat_id,
  bot_id,
  channel_type AS platform,
  channel_config_id,
  external_conversation_id AS conversation_id,
  external_thread_id AS thread_id,
  conversation_type,
  default_reply_target AS reply_target,
  metadata,
  created_at,
  updated_at
FROM bot_channel_routes
WHERE bot_id = $1
ORDER BY created_at ASC
`

type ListChatRoutesRow struct {
	ID               pgtype.UUID        `json:"id"`
	ChatID           pgtype.UUID        `json:"chat_id"`
	BotID            pgtype.UUID        `json:"bot_id"`
	Platform         string             `json:"platform"`
	ChannelConfigID  pgtype.UUID        `json:"channel_config_id"`
	ConversationID   string             `json:"conversation_id"`
	ThreadID         pgtype.Text        `json:"thread_id"`
	ConversationType pgtype.Text        `json:"conversation_type"`
	ReplyTarget      pgtype.Text        `json:"reply_target"`
	Metadata         []byte             `json:"metadata"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
	UpdatedAt        pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) ListChatRoutes(ctx context.Context, chatID pgtype.UUID) ([]ListChatRoutesRow, error) {
	rows, err := q.db.Query(ctx, listChatRoutes, chatID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListChatRoutesRow
	for rows.Next() {
		var i ListChatRoutesRow
		if err := rows.Scan(
			&i.ID,
			&i.ChatID,
			&i.BotID,
			&i.Platform,
			&i.ChannelConfigID,
			&i.ConversationID,
			&i.ThreadID,
			&i.ConversationType,
			&i.ReplyTarget,
			&i.Metadata,
			&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 updateChatRouteMetadata = `-- name: UpdateChatRouteMetadata :exec
UPDATE bot_channel_routes
SET metadata = $1, updated_at = now()
WHERE id = $2
`

type UpdateChatRouteMetadataParams struct {
	Metadata []byte      `json:"metadata"`
	ID       pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateChatRouteMetadata(ctx context.Context, arg UpdateChatRouteMetadataParams) error {
	_, err := q.db.Exec(ctx, updateChatRouteMetadata, arg.Metadata, arg.ID)
	return err
}

const updateChatRouteReplyTarget = `-- name: UpdateChatRouteReplyTarget :exec
UPDATE bot_channel_routes
SET default_reply_target = $1, updated_at = now()
WHERE id = $2
`

type UpdateChatRouteReplyTargetParams struct {
	ReplyTarget pgtype.Text `json:"reply_target"`
	ID          pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateChatRouteReplyTarget(ctx context.Context, arg UpdateChatRouteReplyTargetParams) error {
	_, err := q.db.Exec(ctx, updateChatRouteReplyTarget, arg.ReplyTarget, arg.ID)
	return err
}
