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

package gen

import (
	"context"
	"database/sql"
	"time"

	"github.com/aoagents/agent-orchestrator/backend/internal/domain"
)

const getSession = `-- name: GetSession :one
SELECT id, project_id, num, issue_id, kind, harness,
    activity_state, activity_last_at, is_terminated, branch, workspace_path,
    runtime_handle_id, agent_session_id, prompt, created_at, updated_at, display_name, first_signal_at, preview_url, preview_revision
FROM sessions WHERE id = ?
`

func (q *Queries) GetSession(ctx context.Context, id domain.SessionID) (Session, error) {
	row := q.db.QueryRowContext(ctx, getSession, id)
	var i Session
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Num,
		&i.IssueID,
		&i.Kind,
		&i.Harness,
		&i.ActivityState,
		&i.ActivityLastAt,
		&i.IsTerminated,
		&i.Branch,
		&i.WorkspacePath,
		&i.RuntimeHandleID,
		&i.AgentSessionID,
		&i.Prompt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DisplayName,
		&i.FirstSignalAt,
		&i.PreviewURL,
		&i.PreviewRevision,
	)
	return i, err
}

const insertSession = `-- name: InsertSession :exec
INSERT INTO sessions (
    id, project_id, num, issue_id, kind, harness, display_name,
    activity_state, activity_last_at, first_signal_at, is_terminated,
    branch, workspace_path, runtime_handle_id, agent_session_id, prompt,
    preview_url, preview_revision, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`

type InsertSessionParams struct {
	ID              domain.SessionID
	ProjectID       domain.ProjectID
	Num             int64
	IssueID         domain.IssueID
	Kind            domain.SessionKind
	Harness         domain.AgentHarness
	DisplayName     string
	ActivityState   domain.ActivityState
	ActivityLastAt  time.Time
	FirstSignalAt   sql.NullTime
	IsTerminated    bool
	Branch          string
	WorkspacePath   string
	RuntimeHandleID string
	AgentSessionID  string
	Prompt          string
	PreviewURL      string
	PreviewRevision int64
	CreatedAt       time.Time
	UpdatedAt       time.Time
}

func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) error {
	_, err := q.db.ExecContext(ctx, insertSession,
		arg.ID,
		arg.ProjectID,
		arg.Num,
		arg.IssueID,
		arg.Kind,
		arg.Harness,
		arg.DisplayName,
		arg.ActivityState,
		arg.ActivityLastAt,
		arg.FirstSignalAt,
		arg.IsTerminated,
		arg.Branch,
		arg.WorkspacePath,
		arg.RuntimeHandleID,
		arg.AgentSessionID,
		arg.Prompt,
		arg.PreviewURL,
		arg.PreviewRevision,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}

const listAllSessions = `-- name: ListAllSessions :many
SELECT id, project_id, num, issue_id, kind, harness,
    activity_state, activity_last_at, is_terminated, branch, workspace_path,
    runtime_handle_id, agent_session_id, prompt, created_at, updated_at, display_name, first_signal_at, preview_url, preview_revision
FROM sessions ORDER BY project_id, num
`

func (q *Queries) ListAllSessions(ctx context.Context) ([]Session, error) {
	rows, err := q.db.QueryContext(ctx, listAllSessions)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Session{}
	for rows.Next() {
		var i Session
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Num,
			&i.IssueID,
			&i.Kind,
			&i.Harness,
			&i.ActivityState,
			&i.ActivityLastAt,
			&i.IsTerminated,
			&i.Branch,
			&i.WorkspacePath,
			&i.RuntimeHandleID,
			&i.AgentSessionID,
			&i.Prompt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DisplayName,
			&i.FirstSignalAt,
			&i.PreviewURL,
			&i.PreviewRevision,
		); 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 listSessionsByProject = `-- name: ListSessionsByProject :many
SELECT id, project_id, num, issue_id, kind, harness,
    activity_state, activity_last_at, is_terminated, branch, workspace_path,
    runtime_handle_id, agent_session_id, prompt, created_at, updated_at, display_name, first_signal_at, preview_url, preview_revision
FROM sessions WHERE project_id = ? ORDER BY num
`

func (q *Queries) ListSessionsByProject(ctx context.Context, projectID domain.ProjectID) ([]Session, error) {
	rows, err := q.db.QueryContext(ctx, listSessionsByProject, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Session{}
	for rows.Next() {
		var i Session
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Num,
			&i.IssueID,
			&i.Kind,
			&i.Harness,
			&i.ActivityState,
			&i.ActivityLastAt,
			&i.IsTerminated,
			&i.Branch,
			&i.WorkspacePath,
			&i.RuntimeHandleID,
			&i.AgentSessionID,
			&i.Prompt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DisplayName,
			&i.FirstSignalAt,
			&i.PreviewURL,
			&i.PreviewRevision,
		); 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 nextSessionNum = `-- name: NextSessionNum :one
SELECT COALESCE(MAX(num), 0) + 1 AS next FROM sessions WHERE project_id = ?
`

func (q *Queries) NextSessionNum(ctx context.Context, projectID domain.ProjectID) (int64, error) {
	row := q.db.QueryRowContext(ctx, nextSessionNum, projectID)
	var next int64
	err := row.Scan(&next)
	return next, err
}

const renameSession = `-- name: RenameSession :execrows
UPDATE sessions SET display_name = ?, updated_at = ? WHERE id = ?
`

type RenameSessionParams struct {
	DisplayName string
	UpdatedAt   time.Time
	ID          domain.SessionID
}

func (q *Queries) RenameSession(ctx context.Context, arg RenameSessionParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, renameSession, arg.DisplayName, arg.UpdatedAt, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const sessionIsSeed = `-- name: SessionIsSeed :one
SELECT EXISTS(
    SELECT 1 FROM sessions
    WHERE id = ?
      AND is_terminated = 0
      AND workspace_path = ''
      AND runtime_handle_id = ''
      AND agent_session_id = ''
      AND prompt = ''
) AS is_seed
`

// SessionIsSeed reports whether the session id matches a row still in seed
// state (see DeleteSeedSession for the conditions). Callers probe with this
// before touching change_log so that DeleteSession is a true no-op for live
// sessions instead of silently destroying their CDC events. Returns 0 when
// the row does not exist OR has progressed past seed state.
func (q *Queries) SessionIsSeed(ctx context.Context, id domain.SessionID) (bool, error) {
	row := q.db.QueryRowContext(ctx, sessionIsSeed, id)
	var is_seed bool
	err := row.Scan(&is_seed)
	return is_seed, err
}

const setSessionPreviewURL = `-- name: SetSessionPreviewURL :execrows
UPDATE sessions SET preview_url = ?, preview_revision = preview_revision + 1, updated_at = ? WHERE id = ?
`

type SetSessionPreviewURLParams struct {
	PreviewURL string
	UpdatedAt  time.Time
	ID         domain.SessionID
}

// preview_revision is bumped on every call (even when preview_url is unchanged)
// so a repeated `ao preview <same-url>` still trips the sessions_cdc_update
// trigger and the desktop browser panel re-navigates / refreshes.
func (q *Queries) SetSessionPreviewURL(ctx context.Context, arg SetSessionPreviewURLParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, setSessionPreviewURL, arg.PreviewURL, arg.UpdatedAt, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const updateSession = `-- name: UpdateSession :exec
UPDATE sessions SET
    issue_id = ?, kind = ?, harness = ?, display_name = ?,
    activity_state = ?, activity_last_at = ?, first_signal_at = ?, is_terminated = ?,
    branch = ?, workspace_path = ?, runtime_handle_id = ?, agent_session_id = ?, prompt = ?,
    preview_url = ?, preview_revision = ?, updated_at = ?
WHERE id = ?
`

type UpdateSessionParams struct {
	IssueID         domain.IssueID
	Kind            domain.SessionKind
	Harness         domain.AgentHarness
	DisplayName     string
	ActivityState   domain.ActivityState
	ActivityLastAt  time.Time
	FirstSignalAt   sql.NullTime
	IsTerminated    bool
	Branch          string
	WorkspacePath   string
	RuntimeHandleID string
	AgentSessionID  string
	Prompt          string
	PreviewURL      string
	PreviewRevision int64
	UpdatedAt       time.Time
	ID              domain.SessionID
}

func (q *Queries) UpdateSession(ctx context.Context, arg UpdateSessionParams) error {
	_, err := q.db.ExecContext(ctx, updateSession,
		arg.IssueID,
		arg.Kind,
		arg.Harness,
		arg.DisplayName,
		arg.ActivityState,
		arg.ActivityLastAt,
		arg.FirstSignalAt,
		arg.IsTerminated,
		arg.Branch,
		arg.WorkspacePath,
		arg.RuntimeHandleID,
		arg.AgentSessionID,
		arg.Prompt,
		arg.PreviewURL,
		arg.PreviewRevision,
		arg.UpdatedAt,
		arg.ID,
	)
	return err
}
