CREATE TABLE IF NOT EXISTS `dismantling_vehicles` (
 `id` bigint unsigned NOT NULL AUTO_INCREMENT,
 `vin` varchar(17) DEFAULT NULL, `brand` varchar(100) NOT NULL, `model` varchar(120) NOT NULL,
 `production_month` tinyint unsigned DEFAULT NULL, `production_year` smallint unsigned DEFAULT NULL,
 `engine` varchar(120) DEFAULT NULL, `engine_code` varchar(50) DEFAULT NULL, `power_hp` smallint unsigned DEFAULT NULL,
 `emission_standard` varchar(30) DEFAULT NULL, `gearbox` varchar(50) DEFAULT NULL, `mileage` int unsigned DEFAULT NULL,
 `color` varchar(80) DEFAULT NULL, `color_code` varchar(40) DEFAULT NULL, `damage_area` varchar(190) DEFAULT NULL,
 `source` varchar(190) DEFAULT NULL, `purchase_price` decimal(12,2) NOT NULL DEFAULT 0, `transport_cost` decimal(12,2) NOT NULL DEFAULT 0,
 `documents_cost` decimal(12,2) NOT NULL DEFAULT 0, `dismantling_cost` decimal(12,2) NOT NULL DEFAULT 0,
 `other_costs` decimal(12,2) NOT NULL DEFAULT 0, `desired_profit` decimal(12,2) NOT NULL DEFAULT 5000,
 `status` enum('evaluation','purchased','dismantling','active','closed') NOT NULL DEFAULT 'evaluation',
 `notes` text DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`), KEY `idx_dv_vin` (`vin`), KEY `idx_dv_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `dismantling_parts` (
 `id` bigint unsigned NOT NULL AUTO_INCREMENT, `vehicle_id` bigint unsigned NOT NULL,
 `category` varchar(100) NOT NULL, `name` varchar(190) NOT NULL, `internal_code` varchar(80) DEFAULT NULL,
 `oe_code` varchar(190) DEFAULT NULL, `manufacturer_code` varchar(190) DEFAULT NULL,
 `condition_status` enum('good','needs_test','repair','damaged','missing') NOT NULL DEFAULT 'needs_test',
 `tested` tinyint(1) NOT NULL DEFAULT 0, `estimated_price` decimal(12,2) NOT NULL DEFAULT 0,
 `quick_sale_price` decimal(12,2) NOT NULL DEFAULT 0, `sale_probability` tinyint unsigned NOT NULL DEFAULT 50,
 `preparation_cost` decimal(12,2) NOT NULL DEFAULT 0,
 `status` enum('available','reserved','sold','discarded') NOT NULL DEFAULT 'available',
 `sale_price` decimal(12,2) NOT NULL DEFAULT 0, `location` varchar(100) DEFAULT NULL, `notes` text DEFAULT NULL,
 `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`), KEY `idx_dp_vehicle` (`vehicle_id`), KEY `idx_dp_codes` (`oe_code`,`manufacturer_code`), KEY `idx_dp_status` (`status`),
 CONSTRAINT `fk_dismantling_parts_vehicle` FOREIGN KEY (`vehicle_id`) REFERENCES `dismantling_vehicles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
