<?php

namespace CarDealerPress\Includes\Database;

use CarDealerPress\includes\functions\Logger;
use CarDealerPress\Includes\Helpers\Option;

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

class Create_Tables {
	use Logger;

	/**
	 * @var int
	 */
	private static $blog;
	public $current_inventory_version;

	public $current_company_version;

	public static $wpdb;


	public function __construct() {
		global $wpdb;
		self::$wpdb                      = $wpdb;
		self::$blog                      = get_current_blog_id();
		$this->current_company_version   = Option::check_for_option(
			'cdp_admin_options',
			array(
				'admin',
				'database',
				'company',
				'current',
			)
		);
		$this->current_inventory_version = Option::check_for_option(
			'cdp_admin_options',
			array(
				'admin',
				'database',
				'inventory',
				'current',
			)
		);
	}

	function all_tables(){
		$this->company_table();
		$this->inventory_table();
		$this->data_table();
	}


	function company_table() {
		$charset_collate = self::$wpdb->get_charset_collate();
		$table_main      = CDP_COMPANIES;

		// @codingStandardsIgnoreStart
		if ( self::$wpdb->get_var( "SHOW TABLES LIKE '$table_main'" ) !== $table_main ) {
		// @codingStandardsIgnoreStart
			$this->append_log( array( 'message' => 'CDP Message: Company DB Created Version: ' . $this->current_company_version ) );

			$columns = '
				id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
				blog_id int(11) NOT NULL,
				company_id int(11) NOT NULL,
				dealer_id int(11) NOT NULL,
				name varchar(255) NOT NULL,
				street varchar(255) NOT NULL,
				city varchar(255) NOT NULL,
				state varchar(255) NOT NULL,
				zip varchar(255) NOT NULL,
				phone varchar(255) NOT NULL,
				country_code varchar(255) NOT NULL,
				apikeys longtext NOT NULL,
				automalls longtext NOT NULL,
				created date NOT NULL,
				last_updated DATETIME NOT NULL,
				inventory_last_updated DATETIME NOT NULL,
				inventory_api_last_modified DATETIME NOT NULL,
				inventory_last_checked DATETIME NOT NULL,
				inventory_last_checked_response DATETIME NOT NULL,
				seo longtext NOT NULL
			';

			$sql = "CREATE TABLE $table_main ( " . $columns . " ) $charset_collate;";

			include_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
		}

	}

	function inventory_table() {
		$charset_collate = self::$wpdb->get_charset_collate();
		$table_main      = CDP_INVENTORY_TABLE;
		$table_import    = CDP_INVENTORY_IMPORT_TABLE;

		// @codingStandardsIgnoreStart
		if ( self::$wpdb->get_var( "SHOW TABLES LIKE '$table_main'" ) !== $table_main ) {
		// @codingStandardsIgnoreEnd

			$this->append_log( array( 'message' => 'CDP Message: Inventory DB Created Version: ' . $this->current_inventory_version ) );

			$columns = "
				id bigint(20) NOT NULL AUTO_INCREMENT,
				blog_id int(11) NOT NULL DEFAULT '1',
				vms_id int(11) NOT NULL,
				company_id int(11) NOT NULL,
				dealer_id int(11) NOT NULL,
				dealer varchar(255) NOT NULL,
				stock varchar(255) NOT NULL,
				vin varchar(255) NOT NULL,
				saleclass varchar(255) NOT NULL,
				certified tinyint(1) NOT NULL DEFAULT '0',
				make varchar(255) NOT NULL,
				model varchar(255) NOT NULL,
				model_code varchar(255) NOT NULL,
				trim varchar(255) NOT NULL,
				year int(11) NOT NULL,
				askingprice decimal(18,0) NOT NULL,
				saleprice decimal(18,0) NOT NULL,
				retailprice decimal(18,0) NOT NULL,
				msrp decimal(18,0) NOT NULL,
				invoice decimal(18,0) NOT NULL,
				sortprice decimal(18,0) NOT NULL,
				pricesort int(11) NOT NULL DEFAULT '0',
				sale_expire varchar(255) NOT NULL,
				headline varchar(512) NOT NULL,
				description text NOT NULL,
				exterior varchar(255) NOT NULL,
				interior varchar(255) NOT NULL,
				color_map varchar(255) NOT NULL,
				color_map_int varchar(255) NOT NULL,
				body varchar(255) NOT NULL,
				vehicleclass varchar(255) NOT NULL,
				tags varchar(350) NOT NULL,
				drivetrain varchar(255) NOT NULL,
				doors varchar(10) NOT NULL,
				engine varchar(255) NOT NULL,
				transmission varchar(255) NOT NULL,
				fuel_type varchar(255) NOT NULL,
				highway varchar(255) NOT NULL,
				city varchar(255) NOT NULL,
				odometer int(11) NOT NULL,
				carfax varchar(255) NOT NULL,
				carfax_image_url varchar(255) NOT NULL,
				autocheck varchar(255) NOT NULL,
				video varchar(255) NOT NULL,
				acode varchar(255) NOT NULL,
				thumbnail varchar(350) NOT NULL,
				image_medium varchar(350) NOT NULL,
				geo_state varchar(255) NOT NULL,
				geo_city varchar (255) NOT NULL,
				geo_zip varchar (255) NOT NULL,
				location varchar (255) NOT NULL,
				phone varchar (100) NOT NULL,
				created datetime NOT NULL,
				updated datetime NOT NULL,
				sold_on varchar (10) DEFAULT '',
				sort int(11) NOT NULL DEFAULT '0',
				extra text NOT NULL,
				vehicle_url varchar (255) NOT NULL,
				payment decimal(18,0) NOT NULL,
				UNIQUE KEY id (id)
			";
			$sql     = "CREATE TABLE $table_main ( " . $columns . " ) $charset_collate;";
			$sql2    = "CREATE TABLE $table_import ( " . $columns . " ) $charset_collate;";

			include_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
			dbDelta( $sql2 );
		}

	}


	public function data_table() {
		$charset_collate = self::$wpdb->get_charset_collate();
		$table_main      = CDP_DATA;

		// @codingStandardsIgnoreStart
		if ( self::$wpdb->get_var( "SHOW TABLES LIKE '$table_main'" ) !== $table_main ) {
		// @codingStandardsIgnoreEnd

			$this->append_log( array( 'message' => 'CDP Message: CDP Data table created' ) );

			$columns = "
				data_id bigint(20) NOT NULL AUTO_INCREMENT,
					blog_id int(11) NOT NULL,
				dealer_id int(11) NOT NULL,
				data_key varchar(255) NOT NULL DEFAULT '',
				data_value longtext NOT NULL DEFAULT '',
				UNIQUE KEY (data_id)
			";

			$sql = "CREATE TABLE $table_main ( " . $columns . " ) $charset_collate;";

			include_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
		}

	}


}
