<?php

namespace objects;

use classes\db;
use classes\object_property;
use Exception;
use traits\db_object_t;

class department_notification_sms_o extends db
{
    use db_object_t;

    /**
     * The department id
     * @var object_property $department The department id
     */
    public object_property $department;
    /**
     * The label
     * @var object_property $label The label
     */
    public object_property $label;
    /**
     * The country code (e.g. 31 for +31)
     * @var object_property $phone_country_code The country code (e.g. 31 for +31)
     */
    public object_property $phone_country_code;
    /**
     * The phone number
     * @var object_property $phone The phone number
     */
    public object_property $phone;
    /**
     * The verified at date
     * @var object_property $verified_at The verified at date
     */
    public object_property $verified_at;
    /**
     * The enabled property
     * @var object_property $enabled The enabled property
     */
    public object_property $enabled;
    /**
     * The verification code
     * @var object_property $verification_code The verification code
     */
    public object_property $verification_code;
    /**
     * The created at date
     * @var object_property $created_at The created at date
     */
    public object_property $created_at;

    /**
     * Decode the data
     * @param string $data The data JSON encoded
     * @return array The data
     */
    public static function decodeData(string $data): array
    {
        return json_decode($data, true);
    }

    public function structure(): void
    {
        $this->setTable('department_notification_sms');
    }

    /**
     * Add a department notification SMS object
     * @param int $department The department id
     * @param array $data
     * @return void
     * @throws Exception If the object was not created successfully
     */
    public function add(int $department, array $data): void
    {
        global /** @var db $db */
        $db;
        // Set the default values
        $data_default = [
            'label' => '',
            'phone_country_code' => 45,
            'phone' => 0,
            'enabled' => 1,
        ];
        // Merge the default values with the data
        $data = array_merge($data_default, $data);
        // Sanitize the input
        $data['label'] = (string)$db->escape_string($data['label']);
        $data['phone_country_code'] = (int)$data['phone_country_code'];
        $data['phone'] = (int)$db->escape_string($data['phone']);
        // Add the object
        $tmp_id = self::add_object([
            ...$data,
            'department' => (int)$department,
        ]);
        if (!$tmp_id) {
            throw new Exception('The object was not created successfully.');
        }
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
    }

    public function getObjectProperties(): void
    {
        $this->department = new object_property($this->table, $this->id, 'department', 'int', false);
        $this->label = new object_property($this->table, $this->id, 'label', 'string', false);
        $this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false);
        $this->phone = new object_property($this->table, $this->id, 'phone', 'int', false);
        $this->verified_at = new object_property($this->table, $this->id, 'verified_at', 'datetime', false);
        $this->enabled = new object_property($this->table, $this->id, 'enabled', 'bool', false);
        $this->verification_code = new object_property($this->table, $this->id, 'verification_code', 'string', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'datetime', false);
    }

    public function objectChanged(): void
    {
        //TODO: Add cache invalidation
    }

    /**
     * Get the object as an array
     * @return array The object as an array
     */
    public function asArray(): array
    {
        return [
            'id' => $this->id,
            'department' => $this->department->value(),
            'label' => $this->label->value(),
            'phone_country_code' => $this->phone_country_code->value(),
            'phone' => $this->phone->value(),
            'verified_at' => $this->verified_at->value(),
            'enabled' => $this->enabled->value(),
            'created_at' => $this->created_at->value(),
        ];
    }

    /**
     * Get the department active phone numbers
     * @param int $id The department id
     * @return array The department active phone numbers
     */
    public function getDepartmentActivePhoneNumbers(int $id): array
    {
        $tmp_phones = [];
        $result = self::getFieldsWhere([
            'department' => $id,
            'enabled' => 1,
        ], [
            'phone_country_code',
            'phone',
        ]);
        if ($result) {
            foreach ( $result as $row ) {
                $tmp_phones[] = '' . $row['phone_country_code'] . $row['phone'];
            }
        }
        return $tmp_phones;
    }
}