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

package gen

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

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

const archiveProject = `-- name: ArchiveProject :execrows
UPDATE projects SET archived_at = ? WHERE id = ? AND archived_at IS NULL
`

type ArchiveProjectParams struct {
	ArchivedAt sql.NullTime
	ID         domain.ProjectID
}

func (q *Queries) ArchiveProject(ctx context.Context, arg ArchiveProjectParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, archiveProject, arg.ArchivedAt, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const findProjectByPath = `-- name: FindProjectByPath :one
SELECT id, path, repo_origin_url, display_name, registered_at, archived_at, config, kind
FROM projects WHERE path = ? AND archived_at IS NULL
`

func (q *Queries) FindProjectByPath(ctx context.Context, path string) (Project, error) {
	row := q.db.QueryRowContext(ctx, findProjectByPath, path)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Path,
		&i.RepoOriginURL,
		&i.DisplayName,
		&i.RegisteredAt,
		&i.ArchivedAt,
		&i.Config,
		&i.Kind,
	)
	return i, err
}

const getProject = `-- name: GetProject :one
SELECT id, path, repo_origin_url, display_name, registered_at, archived_at, config, kind
FROM projects WHERE id = ?
`

func (q *Queries) GetProject(ctx context.Context, id domain.ProjectID) (Project, error) {
	row := q.db.QueryRowContext(ctx, getProject, id)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Path,
		&i.RepoOriginURL,
		&i.DisplayName,
		&i.RegisteredAt,
		&i.ArchivedAt,
		&i.Config,
		&i.Kind,
	)
	return i, err
}

const listProjects = `-- name: ListProjects :many
SELECT id, path, repo_origin_url, display_name, registered_at, archived_at, config, kind
FROM projects WHERE archived_at IS NULL ORDER BY id
`

func (q *Queries) ListProjects(ctx context.Context) ([]Project, error) {
	rows, err := q.db.QueryContext(ctx, listProjects)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Project{}
	for rows.Next() {
		var i Project
		if err := rows.Scan(
			&i.ID,
			&i.Path,
			&i.RepoOriginURL,
			&i.DisplayName,
			&i.RegisteredAt,
			&i.ArchivedAt,
			&i.Config,
			&i.Kind,
		); 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 upsertProject = `-- name: UpsertProject :exec
INSERT INTO projects (id, path, repo_origin_url, display_name, registered_at, archived_at, config, kind)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (id) DO UPDATE SET
    path = excluded.path,
    repo_origin_url = excluded.repo_origin_url,
    display_name = excluded.display_name,
    archived_at = excluded.archived_at,
    config = excluded.config,
    kind = excluded.kind
`

type UpsertProjectParams struct {
	ID            domain.ProjectID
	Path          string
	RepoOriginURL string
	DisplayName   string
	RegisteredAt  time.Time
	ArchivedAt    sql.NullTime
	Config        sql.NullString
	Kind          string
}

func (q *Queries) UpsertProject(ctx context.Context, arg UpsertProjectParams) error {
	_, err := q.db.ExecContext(ctx, upsertProject,
		arg.ID,
		arg.Path,
		arg.RepoOriginURL,
		arg.DisplayName,
		arg.RegisteredAt,
		arg.ArchivedAt,
		arg.Config,
		arg.Kind,
	)
	return err
}
