-- 创建数据库
CREATE DATABASE IF NOT EXISTS command_center CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE command_center;

-- 创建用户表
CREATE TABLE IF NOT EXISTS users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  role ENUM('admin', 'operator') NOT NULL DEFAULT 'operator',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 创建无人机表
CREATE TABLE IF NOT EXISTS drones (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  type VARCHAR(50) NOT NULL,
  status ENUM('active', 'idle', 'error') NOT NULL DEFAULT 'idle',
  latitude DECIMAL(10, 8),
  longitude DECIMAL(11, 8),
  battery INT DEFAULT 100,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 创建路径规划记录表
CREATE TABLE IF NOT EXISTS path_plans (
  id INT PRIMARY KEY AUTO_INCREMENT,
  drone_id INT NOT NULL,
  start_latitude DECIMAL(10, 8) NOT NULL,
  start_longitude DECIMAL(11, 8) NOT NULL,
  end_latitude DECIMAL(10, 8) NOT NULL,
  end_longitude DECIMAL(11, 8) NOT NULL,
  path_points JSON,
  status ENUM('pending', 'executing', 'completed', 'failed') NOT NULL DEFAULT 'pending',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (drone_id) REFERENCES drones(id)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 删除现有威胁区表（如果存在）以确保结构正确
DROP TABLE IF EXISTS threat_zones;

-- 创建威胁区表
CREATE TABLE threat_zones (
  id INT PRIMARY KEY AUTO_INCREMENT,
  type ENUM('radar', 'missile', 'aircraft', 'ground', 'weather') NOT NULL,
  level ENUM('low', 'medium', 'high', 'critical') NOT NULL,
  description TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  geometry_type ENUM('circle', 'polygon', 'rectangle') NOT NULL,
  geometry_data JSON NOT NULL COMMENT '存储几何形状数据',
  time_start TIMESTAMP NULL COMMENT '威胁区开始时间',
  time_end TIMESTAMP NULL COMMENT '威胁区结束时间',
  status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  created_by INT DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (created_by) REFERENCES users(id),
  INDEX idx_type (type),
  INDEX idx_level (level),
  INDEX idx_status (status)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 创建操作日志表
CREATE TABLE IF NOT EXISTS operation_logs (
  id INT PRIMARY KEY AUTO_INCREMENT,
  user_id INT NOT NULL,
  action VARCHAR(50) NOT NULL,
  target_type VARCHAR(50) NOT NULL,
  target_id INT NOT NULL,
  details JSON,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 插入默认管理员用户
INSERT INTO users (username, password, role)
VALUES ('admin', '$2a$10$X7UrH5QxX5QxX5QxX5QxX.5QxX5QxX5QxX5QxX5QxX5QxX5QxX5Qx', 'admin')
ON DUPLICATE KEY UPDATE username = username;

-- 插入示例威胁区数据
INSERT INTO threat_zones (type, level, description, geometry_type, geometry_data, time_start, time_end, status)
VALUES 
('radar', 'high', 'Enemy radar station', 'circle', 
 JSON_OBJECT('center', JSON_ARRAY(116.397428, 39.91), 'radius', 2000), 
 '2024-01-01 00:00:00', '2024-12-31 23:59:59', 'active'),
('missile', 'critical', 'Missile launch site', 'circle', 
 JSON_OBJECT('center', JSON_ARRAY(116.42, 39.89), 'radius', 3000), 
 '2024-01-01 00:00:00', '2024-12-31 23:59:59', 'active'),
('aircraft', 'medium', 'Air patrol zone', 'polygon', 
 JSON_OBJECT('path', JSON_ARRAY(
   JSON_ARRAY(116.38, 39.92),
   JSON_ARRAY(116.40, 39.92), 
   JSON_ARRAY(116.40, 39.90),
   JSON_ARRAY(116.38, 39.90)
 )), 
 '2024-01-01 06:00:00', '2024-01-01 18:00:00', 'active'); 