CREATE TABLE IF NOT EXISTS `ApiUserProfile` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `DeviceID` varchar(128) NOT NULL,
  `Account` varchar(100) NOT NULL DEFAULT 'daywin888',
  `DisplayName` varchar(100) DEFAULT NULL,
  `LastLoginAt` datetime DEFAULT NULL,
  `CreatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `UpdatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `uniq_device` (`DeviceID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ApiContactSync` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `DeviceID` varchar(128) NOT NULL,
  `Platform` varchar(20) NOT NULL DEFAULT 'unknown',
  `PayloadJson` longtext NOT NULL,
  `CreatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`),
  KEY `idx_device` (`DeviceID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ApiCalendarEvent` (
  `ID` bigint NOT NULL AUTO_INCREMENT,
  `DeviceID` varchar(128) NOT NULL,
  `Title` varchar(150) NOT NULL,
  `Notes` text,
  `StartAt` datetime DEFAULT NULL,
  `EndAt` datetime DEFAULT NULL,
  `CreatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`),
  KEY `idx_device_time` (`DeviceID`, `CreatedAt`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
