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

-- name: GetReportInventory :many
SELECT * FROM "Inventory"
ORDER BY "CreationTime";

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

-- name: GetInventoryByVcenter :many
SELECT * FROM "Inventory"
WHERE "Vcenter" = ?;

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

-- name: GetInventoryVmUuid :one
SELECT * FROM "Inventory"
WHERE "VmUuid" = sqlc.arg('vmUuid')  AND "Datacenter" = sqlc.arg('datacenterName');

-- name: GetInventoryVcUrl :many
SELECT * FROM "Inventory"
WHERE "Vcenter" = sqlc.arg('vc');

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

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

-- name: InventoryUpdate :exec
UPDATE "Inventory"
SET "VmUuid" = sqlc.arg('uuid'), "SrmPlaceholder" = sqlc.arg('srmPlaceholder')
WHERE "Iid" = sqlc.arg('iid');

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

-- name: InventoryCleanup :exec
DELETE FROM "Inventory"
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName')
RETURNING *;

-- name: InventoryCleanupVcenter :exec
DELETE FROM "Inventory"
WHERE "Vcenter" = sqlc.arg('vc')
RETURNING *;

-- name: InventoryCleanupTemplates :exec
DELETE FROM "Inventory"
WHERE "IsTemplate" = "TRUE"
RETURNING *;

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

-- name: GetReportUpdates :many
SELECT * FROM "Updates"
ORDER BY "UpdateTime";

-- name: GetVmUpdates :many
SELECT * FROM "Updates"
WHERE "UpdateType" = sqlc.arg('updateType') AND "InventoryId" = sqlc.arg('InventoryId');

-- name: CleanupUpdates :exec
DELETE FROM "Updates"
WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime')
RETURNING *;

-- name: CleanupUpdatesNullVm :exec
DELETE FROM "Updates"
WHERE "InventoryId" IS NULL
RETURNING *;

-- name: CreateEvent :one
INSERT INTO "Events" (
    "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventType", "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
AND "EventTime" > sqlc.arg('eventTime')
ORDER BY "EventTime";

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

-- name: CreateInventoryHistory :one
INSERT INTO "InventoryHistory" (
    "InventoryId", "ReportDate", "UpdateTime", "PreviousVcpus", "PreviousRam", "PreviousResourcePool", "PreviousProvisionedDisk"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;