<?php

namespace onespace\tools\activeApi\models\onehealth;

use onespace\tools\activeApi\components\clients\OneHealthClient;
use Override;
use yii\web\NotFoundHttpException;

/**
 * @property string $id
 * @property integer $site_id
 * @property string $alert_id
 * @property string $correlation_id
 * @property integer $gateway_id
 * @property integer $button_id
 * @property integer $person_id
 * @property integer $event_code
 * @property array $event_data
 * @property array $correlated_data
 * @property string $created_at
 */
class Event extends \onespace\tools\activeApi\models\BaseActiveApiModel {
    public $id;
    public $site_id;
    public $alert_id;
    public $correlation_id;
    public $gateway_id;
    public $button_id;
    public $person_id;
    public $event_code;
    public $event_data;
    public $correlated_data;
    public $created_at;

    public function client(): OneHealthClient {
        return new OneHealthClient();
    }

    public function primaryKey(): string {
        return 'id';
    }

    public function baseEndpoints(): array {
        return [
            'v1' => 'api/v1/active/event',
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function rules() {
        return [
            [['alert_id', 'correlation_id', 'gateway_id', 'button_id', 'person_id', 'event_code', 'event_data', 'correlated_data'], 'default', 'value' => null],
            [[
                // 'id', // set automatically
                'site_id',
            ], 'required'], // set automatically
            [['gateway_id', 'button_id', 'person_id', 'event_code'], 'integer'],
            [['event_data', 'correlated_data'], 'safe'],
            [['id', 'alert_id'], 'string', 'max' => 26],
            [['correlation_id'], 'string', 'max' => 50],
            // [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'site_id' => 'Site ID',
            'alert_id' => 'Alert ID',
            'correlation_id' => 'Correlation ID',
            'gateway_id' => 'Gateway ID',
            'button_id' => 'Button ID',
            'person_id' => 'Person ID',
            'event_code' => 'Event Code',
            'event_data' => 'Event Data',
            'correlated_data' => 'Correlated Data',
            'created_at' => 'Created At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function attributeHints() {
        return [
            'id' => 'ULID Primary Key',
            'site_id' => 'FK: Site',
            'alert_id' => 'FK: Alerts, if a formal alert has been identified',
            'correlation_id' => 'Hardware ID grouping a specific event',
            'gateway_id' => 'FK: Health Gateway – the gateway which received this event',
            'button_id' => 'FK: Button – the button which generated this event',
            'person_id' => 'FK: People, the person who generated the event',
            'event_code' => 'The event code from the hardware',
            'event_data' => 'A snapshot of relevant tables at the time of the event',
            'correlated_data' => 'The raw payload as received',
            'created_at' => 'When the event was created',
        ];
    }

    public static function findById(string $id): static {
        $model = self::findOne($id);
        if ($model === null) {
            throw new NotFoundHttpException("Event not found");
        }
        return $model;
    }
}
