CREATE DATABASE IF NOT EXISTS `mobilemodels`
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_0900_ai_ci;

DROP DATABASE IF EXISTS `python_services_test`;

USE `mobilemodels`;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `mm_device_catalog`'
    WHEN 'VIEW' THEN 'DROP VIEW `mm_device_catalog`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'mm_device_catalog'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `mm_brand_lookup`'
    WHEN 'VIEW' THEN 'DROP VIEW `mm_brand_lookup`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'mm_brand_lookup'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `mm_device_lookup`'
    WHEN 'VIEW' THEN 'DROP VIEW `mm_device_lookup`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'mm_device_lookup'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `mm_device_record`'
    WHEN 'VIEW' THEN 'DROP VIEW `mm_device_record`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'mm_device_record'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `models`'
    WHEN 'VIEW' THEN 'DROP VIEW `models`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'models'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `vw_mm_device_lookup`'
    WHEN 'VIEW' THEN 'DROP VIEW `vw_mm_device_lookup`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'vw_mm_device_lookup'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_stmt = (
  SELECT CASE `TABLE_TYPE`
    WHEN 'BASE TABLE' THEN 'DROP TABLE `vw_models`'
    WHEN 'VIEW' THEN 'DROP VIEW `vw_models`'
    ELSE 'DO 0'
  END
  FROM `information_schema`.`TABLES`
  WHERE `TABLE_SCHEMA` = 'mobilemodels' AND `TABLE_NAME` = 'vw_models'
  LIMIT 1
);
SET @drop_stmt = COALESCE(@drop_stmt, 'DO 0');
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS `mm_device_catalog` (
  `record_id` varchar(64) NOT NULL,
  `model` varchar(191) NOT NULL,
  `alias_norm` varchar(191) NOT NULL,
  `device_name` varchar(255) NOT NULL,
  `brand` varchar(64) NOT NULL,
  `manufacturer_brand` varchar(64) NOT NULL,
  `parent_brand` varchar(64) NOT NULL,
  `market_brand` varchar(64) NOT NULL,
  `device_type` enum('phone','tablet','wear','tv','computer','other') NOT NULL,
  `code` varchar(64) DEFAULT NULL,
  `code_alias` varchar(255) DEFAULT NULL,
  `ver_name` text DEFAULT NULL,
  `source_file` varchar(255) NOT NULL,
  `section` varchar(255) NOT NULL,
  `source_rank` int NOT NULL,
  `source_weight` decimal(6,3) NOT NULL,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `hash_md5` char(32) GENERATED ALWAYS AS (
    md5(concat_ws(_utf8mb4'|', `model`, `device_type`, `market_brand`, `manufacturer_brand`, `code`, `code_alias`, `device_name`, `ver_name`))
  ) STORED,
  `hash_crc` int unsigned GENERATED ALWAYS AS (
    crc32(concat_ws(_utf8mb4'|', `model`, `device_type`, `market_brand`, `manufacturer_brand`, `code`, `code_alias`, `device_name`, `ver_name`))
  ) STORED,
  PRIMARY KEY (`record_id`, `model`),
  KEY `idx_mm_device_catalog_alias_norm` (`alias_norm`, `source_rank`, `record_id`),
  KEY `idx_mm_device_catalog_model` (`model`),
  KEY `idx_mm_device_catalog_market_brand` (`market_brand`),
  KEY `idx_mm_device_catalog_parent_brand` (`parent_brand`),
  KEY `idx_mm_device_catalog_manufacturer_brand` (`manufacturer_brand`),
  KEY `idx_mm_device_catalog_device_type` (`device_type`),
  KEY `idx_mm_device_catalog_code` (`code`),
  KEY `idx_mm_device_catalog_hash` (`hash_md5`, `hash_crc`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS `mm_brand_lookup` (
  `alias_norm` varchar(191) NOT NULL,
  `alias_type` enum('manufacturer','parent','market') NOT NULL,
  `canonical_brand` varchar(64) NOT NULL,
  `manufacturer_brand` varchar(64) DEFAULT NULL,
  `parent_brand` varchar(64) DEFAULT NULL,
  `market_brand` varchar(64) DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`alias_norm`, `alias_type`),
  KEY `idx_mm_brand_lookup_canonical_brand` (`canonical_brand`),
  KEY `idx_mm_brand_lookup_manufacturer_brand` (`manufacturer_brand`),
  KEY `idx_mm_brand_lookup_parent_brand` (`parent_brand`),
  KEY `idx_mm_brand_lookup_market_brand` (`market_brand`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
