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

package workspacesqlc

import (
	"context"

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

const createWorkspace = `-- name: CreateWorkspace :one
INSERT INTO workspaces (
  id, project_id, slug, name, description,
  git_remote_url, default_branch, main_path, sync_status
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
RETURNING id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at, active_main_session_id
`

type CreateWorkspaceParams struct {
	ID            pgtype.UUID `json:"id"`
	ProjectID     pgtype.UUID `json:"project_id"`
	Slug          string      `json:"slug"`
	Name          string      `json:"name"`
	Description   string      `json:"description"`
	GitRemoteUrl  string      `json:"git_remote_url"`
	DefaultBranch string      `json:"default_branch"`
	MainPath      string      `json:"main_path"`
	SyncStatus    string      `json:"sync_status"`
}

func (q *Queries) CreateWorkspace(ctx context.Context, arg CreateWorkspaceParams) (Workspace, error) {
	row := q.db.QueryRow(ctx, createWorkspace,
		arg.ID,
		arg.ProjectID,
		arg.Slug,
		arg.Name,
		arg.Description,
		arg.GitRemoteUrl,
		arg.DefaultBranch,
		arg.MainPath,
		arg.SyncStatus,
	)
	var i Workspace
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.GitRemoteUrl,
		&i.DefaultBranch,
		&i.MainPath,
		&i.SyncStatus,
		&i.LastSyncedAt,
		&i.LastSyncError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ActiveMainSessionID,
	)
	return i, err
}

const deleteWorkspace = `-- name: DeleteWorkspace :exec
DELETE FROM workspaces WHERE id = $1
`

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

const getWorkspaceByID = `-- name: GetWorkspaceByID :one
SELECT id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at, active_main_session_id FROM workspaces WHERE id = $1
`

func (q *Queries) GetWorkspaceByID(ctx context.Context, id pgtype.UUID) (Workspace, error) {
	row := q.db.QueryRow(ctx, getWorkspaceByID, id)
	var i Workspace
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.GitRemoteUrl,
		&i.DefaultBranch,
		&i.MainPath,
		&i.SyncStatus,
		&i.LastSyncedAt,
		&i.LastSyncError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ActiveMainSessionID,
	)
	return i, err
}

const getWorkspaceBySlug = `-- name: GetWorkspaceBySlug :one
SELECT w.id, w.project_id, w.slug, w.name, w.description, w.git_remote_url, w.default_branch, w.main_path, w.sync_status, w.last_synced_at, w.last_sync_error, w.created_at, w.updated_at, w.active_main_session_id FROM workspaces w
JOIN projects p ON p.id = w.project_id
WHERE p.slug = $1 AND w.slug = $2
`

type GetWorkspaceBySlugParams struct {
	Slug   string `json:"slug"`
	Slug_2 string `json:"slug_2"`
}

func (q *Queries) GetWorkspaceBySlug(ctx context.Context, arg GetWorkspaceBySlugParams) (Workspace, error) {
	row := q.db.QueryRow(ctx, getWorkspaceBySlug, arg.Slug, arg.Slug_2)
	var i Workspace
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.GitRemoteUrl,
		&i.DefaultBranch,
		&i.MainPath,
		&i.SyncStatus,
		&i.LastSyncedAt,
		&i.LastSyncError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ActiveMainSessionID,
	)
	return i, err
}

const listFetchTargets = `-- name: ListFetchTargets :many
SELECT w.id, p.owner_id, w.name, w.main_path
  FROM workspaces w
  JOIN projects p ON p.id = w.project_id
 WHERE w.archived_at IS NULL
   AND w.sync_status IN ('idle','error')
 ORDER BY w.id
`

type ListFetchTargetsRow struct {
	ID       pgtype.UUID `json:"id"`
	OwnerID  pgtype.UUID `json:"owner_id"`
	Name     string      `json:"name"`
	MainPath string      `json:"main_path"`
}

func (q *Queries) ListFetchTargets(ctx context.Context) ([]ListFetchTargetsRow, error) {
	rows, err := q.db.Query(ctx, listFetchTargets)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListFetchTargetsRow
	for rows.Next() {
		var i ListFetchTargetsRow
		if err := rows.Scan(
			&i.ID,
			&i.OwnerID,
			&i.Name,
			&i.MainPath,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkspacesByProject = `-- name: ListWorkspacesByProject :many
SELECT id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at, active_main_session_id FROM workspaces WHERE project_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListWorkspacesByProject(ctx context.Context, projectID pgtype.UUID) ([]Workspace, error) {
	rows, err := q.db.Query(ctx, listWorkspacesByProject, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Workspace
	for rows.Next() {
		var i Workspace
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Slug,
			&i.Name,
			&i.Description,
			&i.GitRemoteUrl,
			&i.DefaultBranch,
			&i.MainPath,
			&i.SyncStatus,
			&i.LastSyncedAt,
			&i.LastSyncError,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ActiveMainSessionID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markFetchResult = `-- name: MarkFetchResult :exec
UPDATE workspaces
   SET sync_status = CASE WHEN $2::bool THEN 'idle' ELSE 'error' END,
       last_synced_at = CASE WHEN $2::bool THEN now() ELSE last_synced_at END,
       last_sync_error = $3,
       updated_at = now()
 WHERE id = $1
`

type MarkFetchResultParams struct {
	ID            pgtype.UUID `json:"id"`
	Column2       bool        `json:"column_2"`
	LastSyncError *string     `json:"last_sync_error"`
}

func (q *Queries) MarkFetchResult(ctx context.Context, arg MarkFetchResultParams) error {
	_, err := q.db.Exec(ctx, markFetchResult, arg.ID, arg.Column2, arg.LastSyncError)
	return err
}

const markSyncingCAS = `-- name: MarkSyncingCAS :execrows
UPDATE workspaces SET sync_status='syncing', updated_at=now()
 WHERE id=$1 AND sync_status IN ('idle','error')
`

func (q *Queries) MarkSyncingCAS(ctx context.Context, id pgtype.UUID) (int64, error) {
	result, err := q.db.Exec(ctx, markSyncingCAS, id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const resetStuckSyncStatuses = `-- name: ResetStuckSyncStatuses :many
UPDATE workspaces
SET sync_status = 'error',
    last_sync_error = 'process_restarted_during_' || sync_status,
    updated_at = now()
WHERE sync_status IN ('cloning', 'syncing')
RETURNING id, last_sync_error
`

type ResetStuckSyncStatusesRow struct {
	ID            pgtype.UUID `json:"id"`
	LastSyncError *string     `json:"last_sync_error"`
}

func (q *Queries) ResetStuckSyncStatuses(ctx context.Context) ([]ResetStuckSyncStatusesRow, error) {
	rows, err := q.db.Query(ctx, resetStuckSyncStatuses)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ResetStuckSyncStatusesRow
	for rows.Next() {
		var i ResetStuckSyncStatusesRow
		if err := rows.Scan(&i.ID, &i.LastSyncError); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateWorkspaceCore = `-- name: UpdateWorkspaceCore :one
UPDATE workspaces
SET name = $2, description = $3, default_branch = $4, updated_at = now()
WHERE id = $1
RETURNING id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at, active_main_session_id
`

type UpdateWorkspaceCoreParams struct {
	ID            pgtype.UUID `json:"id"`
	Name          string      `json:"name"`
	Description   string      `json:"description"`
	DefaultBranch string      `json:"default_branch"`
}

func (q *Queries) UpdateWorkspaceCore(ctx context.Context, arg UpdateWorkspaceCoreParams) (Workspace, error) {
	row := q.db.QueryRow(ctx, updateWorkspaceCore,
		arg.ID,
		arg.Name,
		arg.Description,
		arg.DefaultBranch,
	)
	var i Workspace
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.GitRemoteUrl,
		&i.DefaultBranch,
		&i.MainPath,
		&i.SyncStatus,
		&i.LastSyncedAt,
		&i.LastSyncError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ActiveMainSessionID,
	)
	return i, err
}

const updateWorkspaceSyncStatus = `-- name: UpdateWorkspaceSyncStatus :one
UPDATE workspaces
SET sync_status = $2,
    last_synced_at = $3,
    last_sync_error = $4,
    updated_at = now()
WHERE id = $1
RETURNING id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at, active_main_session_id
`

type UpdateWorkspaceSyncStatusParams struct {
	ID            pgtype.UUID        `json:"id"`
	SyncStatus    string             `json:"sync_status"`
	LastSyncedAt  pgtype.Timestamptz `json:"last_synced_at"`
	LastSyncError *string            `json:"last_sync_error"`
}

func (q *Queries) UpdateWorkspaceSyncStatus(ctx context.Context, arg UpdateWorkspaceSyncStatusParams) (Workspace, error) {
	row := q.db.QueryRow(ctx, updateWorkspaceSyncStatus,
		arg.ID,
		arg.SyncStatus,
		arg.LastSyncedAt,
		arg.LastSyncError,
	)
	var i Workspace
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Slug,
		&i.Name,
		&i.Description,
		&i.GitRemoteUrl,
		&i.DefaultBranch,
		&i.MainPath,
		&i.SyncStatus,
		&i.LastSyncedAt,
		&i.LastSyncError,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ActiveMainSessionID,
	)
	return i, err
}
