/*
# 创建电池管理系统数据库表

## 1. 新建表结构

### 1.1 设备管理表 (devices)
- `id` (uuid, 主键, 默认: gen_random_uuid())
- `device_id` (text, 唯一, 设备ID)
- `device_name` (text, 设备名称)
- `device_type` (text, 设备类型, 默认: 'BBox')
- `status` (device_status, 设备状态)
- `ip_address` (text, IP地址)
- `firmware_version` (text, 固件版本)
- `last_online` (timestamptz, 最后在线时间)
- `created_at` (timestamptz, 创建时间, 默认: now())
- `updated_at` (timestamptz, 更新时间, 默认: now())

### 1.2 电池数据表 (battery_data)
- `id` (uuid, 主键, 默认: gen_random_uuid())
- `device_id` (text, 设备ID, 外键)
- `voltage` (numeric, 电压)
- `current` (numeric, 电流)
- `temperature` (numeric, 温度)
- `soc` (numeric, 电量百分比)
- `soh` (numeric, 健康度)
- `power` (numeric, 功率)
- `timestamp` (timestamptz, 数据时间戳, 默认: now())

### 1.3 OTA升级任务表 (ota_tasks)
- `id` (uuid, 主键, 默认: gen_random_uuid())
- `device_id` (text, 设备ID)
- `task_name` (text, 任务名称)
- `firmware_version` (text, 目标固件版本)
- `status` (ota_status, 升级状态)
- `progress` (integer, 升级进度, 默认: 0)
- `start_time` (timestamptz, 开始时间)
- `end_time` (timestamptz, 结束时间)
- `error_message` (text, 错误信息)
- `created_at` (timestamptz, 创建时间, 默认: now())

### 1.4 MQTT连接日志表 (mqtt_logs)
- `id` (uuid, 主键, 默认: gen_random_uuid())
- `device_id` (text, 设备ID)
- `event_type` (text, 事件类型)
- `message` (text, 消息内容)
- `topic` (text, MQTT主题)
- `timestamp` (timestamptz, 时间戳, 默认: now())

### 1.5 系统配置表 (system_config)
- `id` (uuid, 主键, 默认: gen_random_uuid())
- `config_key` (text, 唯一, 配置键)
- `config_value` (text, 配置值)
- `description` (text, 配置描述)
- `updated_at` (timestamptz, 更新时间, 默认: now())

## 2. 安全策略
- 所有表均为公开访问，不启用RLS
- 管理员可以对所有数据进行完全访问

## 3. 初始数据
- 添加示例设备数据
- 添加系统默认配置
- 添加模拟电池数据用于展示

*/

-- 创建枚举类型
CREATE TYPE device_status AS ENUM ('online', 'offline', 'maintenance', 'error');
CREATE TYPE ota_status AS ENUM ('pending', 'downloading', 'installing', 'completed', 'failed');

-- 创建设备管理表
CREATE TABLE IF NOT EXISTS devices (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  device_id text UNIQUE NOT NULL,
  device_name text NOT NULL,
  device_type text DEFAULT 'BBox' NOT NULL,
  status device_status DEFAULT 'offline'::device_status NOT NULL,
  ip_address text,
  firmware_version text,
  last_online timestamptz,
  created_at timestamptz DEFAULT now(),
  updated_at timestamptz DEFAULT now()
);

-- 创建电池数据表
CREATE TABLE IF NOT EXISTS battery_data (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  device_id text NOT NULL,
  voltage numeric(8,2),
  current numeric(8,2),
  temperature numeric(5,2),
  soc numeric(5,2),
  soh numeric(5,2),
  power numeric(8,2),
  timestamp timestamptz DEFAULT now()
);

-- 创建OTA升级任务表
CREATE TABLE IF NOT EXISTS ota_tasks (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  device_id text NOT NULL,
  task_name text NOT NULL,
  firmware_version text NOT NULL,
  status ota_status DEFAULT 'pending'::ota_status NOT NULL,
  progress integer DEFAULT 0,
  start_time timestamptz,
  end_time timestamptz,
  error_message text,
  created_at timestamptz DEFAULT now()
);

-- 创建MQTT连接日志表
CREATE TABLE IF NOT EXISTS mqtt_logs (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  device_id text NOT NULL,
  event_type text NOT NULL,
  message text,
  topic text,
  timestamp timestamptz DEFAULT now()
);

-- 创建系统配置表
CREATE TABLE IF NOT EXISTS system_config (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  config_key text UNIQUE NOT NULL,
  config_value text NOT NULL,
  description text,
  updated_at timestamptz DEFAULT now()
);

-- 插入示例设备数据
INSERT INTO devices (device_id, device_name, device_type, status, ip_address, firmware_version, last_online) VALUES
('BBOX-001', 'BBox设备-001', 'BBox', 'online'::device_status, '192.168.1.101', 'v2.1.3', now() - interval '5 minutes'),
('BBOX-002', 'BBox设备-002', 'BBox', 'online'::device_status, '192.168.1.102', 'v2.1.3', now() - interval '2 minutes'),
('BBOX-003', 'BBox设备-003', 'BBox', 'offline'::device_status, '192.168.1.103', 'v2.1.2', now() - interval '2 hours'),
('BBOX-004', 'BBox设备-004', 'BBox', 'maintenance'::device_status, '192.168.1.104', 'v2.1.3', now() - interval '30 minutes'),
('BBOX-005', 'BBox设备-005', 'BBox', 'error'::device_status, '192.168.1.105', 'v2.1.1', now() - interval '1 hour');

-- 插入系统配置数据
INSERT INTO system_config (config_key, config_value, description) VALUES
('mqtt_server', 'mqtt://192.168.1.100:1883', 'MQTT服务器地址'),
('mqtt_username', 'admin', 'MQTT用户名'),
('data_retention_days', '30', '数据保留天数'),
('alert_temperature_high', '60', '高温报警阈值'),
('alert_temperature_low', '-10', '低温报警阈值'),
('alert_voltage_high', '4.2', '高电压报警阈值'),
('alert_voltage_low', '3.0', '低电压报警阈值'),
('ota_server_url', 'https://ota.yidong-energy.com', 'OTA升级服务器地址');

-- 插入模拟电池数据
INSERT INTO battery_data (device_id, voltage, current, temperature, soc, soh, power, timestamp) VALUES
('BBOX-001', 3.85, 2.5, 25.3, 85.2, 98.5, 9.625, now() - interval '1 minute'),
('BBOX-001', 3.84, 2.4, 25.5, 84.8, 98.5, 9.216, now() - interval '2 minutes'),
('BBOX-001', 3.86, 2.6, 25.1, 85.6, 98.5, 10.036, now() - interval '3 minutes'),
('BBOX-002', 3.92, 1.8, 24.8, 92.1, 97.8, 7.056, now() - interval '1 minute'),
('BBOX-002', 3.91, 1.9, 24.9, 91.8, 97.8, 7.429, now() - interval '2 minutes'),
('BBOX-002', 3.93, 1.7, 24.7, 92.4, 97.8, 6.681, now() - interval '3 minutes');

-- 插入OTA升级任务数据
INSERT INTO ota_tasks (device_id, task_name, firmware_version, status, progress, start_time, end_time) VALUES
('BBOX-003', '固件升级至v2.1.3', 'v2.1.3', 'completed'::ota_status, 100, now() - interval '2 hours', now() - interval '1 hour 45 minutes'),
('BBOX-005', '固件升级至v2.1.3', 'v2.1.3', 'failed'::ota_status, 65, now() - interval '1 hour 30 minutes', now() - interval '1 hour 15 minutes'),
('BBOX-004', '固件升级至v2.1.4', 'v2.1.4', 'downloading'::ota_status, 35, now() - interval '15 minutes', NULL);

-- 插入MQTT日志数据
INSERT INTO mqtt_logs (device_id, event_type, message, topic, timestamp) VALUES
('BBOX-001', 'connect', '设备连接成功', 'device/BBOX-001/status', now() - interval '5 minutes'),
('BBOX-001', 'data', '电池数据上报', 'device/BBOX-001/battery', now() - interval '1 minute'),
('BBOX-002', 'connect', '设备连接成功', 'device/BBOX-002/status', now() - interval '3 minutes'),
('BBOX-002', 'data', '电池数据上报', 'device/BBOX-002/battery', now() - interval '1 minute'),
('BBOX-003', 'disconnect', '设备断开连接', 'device/BBOX-003/status', now() - interval '2 hours'),
('BBOX-005', 'error', '设备通信异常', 'device/BBOX-005/error', now() - interval '1 hour');