<?php

namespace onespace\tools\activeApi\components\clients;

use onespace\tools\rabbit\components\Rabbit;
use Yii;
use yii\helpers\Json;

class PeopleClient extends \onespace\tools\activeApi\components\clients\base\BaseOneSpaceClient {
    public function serverUrl(): string {
        return Yii::$app->params['serviceUrls']['people'];
    }

    /**
     * When a record state changes, this needs to be called to emit back to
     * the People Microservice
     * 
     * @param string $id    The relevant person GUID.
     * @param bool $enabled Whether or not the action event caused a record
     *                      to be enabled or disabled.
     */
    public function registerPersonStateCheck(string $id, bool $enabled): void {
        $time = time();

        /** @var Rabbit $rabbit */
        $rabbit = Yii::$app->rabbit;

        $rabbit->outboxSendMessage(
            "onespace.system",
            Json::encode([
                'event' => [
                    'type'      => 'create',
                    'timestamp' => $time,
                ],
                'entity' => [
                    'type'       => 'person-state-check',
                    'primaryKey' => ['id' => $id],
                    'personId' => $id,
                    'recordState' => $enabled,
                ],
            ]),
            "v1.json.person.status.{$id}",
            timestamp: $time,
        );
    }
}
