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

package gen

import (
	"context"
	"time"

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

const deleteSessionWorktrees = `-- name: DeleteSessionWorktrees :exec
DELETE FROM session_worktrees WHERE session_id = ?
`

func (q *Queries) DeleteSessionWorktrees(ctx context.Context, sessionID domain.SessionID) error {
	_, err := q.db.ExecContext(ctx, deleteSessionWorktrees, sessionID)
	return err
}

const deleteWorkspaceReposByProject = `-- name: DeleteWorkspaceReposByProject :exec
DELETE FROM workspace_repos WHERE project_id = ?
`

func (q *Queries) DeleteWorkspaceReposByProject(ctx context.Context, projectID domain.ProjectID) error {
	_, err := q.db.ExecContext(ctx, deleteWorkspaceReposByProject, projectID)
	return err
}

const getSessionWorktree = `-- name: GetSessionWorktree :one
SELECT session_id, repo_name, branch, base_sha, worktree_path, preserved_ref, state
FROM session_worktrees
WHERE session_id = ? AND repo_name = ?
`

type GetSessionWorktreeParams struct {
	SessionID domain.SessionID
	RepoName  string
}

func (q *Queries) GetSessionWorktree(ctx context.Context, arg GetSessionWorktreeParams) (SessionWorktree, error) {
	row := q.db.QueryRowContext(ctx, getSessionWorktree, arg.SessionID, arg.RepoName)
	var i SessionWorktree
	err := row.Scan(
		&i.SessionID,
		&i.RepoName,
		&i.Branch,
		&i.BaseSha,
		&i.WorktreePath,
		&i.PreservedRef,
		&i.State,
	)
	return i, err
}

const listSessionWorktrees = `-- name: ListSessionWorktrees :many
SELECT session_id, repo_name, branch, base_sha, worktree_path, preserved_ref, state
FROM session_worktrees
WHERE session_id = ?
ORDER BY CASE WHEN repo_name = '__root__' THEN 0 ELSE 1 END, repo_name
`

func (q *Queries) ListSessionWorktrees(ctx context.Context, sessionID domain.SessionID) ([]SessionWorktree, error) {
	rows, err := q.db.QueryContext(ctx, listSessionWorktrees, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SessionWorktree{}
	for rows.Next() {
		var i SessionWorktree
		if err := rows.Scan(
			&i.SessionID,
			&i.RepoName,
			&i.Branch,
			&i.BaseSha,
			&i.WorktreePath,
			&i.PreservedRef,
			&i.State,
		); 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 listWorkspaceRepos = `-- name: ListWorkspaceRepos :many
SELECT project_id, name, relative_path, repo_origin_url, registered_at
FROM workspace_repos
WHERE project_id = ?
ORDER BY name
`

func (q *Queries) ListWorkspaceRepos(ctx context.Context, projectID domain.ProjectID) ([]WorkspaceRepo, error) {
	rows, err := q.db.QueryContext(ctx, listWorkspaceRepos, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []WorkspaceRepo{}
	for rows.Next() {
		var i WorkspaceRepo
		if err := rows.Scan(
			&i.ProjectID,
			&i.Name,
			&i.RelativePath,
			&i.RepoOriginURL,
			&i.RegisteredAt,
		); 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 upsertSessionWorktree = `-- name: UpsertSessionWorktree :exec
INSERT INTO session_worktrees (session_id, repo_name, branch, base_sha, worktree_path, preserved_ref, state)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (session_id, repo_name) DO UPDATE SET
    branch = excluded.branch,
    base_sha = excluded.base_sha,
    worktree_path = excluded.worktree_path,
    preserved_ref = excluded.preserved_ref,
    state = excluded.state
`

type UpsertSessionWorktreeParams struct {
	SessionID    domain.SessionID
	RepoName     string
	Branch       string
	BaseSha      string
	WorktreePath string
	PreservedRef string
	State        string
}

func (q *Queries) UpsertSessionWorktree(ctx context.Context, arg UpsertSessionWorktreeParams) error {
	_, err := q.db.ExecContext(ctx, upsertSessionWorktree,
		arg.SessionID,
		arg.RepoName,
		arg.Branch,
		arg.BaseSha,
		arg.WorktreePath,
		arg.PreservedRef,
		arg.State,
	)
	return err
}

const upsertWorkspaceRepo = `-- name: UpsertWorkspaceRepo :exec
INSERT INTO workspace_repos (project_id, name, relative_path, repo_origin_url, registered_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (project_id, name) DO UPDATE SET
    relative_path = excluded.relative_path,
    repo_origin_url = excluded.repo_origin_url,
    registered_at = excluded.registered_at
`

type UpsertWorkspaceRepoParams struct {
	ProjectID     domain.ProjectID
	Name          string
	RelativePath  string
	RepoOriginURL string
	RegisteredAt  time.Time
}

func (q *Queries) UpsertWorkspaceRepo(ctx context.Context, arg UpsertWorkspaceRepoParams) error {
	_, err := q.db.ExecContext(ctx, upsertWorkspaceRepo,
		arg.ProjectID,
		arg.Name,
		arg.RelativePath,
		arg.RepoOriginURL,
		arg.RegisteredAt,
	)
	return err
}
