-- 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", "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 inventory_history (
    "InventoryId", "ReportDate", "UpdateTime", "PreviousVcpus", "PreviousRam", "PreviousResourcePool", "PreviousProvisionedDisk"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: SqliteTableExists :one
SELECT COUNT(1) AS count
FROM sqlite_master
WHERE type = 'table' AND name = sqlc.arg('table_name');

-- name: SqliteColumnExists :one
SELECT COUNT(1) AS count
FROM pragma_table_info
WHERE name = sqlc.arg('column_name');
