<?php

namespace SugarCalendarBookings\Support\Tags;

use SugarCalendarBookings\Support\Database\AbstractModel;

/**
 * Tag model.
 *
 * @since 1.0.0
 *
 * @property int    $id
 * @property string $name
 * @property string $slug
 * @property string $object_type
 */
class TagModel extends AbstractModel {

	/**
	 * The database table name without prefix.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	protected static $table_name = 'tags';

	/**
	 * The model type identifier.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	protected static $type = 'tag';

	/**
	 * Column formats for the model.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $column_formats = [
		'id'          => '%d',
		'name'        => '%s',
		'slug'        => '%s',
		'object_type' => '%s',
	];

	/**
	 * The attributes and meta that should be cast.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $casts = [
		'id' => 'integer',
	];

	/**
	 * Save the model to the database.
	 *
	 * @since 1.0.0
	 *
	 * @return bool True on success, false on failure.
	 */
	public function save(): bool {

		// Generate slug if not set.
		if ( empty( $this->get_attribute( 'slug' ) ) ) {
			$this->set_attribute( 'slug', sanitize_title( $this->get_attribute( 'name' ) ) );
		}

		return parent::save();
	}
}
