<?php

declare(strict_types=1);

namespace mirzaev\deeproots\models;

// Files of the project
use mirzaev\deeproots\models\core;

// Svoboda time
use svoboda\time\statement as svoboda;

// Baza database
use mirzaev\baza\database,
    mirzaev\baza\column,
    mirzaev\baza\record,
    mirzaev\baza\enumerations\encoding,
    mirzaev\baza\enumerations\type;

// Framework for Telegram
use Zanzara\Telegram\Type\User as telegram;

// Built-in libraries
use Exception as exception,
    RuntimeException as exception_runtime;

/**
 * Connection between account::class and telegram::class
 *
 * @package mirzaev\deeproots\models
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class connection extends core
{
    /**
     * File
     *
     * @var string $database Path to the database file
     */
    protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'connection.baza';

    /**
     * Database
     *
     * Identifier: The record identifier
     * Account: The account identifier
     * Telegram: The telegram account identifier
     * Updated: Timestamp of the last the record update
     * Created: Timestamp of the record creating
     *
     * @var database $database The database
     */
    public protected(set) database $database;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        // Initializing the database
        $this->database = new database()
            ->encoding(encoding::ascii)
            ->columns(
                new column('identifier', type::integer_unsigned),
                new column('account', type::integer_unsigned),
                new column('telegram', type::long_long_unsigned),
                new column('active', type::char),
                new column('updated', type::integer_unsigned),
                new column('created', type::integer_unsigned)
            )
            ->connect($this->file);
    }

    /**
     * Create
     *
     * Creates the record in the database
     *
     * @param int $account Identifier of the account
     * @param int $telegram Identifier of the telegram account
     * @param bool $active Is the connection active?
     *
     * @return int|false The record identifier, if created
     */
    public function create(int $account, int $telegram, bool $active = true): int|false
    {
        // Initializing the identifier
        $identifier = $this->database->count() + 1;

        // Initializing the record
        $record = $this->database->record(
            $identifier,
            $account,
            $telegram,
            (int) $active,
            svoboda::timestamp(),
            svoboda::timestamp()
        );

        // Creating the record in the database
        $created = $this->database->write($record);

        // Exit (success)
        return $created ? $identifier : false;
    }
}
