<?php
/**
 * Migration: Create the test-drive bookings table.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Database\Migrations;

use ThemeREX\AutoDealer\Database\MigrationInterface;

class CreateTestDriveSlotsTable implements MigrationInterface {

	public function up( \wpdb $wpdb ): void {
		$table          = $wpdb->prefix . 'trx_ad_test_drive_slots';
		$charsetCollate = $wpdb->get_charset_collate();

		$sql = "CREATE TABLE {$table} (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			vehicle_id bigint(20) unsigned NOT NULL,
			dealer_id bigint(20) unsigned DEFAULT NULL,
			scheduled_at datetime NOT NULL,
			duration_min smallint(5) unsigned NOT NULL DEFAULT 30,
			customer_name varchar(120) NOT NULL DEFAULT '',
			customer_email varchar(190) NOT NULL DEFAULT '',
			customer_phone varchar(40) NOT NULL DEFAULT '',
			status enum('pending','confirmed','cancelled','completed','no_show') NOT NULL DEFAULT 'pending',
			notes text DEFAULT NULL,
			ip_hash char(64) DEFAULT NULL,
			created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY vehicle_id (vehicle_id),
			KEY dealer_id (dealer_id),
			KEY scheduled_at (scheduled_at),
			KEY status (status),
			KEY vehicle_scheduled (vehicle_id, scheduled_at)
		) {$charsetCollate};";

		dbDelta( $sql );
	}

	public function getVersion(): int {
		return 3;
	}
}
