CREATE TABLE IF NOT EXISTS qipai_scene_codes (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  code VARCHAR(32) NOT NULL,
  target_type VARCHAR(16) NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  room_id BIGINT UNSIGNED NULL,
  generation INT UNSIGNED NOT NULL DEFAULT 1,
  status VARCHAR(16) NOT NULL DEFAULT 'ACTIVE',
  scan_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  last_scanned_at DATETIME(3) NULL,
  created_by BIGINT UNSIGNED NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  revoked_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_scene_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_scene_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_scene_room FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  CONSTRAINT fk_qipai_scene_creator FOREIGN KEY (created_by) REFERENCES qipai_users(id),
  UNIQUE KEY uq_qipai_scene_code (code),
  KEY idx_qipai_scene_target (tenant_id, target_type, store_id, room_id, status),
  CONSTRAINT chk_qipai_scene_target CHECK (
    (target_type = 'STORE' AND room_id IS NULL)
    OR (target_type = 'ROOM' AND room_id IS NOT NULL)
  )
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_scene_scan_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  scene_code_id BIGINT UNSIGNED NOT NULL,
  source_type VARCHAR(16) NOT NULL DEFAULT 'QRCODE',
  user_id BIGINT UNSIGNED NULL,
  trace_id VARCHAR(128) NOT NULL,
  ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(255) NOT NULL DEFAULT '',
  scanned_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_scene_scans_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_scene_scans_code FOREIGN KEY (scene_code_id) REFERENCES qipai_scene_codes(id),
  CONSTRAINT fk_qipai_scene_scans_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  KEY idx_qipai_scene_scans_time (tenant_id, scene_code_id, scanned_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_order_user_access (
  tenant_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  access_type VARCHAR(16) NOT NULL DEFAULT 'OWNER',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  revoked_at DATETIME(3) NULL,
  PRIMARY KEY (tenant_id, order_id, user_id),
  CONSTRAINT fk_qipai_order_access_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_order_access_order FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
  CONSTRAINT fk_qipai_order_access_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  KEY idx_qipai_order_access_user (tenant_id, user_id, revoked_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061810', 'm03d_scene_wifi_access');
