-- name: GetRoadmapSyncIdentityByWorkItem :one
SELECT id, shape, roadmap_milestone_path, roadmap_item_id, provider, tenant, project, work_item_id, parent_work_item_id, provider_revision, roadmap_revision, created_at, updated_at
FROM roadmap_sync_identities
WHERE provider = $1 AND tenant = $2 AND project = $3 AND work_item_id = $4;

-- name: GetRoadmapSyncIdentityByMilestoneID :one
SELECT id, shape, roadmap_milestone_path, roadmap_item_id, provider, tenant, project, work_item_id, parent_work_item_id, provider_revision, roadmap_revision, created_at, updated_at
FROM roadmap_sync_identities
WHERE shape = 'milestone' AND roadmap_item_id = $1;

-- name: UpsertRoadmapSyncIdentityByMilestoneID :one
INSERT INTO roadmap_sync_identities (
    shape, roadmap_milestone_path, roadmap_item_id, provider, tenant, project, work_item_id, parent_work_item_id, provider_revision, roadmap_revision
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (shape, roadmap_item_id) WHERE shape = 'milestone' AND roadmap_item_id <> ''
DO UPDATE SET
    roadmap_milestone_path = EXCLUDED.roadmap_milestone_path,
    provider = EXCLUDED.provider,
    tenant = EXCLUDED.tenant,
    project = EXCLUDED.project,
    work_item_id = EXCLUDED.work_item_id,
    parent_work_item_id = EXCLUDED.parent_work_item_id,
    provider_revision = EXCLUDED.provider_revision,
    roadmap_revision = EXCLUDED.roadmap_revision,
    updated_at = now()
RETURNING id, shape, roadmap_milestone_path, roadmap_item_id, provider, tenant, project, work_item_id, parent_work_item_id, provider_revision, roadmap_revision, created_at, updated_at;
