-- Customer vehicles (run after vehicle_models_schema.sql)

USE fiac_db;

CREATE TABLE IF NOT EXISTS customer_vehicles (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id INT UNSIGNED NOT NULL,
  vehicle_brand_id TINYINT UNSIGNED NOT NULL,
  vehicle_model_id SMALLINT UNSIGNED DEFAULT NULL,
  vehicle_number VARCHAR(30) NOT NULL,
  vehicle_year SMALLINT UNSIGNED DEFAULT NULL,
  color VARCHAR(40) DEFAULT NULL,
  nickname VARCHAR(60) DEFAULT NULL,
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_customer_vehicle_number (customer_id, vehicle_number),
  KEY idx_customer_vehicles_customer (customer_id),
  KEY idx_customer_vehicles_brand (vehicle_brand_id),
  CONSTRAINT fk_cv_customer FOREIGN KEY (customer_id) REFERENCES users (id) ON DELETE CASCADE,
  CONSTRAINT fk_cv_brand FOREIGN KEY (vehicle_brand_id) REFERENCES vehicle_brands (id),
  CONSTRAINT fk_cv_model FOREIGN KEY (vehicle_model_id) REFERENCES vehicle_models (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
