// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: entities.sql

package sqlcgen

import (
	"context"
	"time"

	"github.com/google/uuid"
)

const getEntityByID = `-- name: GetEntityByID :one

SELECT e.id, e.slug, e.type, e.name, e.state, e.attributes, e.maintenance_until, e.version, e.created_at, e.updated_at, e.enrolled_at, e.enrolled_by FROM entities e WHERE e.id = $1
`

// Entity read + mutation queries (API paths). Aliased `e` throughout to
// avoid ambiguity with joined tables.
func (q *Queries) GetEntityByID(ctx context.Context, id uuid.UUID) (Entity, error) {
	row := q.db.QueryRow(ctx, getEntityByID, id)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Type,
		&i.Name,
		&i.State,
		&i.Attributes,
		&i.MaintenanceUntil,
		&i.Version,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EnrolledAt,
		&i.EnrolledBy,
	)
	return i, err
}

const getEntityBySlug = `-- name: GetEntityBySlug :one
SELECT e.id, e.slug, e.type, e.name, e.state, e.attributes, e.maintenance_until, e.version, e.created_at, e.updated_at, e.enrolled_at, e.enrolled_by FROM entities e WHERE e.slug = $1
`

func (q *Queries) GetEntityBySlug(ctx context.Context, slug string) (Entity, error) {
	row := q.db.QueryRow(ctx, getEntityBySlug, slug)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Type,
		&i.Name,
		&i.State,
		&i.Attributes,
		&i.MaintenanceUntil,
		&i.Version,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EnrolledAt,
		&i.EnrolledBy,
	)
	return i, err
}

const insertEntity = `-- name: InsertEntity :one
INSERT INTO entities (id, slug, type, name, state, attributes)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, slug, type, name, state, attributes, maintenance_until, version, created_at, updated_at, enrolled_at, enrolled_by
`

type InsertEntityParams struct {
	ID         uuid.UUID
	Slug       string
	Type       string
	Name       string
	State      *string
	Attributes []byte
}

func (q *Queries) InsertEntity(ctx context.Context, arg InsertEntityParams) (Entity, error) {
	row := q.db.QueryRow(ctx, insertEntity,
		arg.ID,
		arg.Slug,
		arg.Type,
		arg.Name,
		arg.State,
		arg.Attributes,
	)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Type,
		&i.Name,
		&i.State,
		&i.Attributes,
		&i.MaintenanceUntil,
		&i.Version,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EnrolledAt,
		&i.EnrolledBy,
	)
	return i, err
}

const listEntities = `-- name: ListEntities :many
WITH RECURSIVE tt AS (
    SELECT name FROM entity_types WHERE $7::text IS NULL OR name = $7
    UNION
    SELECT et.name FROM entity_types et JOIN tt ON et.parent_type = tt.name
    WHERE $7::text IS NOT NULL
)
SELECT e.id, e.slug, e.type, e.name, e.state, e.attributes, e.maintenance_until, e.version, e.created_at, e.updated_at, e.enrolled_at, e.enrolled_by FROM entities e
JOIN entity_types et ON et.name = e.type
WHERE e.type IN (SELECT name FROM tt)
  AND ($1::text IS NULL OR e.state = $1)
  AND ($2::text IS NULL OR et.domain = $2)
  AND ($3::text IS NULL OR et.layer = $3)
  AND ($4::text IS NULL
       OR e.slug ILIKE '%'||$4||'%'
       OR e.name ILIKE '%'||$4||'%')
  AND ($5::text IS NULL OR e.slug > $5)
ORDER BY e.slug
LIMIT $6
`

type ListEntitiesParams struct {
	State  *string
	Domain *string
	Layer  *string
	Q      *string
	Cursor *string
	Lim    int32
	Type   *string
}

func (q *Queries) ListEntities(ctx context.Context, arg ListEntitiesParams) ([]Entity, error) {
	rows, err := q.db.Query(ctx, listEntities,
		arg.State,
		arg.Domain,
		arg.Layer,
		arg.Q,
		arg.Cursor,
		arg.Lim,
		arg.Type,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Entity
	for rows.Next() {
		var i Entity
		if err := rows.Scan(
			&i.ID,
			&i.Slug,
			&i.Type,
			&i.Name,
			&i.State,
			&i.Attributes,
			&i.MaintenanceUntil,
			&i.Version,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.EnrolledAt,
			&i.EnrolledBy,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const mergeEntityAttributes = `-- name: MergeEntityAttributes :execrows
UPDATE entities SET
    attributes = attributes || $1::jsonb,
    updated_at = now()
WHERE slug = $2
`

type MergeEntityAttributesParams struct {
	Patch []byte
	Slug  string
}

// Shallow-merge a JSON patch into an entity's attributes (the
// update_entity_attributes MCP/HTTP surface). Replaces the raw
// `attributes = attributes || $2::jsonb` used in entity_tools.go.
func (q *Queries) MergeEntityAttributes(ctx context.Context, arg MergeEntityAttributesParams) (int64, error) {
	result, err := q.db.Exec(ctx, mergeEntityAttributes, arg.Patch, arg.Slug)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const setEntityState = `-- name: SetEntityState :execrows
UPDATE entities SET
    state = $1,
    updated_at = now()
WHERE id = $2
`

type SetEntityStateParams struct {
	State *string
	ID    uuid.UUID
}

// Set an entity's lifecycle state by id (the set_entity_state surface, run
// after db.ValidateTransition). Replaces the raw
// `UPDATE entities SET state = $2 ... WHERE id = $1`.
func (q *Queries) SetEntityState(ctx context.Context, arg SetEntityStateParams) (int64, error) {
	result, err := q.db.Exec(ctx, setEntityState, arg.State, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const updateEntity = `-- name: UpdateEntity :one
UPDATE entities SET
    name = COALESCE($1, name),
    state = COALESCE($2, state),
    attributes = COALESCE($3, attributes),
    maintenance_until = CASE WHEN $4::bool
                             THEN $5 ELSE maintenance_until END,
    version = version + 1,
    updated_at = now()
WHERE id = $6 AND version = $7
RETURNING id, slug, type, name, state, attributes, maintenance_until, version, created_at, updated_at, enrolled_at, enrolled_by
`

type UpdateEntityParams struct {
	Name             *string
	State            *string
	Attributes       []byte
	SetMaintenance   bool
	MaintenanceUntil *time.Time
	ID               uuid.UUID
	Version          int32
}

func (q *Queries) UpdateEntity(ctx context.Context, arg UpdateEntityParams) (Entity, error) {
	row := q.db.QueryRow(ctx, updateEntity,
		arg.Name,
		arg.State,
		arg.Attributes,
		arg.SetMaintenance,
		arg.MaintenanceUntil,
		arg.ID,
		arg.Version,
	)
	var i Entity
	err := row.Scan(
		&i.ID,
		&i.Slug,
		&i.Type,
		&i.Name,
		&i.State,
		&i.Attributes,
		&i.MaintenanceUntil,
		&i.Version,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.EnrolledAt,
		&i.EnrolledBy,
	)
	return i, err
}
