<?php
/**
 * Migration: Create the vehicle specs table.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Database\Migrations;

use ThemeREX\AutoDealer\Database\MigrationInterface;

class CreateVehicleSpecsTable implements MigrationInterface {

	/**
	 * {@inheritDoc}
	 */
	public function up( \wpdb $wpdb ): void {
		$table          = $wpdb->prefix . 'trx_ad_vehicle_specs';
		$charsetCollate = $wpdb->get_charset_collate();

		$sql = "CREATE TABLE {$table} (
			post_id bigint(20) unsigned NOT NULL,
			vin varchar(17) DEFAULT NULL,
			stock_number varchar(50) DEFAULT NULL,
			condition_type enum('new','used','cpo') DEFAULT NULL,
			status enum('available','sold','reserved','coming_soon') DEFAULT 'available',
			vehicle_type varchar(50) DEFAULT NULL,
			year smallint(5) unsigned DEFAULT NULL,
			make varchar(100) DEFAULT NULL,
			model varchar(100) DEFAULT NULL,
			trim varchar(100) DEFAULT NULL,
			body_style varchar(50) DEFAULT NULL,
			fuel_type varchar(50) DEFAULT NULL,
			transmission varchar(50) DEFAULT NULL,
			drivetrain varchar(10) DEFAULT NULL,
			mileage int(10) unsigned DEFAULT NULL,
			price decimal(12,2) DEFAULT NULL,
			sale_price decimal(12,2) DEFAULT NULL,
			price_on_request tinyint(1) NOT NULL DEFAULT 0,
			color_ext varchar(50) DEFAULT NULL,
			color_int varchar(50) DEFAULT NULL,
			doors tinyint(3) unsigned DEFAULT NULL,
			seats tinyint(3) unsigned DEFAULT NULL,
			engine_size decimal(3,1) DEFAULT NULL,
			cylinders tinyint(3) unsigned DEFAULT NULL,
			horsepower smallint(5) unsigned DEFAULT NULL,
			torque_nm smallint(5) unsigned DEFAULT NULL,
			fuel_economy_city decimal(5,1) DEFAULT NULL,
			fuel_economy_hwy decimal(5,1) DEFAULT NULL,
			fuel_economy_combined decimal(5,1) DEFAULT NULL,
			ev_range_km smallint(5) unsigned DEFAULT NULL,
			ev_battery_kwh decimal(5,1) DEFAULT NULL,
			ev_charging_type varchar(50) DEFAULT NULL,
			ev_charge_time_min smallint(5) unsigned DEFAULT NULL,
			co2_emissions smallint(5) unsigned DEFAULT NULL,
			registration_date date DEFAULT NULL,
			warranty_expiry date DEFAULT NULL,
			weight_kg int(10) unsigned DEFAULT NULL,
			cargo_volume_l smallint(5) unsigned DEFAULT NULL,
			engine_hours int(10) unsigned DEFAULT NULL,
			hull_material varchar(50) DEFAULT NULL,
			prev_owners tinyint(3) unsigned DEFAULT NULL,
			accident_free tinyint(1) NOT NULL DEFAULT 0,
			featured tinyint(1) NOT NULL DEFAULT 0,
			dealer_id bigint(20) unsigned DEFAULT NULL,
			lat decimal(10,7) DEFAULT NULL,
			lng decimal(10,7) DEFAULT NULL,
			expiry_date date DEFAULT NULL,
			price_reduced tinyint(1) NOT NULL DEFAULT 0,
			price_reduced_at datetime DEFAULT NULL,
			price_original decimal(12,2) DEFAULT NULL,
			market_value_badge enum('below','at','above') DEFAULT NULL,
			view_count int(10) unsigned NOT NULL DEFAULT 0,
			call_count int(10) unsigned NOT NULL DEFAULT 0,
			whatsapp_count int(10) unsigned NOT NULL DEFAULT 0,
			inquiry_count int(10) unsigned NOT NULL DEFAULT 0,
			favorites_count int(10) unsigned NOT NULL DEFAULT 0,
			listing_quality tinyint(3) unsigned NOT NULL DEFAULT 0,
			created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY  (post_id),
			UNIQUE KEY vin (vin),
			KEY stock_number (stock_number),
			KEY condition_type (condition_type),
			KEY status (status),
			KEY vehicle_type (vehicle_type),
			KEY year (year),
			KEY make (make),
			KEY model (model),
			KEY body_style (body_style),
			KEY fuel_type (fuel_type),
			KEY transmission (transmission),
			KEY drivetrain (drivetrain),
			KEY mileage (mileage),
			KEY price (price),
			KEY color_ext (color_ext),
			KEY featured (featured),
			KEY dealer_id (dealer_id),
			KEY expiry_date (expiry_date),
			KEY price_reduced (price_reduced)
		) {$charsetCollate};";

		dbDelta( $sql );
	}

	/**
	 * {@inheritDoc}
	 */
	public function getVersion(): int {
		return 1;
	}
}
