-- name: ListInventory :many
SELECT * FROM "Inventory"
ORDER BY "Name";

-- name: GetInventoryByName :many
SELECT * FROM "Inventory"
WHERE "Name" = ?;

-- name: GetInventoryVmId :one
SELECT * FROM "Inventory"
WHERE "VmId" = sqlc.arg('vmId')  AND "Datacenter" = sqlc.arg('datacenterName');

-- name: GetInventoryEventId :one
SELECT * FROM "Inventory"
WHERE "EventId" = ? LIMIT 1;

-- name: CreateInventory :one
INSERT INTO "Inventory" (
    "Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "IsTemplate", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder", "PowerState"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: InventoryMarkDeleted :exec
UPDATE "Inventory"
SET "DeletionTime" = sqlc.arg('deletionTime')
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');

-- name: CreateUpdate :one
INSERT INTO "Updates" (
    "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: CreateEvent :one
INSERT INTO "Events" (
    "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: ListEvents :many
SELECT * FROM "Events"
ORDER BY "EventTime";

-- name: ListUnprocessedEvents :many
SELECT * FROM "Events"
WHERE "Processed" = 0
ORDER BY "EventTime";

-- name: UpdateEventsProcessed :exec
UPDATE "Events"
SET "Processed" = 1
WHERE "Eid" = sqlc.arg('eid');