<?php

declare(strict_types=1);

namespace svoboda\pechatalka\models;

// Files of the project
use svoboda\pechatalka\models\core,
	svoboda\pechatalka\models\enumerations\product\type as product_type;

// 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;

// Active Record pattern
use mirzaev\record\interfaces\record as record_interface,
	mirzaev\record\traits\record as record_trait;


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

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

/**
 * Product
 *
 * @package svoboda\pechatalka\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 product extends core implements record_interface
{
	use record_trait;

	/**
	 * File
	 *
	 * @var string $database Path to the database file
	 */
	protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'products.baza';

	/**
	 * Database
	 *
	 * @method record|null $record The record
	 *
	 * @var database $database The database
	 */
	public protected(set) database $database;
	 
	/**
	 * Serialized
	 *
	 * @var bool $serialized Is the implementator object serialized?
	 */
	private bool $serialized = true;

	/**
	 * Constructor
	 *
	 * @return void
	 */
	public function __construct(?record $record = null)
	{
		// Initializing the database
		$this->database = new database()
			->encoding(encoding::utf8)
			->columns(
				new column('identifier', type::integer_unsigned),
				new column('account', type::integer_unsigned),
				new column('type', type::string, ['length' => 32]),
				new column('layers', type::string, ['length' => 4096]),
				new column('canvas', type::string, ['length' => 4096]),
				new column('updated', type::integer_unsigned),
				new column('created', type::integer_unsigned)
			)
			->connect($this->file);

		// Initializing the record
		$record instanceof record and $this->record = $record;

	}

	/**
	 * Create
	 *
	 * @param int $account The account identifier
	 * @param string|product_type $type The product type
	 * @param string|array $layers The product layers (JSON)
	 * @param string|array $canvas The product canvas (JSON)
	 *
	 * @return int|false The instance, if created
	 */
	public function create(
		int $account,
		string|product_type $type,
		string|array $layers,
		string|array|null $canvas = null
	): static|false
	{
		// Initializing the identifier
		$identifier = $this->database->count() + 1;

		// Initializing the record
		$record = $this->database->record(
			$identifier,
			$account,
			$type instanceof product_type ? $type->name : $type,
			is_string($layers) && json_validate($layers, depth: 16) ? $layers : json_encode($layers, depth: 16),
			is_string($canvas) && json_validate($canvas, depth: 16) ? $canvas : json_encode($canvas, depth: 16),
			svoboda::timestamp(),
			svoboda::timestamp()
		);

		// Writing the record into the database
		$created = $this->database->write($record);
	
	// Exit (success)
		return $record ? new static(record: $record) : false;
	}
}

