<?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 integer $press_event_id
 * @property integer $initiating_button_id
 * @property integer $responding_button_id
 * @property integer $generated_by
 * @property integer $responded_by
 * @property integer $state
 * @property integer $alert_reason_id
 * @property integer $response_action_id
 * @property string $notes
 * @property string $acknowledged_at
 * @property string $responded_at
 * @property string $closed_at
 * @property string $created_at
 * @property string $updated_at
 */
class Alert extends \onespace\tools\activeApi\models\BaseActiveApiModel {
    /** The event has been initiated by a user but has not yet been acknowledged by anyone */
    public const int STATE_UNACKNOWLEDGED = 10;
    /** The event has been ackowledged by a health care worker */
    public const int STATE_ACKNOWLEDGED = 20;
    /** The event is being attended to by a health care worker */
    public const int STATE_ATTENDING = 30;
    /** The event has been completed by a health care worker */
    public const int STATE_COMPLETE = 40;
    /** The event has been cancelled by a health care worker or patient */
    public const int STATE_CANCELLED = 50;

    public $id;
    public $site_id;
    public $press_event_id;
    public $initiating_button_id;
    public $responding_button_id;
    public $generated_by;
    public $responded_by;
    public $state;
    public $alert_reason_id;
    public $response_action_id;
    public $notes;
    public $acknowledged_at;
    public $responded_at;
    public $closed_at;
    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/alert',
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function rules() {
        return [
            [['responding_button_id', 'generated_by', 'responded_by', 'alert_reason_id', 'response_action_id', 'notes', 'acknowledged_at', 'responded_at', 'closed_at'], 'default', 'value' => null],
            [[
                // 'id', // Set automatically
                'press_event_id',
                'initiating_button_id',
                'site_id',
                'state',
            ], 'required'],
            [['site_id', 'press_event_id', 'initiating_button_id', 'responding_button_id', 'generated_by', 'responded_by', 'state', 'alert_reason_id', 'response_action_id'], 'integer'],
            [['notes'], 'string'],
            [['acknowledged_at', 'responded_at', 'closed_at'], 'safe'],
            [['id'], 'string', 'max' => 26],
            // [['press_event_id', 'initiating_button_id'], 'unique', 'targetAttribute' => ['press_event_id', 'initiating_button_id']],
            // [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'site_id' => 'Site ID',
            'press_event_id' => 'Press Event ID',
            'initiating_button_id' => 'Initiating Button ID',
            'responding_button_id' => 'Responding Button ID',
            'generated_by' => 'Generated By',
            'responded_by' => 'Responded By',
            'state' => 'State',
            'alert_reason_id' => 'Alert Reason ID',
            'response_action_id' => 'Response Action ID',
            'notes' => 'Notes',
            'created_at' => 'Created At',
            'acknowledged_at' => 'Acknowledged At',
            'responded_at' => 'Responded At',
            'closed_at' => 'Closed At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    #[Override]
    public function attributeHints() {
        return [
            'id' => 'ULID Primary Key',
            'site_id' => 'FK: Site',
            'press_event_id' => 'The event code from the hardware',
            'initiating_button_id' => 'The button which initiated the alert',
            'responding_button_id' => 'The button which responded to the alert with a button',
            'generated_by' => 'FK: People, who generated the alert',
            'responded_by' => 'FK: People, who marked themselves as responding',
            'state' => 'Enum: 1=Unacknowledged, 2=Acknowledged, 3=Attending, 4=Complete, 5=Cancelled',
            'alert_reason_id' => 'FK: Alert Reasons',
            'response_action_id' => 'FK: Response Actions',
            'notes' => 'Any general notes on the action.',
            'created_at' => 'When this alert was created. Often generated through a button press',
            'acknowledged_at' => 'When the health care worker acknowledged the alert on the dashboard',
            'responded_at' => 'When the health care worker began attending the patient. Often generated through a button press',
            'closed_at' => 'When the alert was closed by a relevant health care worker',
            'updated_at' => 'When this alert was last updated',
        ];
    }

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