CREATE TABLE IF NOT EXISTS `users` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`      BIGINT UNSIGNED NOT NULL,
  `username`      VARCHAR(50)     NOT NULL,
  `password_hash` VARCHAR(255)    NOT NULL,
  `real_name`     VARCHAR(50)     DEFAULT NULL,
  `phone`         VARCHAR(30)     DEFAULT NULL,
  `role`          ENUM('admin','operator') NOT NULL DEFAULT 'operator',
  `is_active`     TINYINT(1)      NOT NULL DEFAULT 1,
  `custom_fields` JSON            DEFAULT NULL,
  `created_at`    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at`    DATETIME        DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hotel_username` (`hotel_id`, `username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `oauth_providers` (
  `id`               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`          BIGINT UNSIGNED NOT NULL,
  `provider`         ENUM('wechat','google','apple') NOT NULL,
  `provider_user_id` VARCHAR(255)    NOT NULL,
  `access_token`     TEXT            DEFAULT NULL,
  `refresh_token`    TEXT            DEFAULT NULL,
  `expires_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`),
  UNIQUE KEY `uk_provider_uid` (`provider`, `provider_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `licenses` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `hotel_id`    BIGINT UNSIGNED NOT NULL,
  `license_key` VARCHAR(255)    NOT NULL,
  `device_id`   VARCHAR(255)    DEFAULT NULL,
  `type`        ENUM('trial','annual','lifetime') NOT NULL DEFAULT 'trial',
  `expires_at`  DATETIME        DEFAULT NULL,
  `is_active`   TINYINT(1)      NOT NULL DEFAULT 1,
  `features`    JSON            DEFAULT NULL,
  `activated_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`),
  UNIQUE KEY `uk_license_key` (`license_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
