import request from '@/utils/request';
import type {
  ApiResponse,
  PageData,
  NotificationParams,
  Notification,
  PersonalData,
  ShortcutItem,
  AvailableComponent,
  DashboardComponentConfig,
  UserComponent,
  ComponentWidthType,
  ComponentCategory
} from './types';

// ==================== 通知相关 ====================

/**
 * 获取通知列表
 */
export const getNotifications = (params?: NotificationParams) => {
  return request<ApiResponse<PageData<Notification>>>({
    url: '/system/notification/notifications',
    method: 'get',
    params
  });
};

/**
 * 获取未读通知计数
 */
export const getUnreadCount = () => {
  return request<
    ApiResponse<{
      total: number;
      todo: number;
      alert: number;
      message: number;
      announcement: number;
    }>
  >({
    url: '/system/notification/unread/count',
    method: 'get'
  });
};

/**
 * 标记通知为已读
 */
export const markNotificationRead = (notificationId: number) => {
  return request<ApiResponse<boolean>>({
    url: `/system/notification/read/${notificationId}`,
    method: 'put'
  });
};

/**
 * 批量标记通知为已读
 */
export const batchMarkRead = (notificationIds: number[]) => {
  return request<ApiResponse<boolean>>({
    url: '/system/notification/read/batch',
    method: 'put',
    data: { notificationIds }
  });
};

/**
 * 标记所有通知为已读
 */
export const markAllRead = (type?: string) => {
  return request<ApiResponse<boolean>>({
    url: '/system/notification/read/all',
    method: 'put',
    params: { type }
  });
};

/**
 * 获取用户待办任务列表
 */
export const getTodoList = (params?: { page?: number; size?: number; templateId?: number; keyword?: string; priority?: string }) => {
  return request<ApiResponse<PageData<any>>>({
    url: '/system/notification/todo/list',
    method: 'get',
    params
  });
};

// ==================== 个人数据相关 ====================

/**
 * 获取个人数据概览
 */
export const getPersonalDataOverview = () => {
  return request<ApiResponse<PersonalData>>({
    url: '/system/dashboard/personal-data',
    method: 'get'
  });
};

// ==================== 快捷入口相关 ====================

/**
 * 获取快捷入口配置
 */
export const getShortcuts = () => {
  return request<ApiResponse<string[]>>({
    url: '/system/dashboard/shortcuts',
    method: 'get'
  });
};

/**
 * 保存快捷入口配置
 */
export const saveShortcuts = (shortcuts: string[]) => {
  return request<ApiResponse<boolean>>({
    url: '/system/dashboard/shortcuts',
    method: 'post',
    data: shortcuts
  });
};

// ==================== 仪表板组件相关 ====================

/**
 * 获取用户可用的组件列表
 */
export const getAvailableComponents = () => {
  return request<ApiResponse<AvailableComponent[]>>({
    url: '/system/dashboard/available-components',
    method: 'get'
  });
};

/**
 * 获取用户当前的组件配置
 */
export const getUserDashboardConfig = () => {
  return request<ApiResponse<DashboardComponentConfig[]>>({
    url: '/system/dashboard/user-config',
    method: 'get'
  });
};

/**
 * 保存用户组件配置
 */
export const saveUserDashboardConfig = (configs: DashboardComponentConfig[]) => {
  return request<ApiResponse<boolean>>({
    url: '/system/dashboard/user-config',
    method: 'post',
    data: { configs }
  });
};

/**
 * 重置用户组件配置为默认配置
 */
export const resetUserDashboardConfig = () => {
  return request<ApiResponse<boolean>>({
    url: '/system/dashboard/reset-config',
    method: 'post'
  });
};

// ==================== 数据转换工具 ====================

/**
 * 组件宽度类型转换为CSS类名
 */
export const getWidthClassName = (widthType: ComponentWidthType): string => {
  const widthMap: Record<ComponentWidthType, string> = {
    'third': 'w-1/3',
    'half': 'w-1/2',
    'two-thirds': 'w-2/3',
    'full': 'w-full'
  };
  return widthMap[widthType] || 'w-full';
};

/**
 * 组件分类转换为中文名称
 */
export const getCategoryLabel = (category: ComponentCategory): string => {
  const categoryMap: Record<ComponentCategory, string> = {
    'data': '数据分析',
    'notification': '通知中心',
    'shortcut': '快捷入口',
    'chart': '图表统计',
    'other': '其他组件'
  };
  return categoryMap[category] || '未知分类';
};

/**
 * 优先级转换为标签类型
 */
export const getPriorityTagType = (priority: string): 'primary' | 'warning' | 'success' | 'info' | 'danger' => {
  const priorityMap: Record<string, 'primary' | 'warning' | 'success' | 'info' | 'danger'> = {
    'high': 'danger',
    'medium': 'warning',
    'low': 'info'
  };
  return priorityMap[priority] || 'info';
};

/**
 * 格式化文件大小
 */
export const formatFileSize = (size: number): string => {
  if (!size) return '0 B';

  const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  let i = 0;

  while (size >= 1024 && i < units.length - 1) {
    size /= 1024;
    i++;
  }

  return `${size.toFixed(2)} ${units[i]}`;
};

/**
 * 数值格式化显示
 */
export const formatNumber = (num: number): string => {
  if (num >= 10000) {
    return `${(num / 10000).toFixed(1)}万`;
  } else if (num >= 1000) {
    return `${(num / 1000).toFixed(1)}k`;
  }
  return num.toString();
};

/**
 * 趋势样式获取
 */
export const getTrendStyle = (trend: number) => {
  if (trend > 0) {
    return {
      color: '#67c23a',
      icon: '↗',
      class: 'trend-up'
    };
  } else if (trend < 0) {
    return {
      color: '#f56c6c',
      icon: '↘',
      class: 'trend-down'
    };
  } else {
    return {
      color: '#909399',
      icon: '→',
      class: 'trend-stable'
    };
  }
};
