<?php
/**
 * Database Class
 *
 * @author Jegstudio
 * @since 1.0.0
 * @package gutenverse-themes-builder
 */

namespace Gutenverse_Themes_Builder\Database;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Themes_Global_Db
 *
 * @package gutenverse-themes-builder
 */
class Themes_Global_Db extends Database_Abstract {
	/**
	 * Return table name (without prefix)
	 *
	 * @return mixed|string
	 */
	public function table_name() {
		return 'gtb_themes_global';
	}

	/**
	 * Script for creating Table
	 *
	 * @return string
	 */
	public function create_table_script() {
		$charset = $this->wpdb->get_charset_collate();
		$table   = $this->get_table_name();

		$sql = "CREATE TABLE $table (
			id				INT NOT NULL AUTO_INCREMENT,
			title			VARCHAR(200) NOT NULL,
			file			TEXT,
            fonts			TEXT,
			colors			TEXT,
			styles			TEXT DEFAULT NULL,       
			PRIMARY KEY 	(id)
		) $charset;";

		return $sql;
	}

	/**
	 * Alter Table
	 */
	public function alter_table() {
		$column_name = 'styles';
		$table       = $this->get_table_name();

		// Query the information schema
		$column_exists = $this->wpdb->get_var(
			$this->wpdb->prepare(
				"SHOW COLUMNS FROM `$table` LIKE %s",
				$column_name
			)
		);
		if ( ! $column_exists ) {
			$this->wpdb->query(
				"ALTER TABLE `$table` ADD `$column_name` TEXT DEFAULT NULL"
			);
		}
	}

	/**
	 * Column Checker
	 *
	 * @param string $column_name .
	 * @return boolean
	 */
	public function column_checker( $column_name ) {
		$table       = $this->get_table_name();
		return $this->wpdb->get_var(
			$this->wpdb->prepare(
				"SHOW COLUMNS FROM `$table` LIKE %s",
				$column_name
			)
		);
	}

	/**
	 * Initialize Data
	 *
	 * @return array
	 */
	public function initial_data() {
		return array();
	}

	/**
	 * Get All Assets.
	 *
	 * @return array|object|null
	 */
	public function get_all_globals() {
		$table = $this->get_table_name();
		$query = $this->wpdb->prepare( "SELECT * FROM $table" ); // phpcs:ignore

		return $this->wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore
	}

	/**
	 * Get Pagination Data
	 *
	 * @param integer $offset .
	 * @param integer $per_page .
	 * @return array|object|null
	 */
	public function get_pagination_data( $offset, $per_page ) {
		$table       = $this->get_table_name();
		$query       = $this->wpdb->prepare( "SELECT * FROM {$table} LIMIT {$offset}, {$per_page}" );
		$data        = $this->wpdb->get_results( $query, ARRAY_A );
		$query_count = $this->wpdb->prepare( "SELECT COUNT(*) as total FROM {$table}" );
		$count       = $this->wpdb->get_results( $query_count, ARRAY_A )[0]['total'];

		return array(
			'list'       => $data,
			'total_data' => $count,
		);
	}

	/**
	 * Get single data.
	 *
	 * @param int $id : data id.
	 *
	 * @return array|object|null
	 */
	public function get_data( $id ) {
		$table = $this->get_table_name();
		$query = $this->wpdb->prepare( "SELECT * FROM $table WHERE id =  %d", $id ); // phpcs:ignore

		return $this->wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore
	}

	/**
	 * Get single data.
	 *
	 * @param int $title : data title.
	 *
	 * @return array|object|null
	 */
	public function get_data_by_title( $title ) {
		$table = $this->get_table_name();
		$query = $this->wpdb->prepare( "SELECT * FROM $table WHERE title =  %s LIMIT 1", $title ); // phpcs:ignore

		return $this->wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore
	}

	/**
	 * Create new data data.
	 *
	 * @param array $data : data data.
	 *
	 * @return false|int
	 */
	public function create_data( $data ) {
		$table    = $this->get_table_name();
		$inserted = $this->wpdb->insert( $table, $data );

		if ( $inserted ) {
			return $this->wpdb->insert_id; // The AUTO_INCREMENT ID of the new row
		} else {
			return false;
		}
	}

	/**
	 * Get Inserted Id
	 *
	 * @return int
	 */
	public function get_inserted_id() {
		return $this->wpdb->insert_id;
	}

	/**
	 * Updating data data.
	 *
	 * @param array $data : data data.
	 * @param array $id : required data id.
	 *
	 * @return false|int
	 */
	public function update_data( $data, $id ) {
		$table = $this->get_table_name();

		return $this->wpdb->update( $table, $data, $id );
	}

	/**
	 * Deleting a data data.
	 *
	 * @param array $id : required data id.
	 *
	 * @return false|int
	 */
	public function delete_data( $id ) {
		$table = $this->get_table_name();

		return $this->wpdb->delete( $table, $id );
	}
}
