CREATE TABLE IF NOT EXISTS `product_import_catalog` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `internal_name` varchar(255) NOT NULL,
  `internal_slug` varchar(275) NOT NULL,
  `barcode` varchar(160) NOT NULL,
  `price` decimal(12,2) NOT NULL DEFAULT 0.00,
  `notes` text DEFAULT NULL,
  `is_used` tinyint(1) NOT NULL DEFAULT 0,
  `linked_product_id` int(10) UNSIGNED DEFAULT NULL,
  `last_imported_at` datetime 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 `idx_pic_internal_slug` (`internal_slug`),
  KEY `idx_pic_barcode` (`barcode`),
  KEY `idx_pic_used` (`is_used`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `products` ADD COLUMN IF NOT EXISTS `import_catalog_id` int(10) UNSIGNED DEFAULT NULL AFTER `id`;
ALTER TABLE `products` ADD COLUMN IF NOT EXISTS `barcode` varchar(160) DEFAULT NULL AFTER `sku`;
ALTER TABLE `products` ADD COLUMN IF NOT EXISTS `internal_name` varchar(255) DEFAULT NULL AFTER `barcode`;
ALTER TABLE `product_variants` ADD COLUMN IF NOT EXISTS `barcode` varchar(160) DEFAULT NULL AFTER `sku`;

ALTER TABLE `products` ADD INDEX IF NOT EXISTS `idx_products_import_catalog_id` (`import_catalog_id`);
ALTER TABLE `products` ADD INDEX IF NOT EXISTS `idx_products_barcode` (`barcode`);
ALTER TABLE `product_variants` ADD INDEX IF NOT EXISTS `idx_product_variants_barcode` (`barcode`);
