;; Relations service — inter-service action endpoints

(defaction attach-child (&key parent-type parent-id child-type child-id
                          label sort-order relation-type metadata)
  "Create or revive a container relation."
  (service "relations" "attach-child"
    :parent-type parent-type :parent-id parent-id
    :child-type child-type :child-id child-id
    :label label :sort-order sort-order
    :relation-type relation-type :metadata metadata))

(defaction detach-child (&key parent-type parent-id child-type child-id relation-type)
  "Soft-delete a container relation."
  (let ((deleted (service "relations" "detach-child"
                   :parent-type parent-type :parent-id parent-id
                   :child-type child-type :child-id child-id
                   :relation-type relation-type)))
    {"deleted" deleted}))

(defaction relate (&key relation-type from-id to-id label sort-order metadata)
  "Create a typed relation with registry validation and cardinality enforcement."
  (service "relations" "relate"
    :relation-type relation-type :from-id from-id :to-id to-id
    :label label :sort-order sort-order :metadata metadata))

(defaction unrelate (&key relation-type from-id to-id)
  "Remove a typed relation with registry validation."
  (let ((deleted (service "relations" "unrelate"
                   :relation-type relation-type
                   :from-id from-id :to-id to-id)))
    {"deleted" deleted}))

(defaction can-relate (&key relation-type from-id)
  "Check if a relation can be created (cardinality, registry validation)."
  (service "relations" "can-relate"
    :relation-type relation-type :from-id from-id))
