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

package sqlc

import (
	"context"

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

const createSession = `-- name: CreateSession :one
INSERT INTO bot_sessions (
  bot_id, route_id, channel_type, type, title, metadata, parent_session_id
)
VALUES (
  $1,
  $2::uuid,
  $3::text,
  $4,
  $5,
  $6,
  $7::uuid
)
RETURNING id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
`

type CreateSessionParams struct {
	BotID           pgtype.UUID `json:"bot_id"`
	RouteID         pgtype.UUID `json:"route_id"`
	ChannelType     pgtype.Text `json:"channel_type"`
	Type            string      `json:"type"`
	Title           string      `json:"title"`
	Metadata        []byte      `json:"metadata"`
	ParentSessionID pgtype.UUID `json:"parent_session_id"`
}

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (BotSession, error) {
	row := q.db.QueryRow(ctx, createSession,
		arg.BotID,
		arg.RouteID,
		arg.ChannelType,
		arg.Type,
		arg.Title,
		arg.Metadata,
		arg.ParentSessionID,
	)
	var i BotSession
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.RouteID,
		&i.ChannelType,
		&i.Type,
		&i.Title,
		&i.Metadata,
		&i.ParentSessionID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getActiveSessionForRoute = `-- name: GetActiveSessionForRoute :one
SELECT s.id, s.bot_id, s.route_id, s.channel_type, s.type, s.title, s.metadata, s.parent_session_id, s.created_at, s.updated_at, s.deleted_at
FROM bot_sessions s
JOIN bot_channel_routes r ON r.active_session_id = s.id
WHERE r.id = $1
  AND s.deleted_at IS NULL
`

func (q *Queries) GetActiveSessionForRoute(ctx context.Context, routeID pgtype.UUID) (BotSession, error) {
	row := q.db.QueryRow(ctx, getActiveSessionForRoute, routeID)
	var i BotSession
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.RouteID,
		&i.ChannelType,
		&i.Type,
		&i.Title,
		&i.Metadata,
		&i.ParentSessionID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getSessionByID = `-- name: GetSessionByID :one
SELECT id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
FROM bot_sessions
WHERE id = $1
  AND deleted_at IS NULL
`

func (q *Queries) GetSessionByID(ctx context.Context, id pgtype.UUID) (BotSession, error) {
	row := q.db.QueryRow(ctx, getSessionByID, id)
	var i BotSession
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.RouteID,
		&i.ChannelType,
		&i.Type,
		&i.Title,
		&i.Metadata,
		&i.ParentSessionID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const listSessionsByBot = `-- name: ListSessionsByBot :many
SELECT
  s.id, s.bot_id, s.route_id, s.channel_type, s.type, s.title, s.metadata,
  s.created_at, s.updated_at, s.deleted_at,
  r.metadata AS route_metadata,
  r.conversation_type AS route_conversation_type
FROM bot_sessions s
LEFT JOIN bot_channel_routes r ON r.id = s.route_id
WHERE s.bot_id = $1
  AND s.deleted_at IS NULL
ORDER BY s.updated_at DESC
`

type ListSessionsByBotRow struct {
	ID                    pgtype.UUID        `json:"id"`
	BotID                 pgtype.UUID        `json:"bot_id"`
	RouteID               pgtype.UUID        `json:"route_id"`
	ChannelType           pgtype.Text        `json:"channel_type"`
	Type                  string             `json:"type"`
	Title                 string             `json:"title"`
	Metadata              []byte             `json:"metadata"`
	CreatedAt             pgtype.Timestamptz `json:"created_at"`
	UpdatedAt             pgtype.Timestamptz `json:"updated_at"`
	DeletedAt             pgtype.Timestamptz `json:"deleted_at"`
	RouteMetadata         []byte             `json:"route_metadata"`
	RouteConversationType pgtype.Text        `json:"route_conversation_type"`
}

func (q *Queries) ListSessionsByBot(ctx context.Context, botID pgtype.UUID) ([]ListSessionsByBotRow, error) {
	rows, err := q.db.Query(ctx, listSessionsByBot, botID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListSessionsByBotRow
	for rows.Next() {
		var i ListSessionsByBotRow
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.RouteID,
			&i.ChannelType,
			&i.Type,
			&i.Title,
			&i.Metadata,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
			&i.RouteMetadata,
			&i.RouteConversationType,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSessionsByRoute = `-- name: ListSessionsByRoute :many
SELECT id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
FROM bot_sessions
WHERE route_id = $1
  AND deleted_at IS NULL
ORDER BY updated_at DESC
`

func (q *Queries) ListSessionsByRoute(ctx context.Context, routeID pgtype.UUID) ([]BotSession, error) {
	rows, err := q.db.Query(ctx, listSessionsByRoute, routeID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotSession
	for rows.Next() {
		var i BotSession
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.RouteID,
			&i.ChannelType,
			&i.Type,
			&i.Title,
			&i.Metadata,
			&i.ParentSessionID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSubagentSessionsByParent = `-- name: ListSubagentSessionsByParent :many
SELECT id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
FROM bot_sessions
WHERE parent_session_id = $1
  AND deleted_at IS NULL
ORDER BY created_at DESC
`

func (q *Queries) ListSubagentSessionsByParent(ctx context.Context, parentSessionID pgtype.UUID) ([]BotSession, error) {
	rows, err := q.db.Query(ctx, listSubagentSessionsByParent, parentSessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotSession
	for rows.Next() {
		var i BotSession
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.RouteID,
			&i.ChannelType,
			&i.Type,
			&i.Title,
			&i.Metadata,
			&i.ParentSessionID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const softDeleteSession = `-- name: SoftDeleteSession :exec
UPDATE bot_sessions
SET deleted_at = now(), updated_at = now()
WHERE id = $1 AND deleted_at IS NULL
`

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

const softDeleteSessionsByBot = `-- name: SoftDeleteSessionsByBot :exec
UPDATE bot_sessions
SET deleted_at = now(), updated_at = now()
WHERE bot_id = $1 AND deleted_at IS NULL
`

func (q *Queries) SoftDeleteSessionsByBot(ctx context.Context, botID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, softDeleteSessionsByBot, botID)
	return err
}

const touchSession = `-- name: TouchSession :exec
UPDATE bot_sessions
SET updated_at = now()
WHERE id = $1 AND deleted_at IS NULL
`

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

const updateSessionMetadata = `-- name: UpdateSessionMetadata :one
UPDATE bot_sessions
SET metadata = $1, updated_at = now()
WHERE id = $2 AND deleted_at IS NULL
RETURNING id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
`

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

func (q *Queries) UpdateSessionMetadata(ctx context.Context, arg UpdateSessionMetadataParams) (BotSession, error) {
	row := q.db.QueryRow(ctx, updateSessionMetadata, arg.Metadata, arg.ID)
	var i BotSession
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.RouteID,
		&i.ChannelType,
		&i.Type,
		&i.Title,
		&i.Metadata,
		&i.ParentSessionID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const updateSessionTitle = `-- name: UpdateSessionTitle :one
UPDATE bot_sessions
SET title = $1, updated_at = now()
WHERE id = $2 AND deleted_at IS NULL
RETURNING id, bot_id, route_id, channel_type, type, title, metadata, parent_session_id, created_at, updated_at, deleted_at
`

type UpdateSessionTitleParams struct {
	Title string      `json:"title"`
	ID    pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateSessionTitle(ctx context.Context, arg UpdateSessionTitleParams) (BotSession, error) {
	row := q.db.QueryRow(ctx, updateSessionTitle, arg.Title, arg.ID)
	var i BotSession
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.RouteID,
		&i.ChannelType,
		&i.Type,
		&i.Title,
		&i.Metadata,
		&i.ParentSessionID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}
