// ============================================================
// 用户相关类型
// ============================================================

export interface User {
  id: number;
  email: string;
  password_hash: string;
  role: 'user' | 'admin';
  created_at: string;
}

export interface UserPublic {
  id: number;
  email: string;
  role: 'user' | 'admin';
  created_at: string;
}

export interface RegisterRequest {
  email: string;
  password: string;
}

export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginResponse {
  token: string;
  user: UserPublic;
}

// ============================================================
// 设备相关类型
// ============================================================

export interface Device {
  id: number;
  user_id: number;
  device_name: string;
  device_fingerprint: string;
  public_key: string;
  status: 'online' | 'offline';
  last_seen_at: string;
}

export interface DeviceRegisterRequest {
  device_name: string;
  device_fingerprint: string;
  public_key: string;
}

export interface DeviceStatusUpdate {
  status: 'online' | 'offline';
}

// ============================================================
// 节点相关类型
// ============================================================

export interface RelayNode {
  id: number;
  name: string;
  host: string;
  port: number;
  status: 'online' | 'offline' | 'busy';
  load: number;
  last_heartbeat_at: string;
}

export interface NodeRegisterRequest {
  name: string;
  host: string;
  port: number;
}

export interface NodeHeartbeatRequest {
  load: number;
  status: 'online' | 'offline' | 'busy';
}

// ============================================================
// 连接相关类型
// ============================================================

export interface ConnectionRequest {
  device_id: number;
  target_device_id?: number;
  node_id: number;
}

export interface ConnectionTokenResponse {
  token: string;
  node: RelayNode;
  expires_at: string;
}

export interface TokenVerifyRequest {
  token: string;
  device_id: number;
}

export interface TokenVerifyResponse {
  valid: boolean;
  user_id: number;
  device_id: number;
  message?: string;
}

export interface DisconnectRequest {
  connection_id?: string;
  device_id: number;
}

// ============================================================
// 消息协议类型
// ============================================================

export type MessageType = 'data' | 'control' | 'heartbeat' | 'error' | 'auth';

export interface NetworkMessage {
  type: MessageType;
  from_device_id: number;
  to_device_id: number;
  payload: string;
  timestamp: number;
  message_id: string;
}

export interface ControlMessage {
  action: 'connect' | 'disconnect' | 'heartbeat';
  device_id: number;
  token?: string;
  data?: string;
}

// ============================================================
// API 响应统一格式
// ============================================================

export interface ApiResponse<T = unknown> {
  success: boolean;
  data?: T;
  error?: string;
  message?: string;
}

// ============================================================
// 连接日志
// ============================================================

export interface ConnectionLog {
  id: number;
  user_id: number;
  device_id: number;
  node_id: number;
  action: string;
  status: 'success' | 'failed';
  message: string;
  created_at: string;
}
