<?php

namespace EDPW\Core\Models;


defined( 'ABSPATH' ) or die();

class EmailTemplates extends BaseModel {
	/**
	 * Table name.
	 *
	 * @var string
	 */
	const TABLE_NAME = 'edpw_templates';

	/**
	 * Table primary key.
	 */
	const PRIMARY_KEY = 'id';

	/**
	 * Field list.
	 *
	 * @var array
	 */
	public static array $fields = [
		'type'              => '%s',
		'is_active'         => '%d',
		'language'          => '%s',
		'styles'            => '%s',
		'elements'          => '%s',
		'css'               => '%s',
		'html'              => '%s',
		'mjml'              => '%s',
		'extra'             => '%s',
		'global_styles'     => '%s',
		'mj_component_data' => '%s'
	];

	/**
	 * run table creation query
	 */
	public static function create() {
		$create_table_query = "CREATE TABLE {table} (
				 `id` int(11) NOT NULL AUTO_INCREMENT,
                 `type` varchar(255) DEFAULT NULL,
                 `is_active` int DEFAULT 1,
                 `css` longtext DEFAULT NULL,
                 `html` longtext DEFAULT NULL,
                 `mjml` longtext DEFAULT NULL,
                 `language` varchar(255) DEFAULT NULL,
                 `styles` longtext DEFAULT NULL,
                 `elements` longtext DEFAULT NULL,
                 `global_styles` longtext DEFAULT NULL,
                 `mj_component_data` longtext DEFAULT NULL,
                 `extra` longtext DEFAULT NULL,
                 PRIMARY KEY (`id`)
			){charset_collate};";
		self::executeQuery( $create_table_query );
	}

	/**
	 * Get Saved templates based on language.
	 *
	 * @param string $language language string.
	 * @param array|string $select select fields.
	 * @param bool $single single record or multiple record.
	 *
	 * @return array|object|null
	 */
	public static function getSavedTemplates( $language, $select = '*', $single = false ) {
		return self::getWhere( self::db()->prepare( 'language=%s', [ $language ] ), $select, $single );
	}

	public function getActiveTemplate( $language, $type, $select = '*', $single = true ) {
		if ( ! is_string( $language ) || empty( $language ) || ! is_string( $type ) || empty( $type ) ) {
			return '';
		}

		return self::getWhere( self::db()->prepare( 'language=%s AND is_active=1 AND type = %s', [
			$language,
			$type
		] ), $select, $single );
	}

	public static function getTemplateBasedLang( $language, $type, $select = '*', $single = true ) {
		if ( ! is_string( $language ) || empty( $language ) || ! is_string( $type ) || empty( $type ) ) {
			return '';
		}

		return self::getWhere( self::db()->prepare( 'language=%s AND type = %s', [
			$language,
			$type
		] ), $select, $single );
	}

	public static function saveNewTemplate( $data ) {
		if ( ! is_array( $data ) ) {
			return 0;
		}
		$save_data = [
			'type'              => ! empty( $data['type'] ) ? $data['type'] : '',
			'is_active'         => $data['is_active'] ?? 1,
			'css'               => ! empty( $data['css'] ) ? $data['css'] : '',
			'html'              => ! empty( $data['html'] ) ? $data['html'] : '',
			'mjml'              => ! empty( $data['mjml'] ) ? $data['mjml'] : '',
			'language'          => ! empty( $data['language'] ) ? $data['language'] : 'en_US',
			'styles'            => ! empty( $data['styles'] ) ? $data['styles'] : '',
			'elements'          => ! empty( $data['elements'] ) ? $data['elements'] : '',
			'global_styles'     => ! empty( $data['global_styles'] ) ? $data['global_styles'] : '',
			'mj_component_data' => ! empty( $data['mj_component_data'] ) ? $data['mj_component_data'] : '',
			'extra'             => ! empty( $data['extra'] ) ? $data['extra'] : '',
		];
		if ( ! empty( self::getTemplateBasedLang( $save_data['language'], $save_data['type'] ) ) ) {
			return 0;
		}

		return self::insert( $save_data );
	}
}