-- name: ListIncoming :many
SELECT * FROM "Incoming"
ORDER BY "id";

-- name: CreateIncoming :one
INSERT INTO "Incoming" (
    "incident_number", "description", "short_description", "urgency", "impact", "state", "external_id", "work_notes", "assignment_group", "assigned_to", "category", "subcategory", "created_at"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: CreateIncident :one
INSERT INTO incidents (
    "description", "short_description", "urgency", "impact", "state", "external_id", "assignment_group", "assigned_to", "category", "subcategory", "created_at", "sys_id"
) VALUES(
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: UpdateIncidentNumber :exec
UPDATE incidents
SET incident_number = sqlc.arg('incidentNumber')
WHERE external_id = sqlc.arg('externalId');

-- name: UpdateIncidentState :exec
UPDATE incidents
SET state = sqlc.arg('state')
WHERE incident_number = sqlc.arg('incidentNumber');

-- name: UpdateIncident :exec
UPDATE incidents
SET
    external_id = ?,
    description = ?,
    short_description = ?,
    urgency = ?,
    impact = ?,
    state = ?,
    assignment_group = ?,
    assigned_to = ?,
    category = ?,
    subcategory = ?
WHERE incident_number = ?;

-- name: GetIncident :one
SELECT * from incidents
WHERE incident_number = sqlc.arg('incidentNumber');

-- name: ListIncidents :many
SELECT * FROM incidents;

-- name: GetIncidentReport :many
SELECT incidents.*, GROUP_CONCAT(worknotes.note, '<br />') AS all_notes
FROM incidents LEFT JOIN worknotes ON incidents.incident_number = worknotes.incident_number;

-- name: CreateWorkNote :one
INSERT into worknotes (
        "incident_number", "note"
) VALUES(
    ?, ?
)
RETURNING *;