<?php

namespace onespace\tools\activeApi\models\onehealth;

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

/**
 * @property integer $id
 * @property string $guid
 * @property integer $logical_address
 * @property integer $site_id
 * @property string $name
 * @property string $timezone
 * @property string $created_at
 * @property string $updated_at
 */

class HealthGateway extends \onespace\tools\activeApi\models\BaseActiveApiModel {
    public $id;
    public $guid;
    public $logical_address;
    public $site_id;
    public $name;
    public $timezone;
    public $created_at;
    public $updated_at;

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

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

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

    /**
     * @inheritdoc
     */
    #[\Override]
    public function rules() {
        $parentRules = parent::rules();
        return ArrayHelper::merge($parentRules, [
            [['name'], 'default', 'value' => 'New Gateway'],
            [['timezone'], 'default', 'value' => '+2:00'],
            [['guid', 'logical_address', 'site_id'], 'required'],
            [['logical_address', 'site_id'], 'integer'],
            [['guid'], 'string', 'max' => 36],
            [['name'], 'string', 'max' => 250],
            [['timezone'], 'string', 'max' => 10],
            [['guid'], 'unique'],
        ]);
    }

    public static function findById(int $id): static {
        $model = self::findOne(['id' => $id]);
        if ($model === null) {
            throw new NotFoundHttpException("Health Gateway not found");
        }
        return $model;
    }

    public static function findByGuid(string $guid): static {
        $model = self::findOne(['guid' => $guid]);
        if ($model === null) {
            throw new NotFoundHttpException("Health Gateway not found");
        }
        return $model;
    }

    public static function findByLogicalAddress(string $id): static {
        $model = self::findOne(['logical_address' => $id]);
        if ($model === null) {
            throw new NotFoundHttpException("Health Gateway not found");
        }
        return $model;
    }
}
