/**
 * 政府端API请求工具 - 重新设计版本
 * 参考银行端设计模式，统一API调用方式
 */
import axios from 'axios'

// 创建axios实例
const instance = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    // 添加认证token
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      // 清除token并跳转到登录页
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// API模块定义 - 参考银行端设计
const api = {
  // 认证相关API
  auth: {
    login: (data) => instance.post('/auth/login', data),
    logout: () => instance.post('/auth/logout'),
    getCurrentUser: () => instance.get('/auth/me'),
    changePassword: (data) => instance.post('/auth/change-password', data)
  },

  // 仪表盘API - 整合数据览仓、市场行情等
  dashboard: {
    // 数据览仓
    getDataCenter: () => instance.get('/dashboard/data-center'),
    // 市场行情
    getMarketPrice: (params) => instance.get('/dashboard/market-price', { params }),
    // 监管统计
    getSupervisionStats: () => instance.get('/dashboard/supervision-stats'),
    // 疫情统计
    getEpidemicStats: () => instance.get('/dashboard/epidemic-stats'),
    // 总体概览
    getOverview: () => instance.get('/dashboard/overview')
  },

  // 政府管理API
  government: {
    // 部门管理
    departments: {
      getList: (params) => instance.get('/government/departments', { params }),
      getById: (id) => instance.get(`/government/departments/${id}`),
      create: (data) => instance.post('/government/departments', data),
      update: (id, data) => instance.put(`/government/departments/${id}`, data),
      delete: (id) => instance.delete(`/government/departments/${id}`)
    },
    // 岗位管理
    positions: {
      getList: (params) => instance.get('/government/positions', { params }),
      getById: (id) => instance.get(`/government/positions/${id}`),
      create: (data) => instance.post('/government/positions', data),
      update: (id, data) => instance.put(`/government/positions/${id}`, data),
      delete: (id) => instance.delete(`/government/positions/${id}`),
      setPermission: (id, data) => instance.post(`/government/positions/${id}/permission`, data)
    },
    // 行政人员管理
    adminStaff: {
      getList: (params) => instance.get('/government/admin-staff', { params }),
      getById: (id) => instance.get(`/government/admin-staff/${id}`),
      create: (data) => instance.post('/government/admin-staff', data),
      update: (id, data) => instance.put(`/government/admin-staff/${id}`, data),
      delete: (id) => instance.delete(`/government/admin-staff/${id}`),
      resetPassword: (id) => instance.post(`/government/admin-staff/${id}/reset-password`)
    },
    // 养殖户管理
    farmers: {
      getList: (params) => instance.get('/government/farmers', { params }),
      getById: (id) => instance.get(`/government/farmers/${id}`),
      create: (data) => instance.post('/government/farmers', data),
      update: (id, data) => instance.put(`/government/farmers/${id}`, data),
      delete: (id) => instance.delete(`/government/farmers/${id}`),
      resetPassword: (id) => instance.post(`/government/farmers/${id}/reset-password`)
    },
    // 智能项圈管理
    collars: {
      getList: (params) => instance.get('/government/collars', { params }),
      getById: (id) => instance.get(`/government/collars/${id}`),
      create: (data) => instance.post('/government/collars', data),
      update: (id, data) => instance.put(`/government/collars/${id}`, data),
      delete: (id) => instance.delete(`/government/collars/${id}`)
    }
  },

  // 人员管理API
  personnel: {
    getList: (params) => instance.get('/personnel', { params }),
    getById: (id) => instance.get(`/personnel/${id}`),
    create: (data) => instance.post('/personnel', data),
    update: (id, data) => instance.put(`/personnel/${id}`, data),
    delete: (id) => instance.delete(`/personnel/${id}`)
  },

  // 监管管理API
  supervision: {
    getList: (params) => instance.get('/supervision', { params }),
    getById: (id) => instance.get(`/supervision/${id}`),
    create: (data) => instance.post('/supervision', data),
    update: (id, data) => instance.put(`/supervision/${id}`, data),
    delete: (id) => instance.delete(`/supervision/${id}`),
    getStats: () => instance.get('/supervision/stats')
  },

  // 审批管理API
  approval: {
    getList: (params) => instance.get('/approval', { params }),
    getById: (id) => instance.get(`/approval/${id}`),
    create: (data) => instance.post('/approval', data),
    update: (id, data) => instance.put(`/approval/${id}`, data),
    delete: (id) => instance.delete(`/approval/${id}`)
  },

  // 疫情管理API
  epidemic: {
    getList: (params) => instance.get('/epidemic', { params }),
    getById: (id) => instance.get(`/epidemic/${id}`),
    create: (data) => instance.post('/epidemic', data),
    update: (id, data) => instance.put(`/epidemic/${id}`, data),
    delete: (id) => instance.delete(`/epidemic/${id}`),
    getStats: () => instance.get('/epidemic/stats')
  },

  // 服务管理API
  service: {
    getList: (params) => instance.get('/service', { params }),
    getById: (id) => instance.get(`/service/${id}`),
    create: (data) => instance.post('/service', data),
    update: (id, data) => instance.put(`/service/${id}`, data),
    delete: (id) => instance.delete(`/service/${id}`)
  },

  // 系统管理API
  system: {
    getList: (params) => instance.get('/system', { params }),
    getById: (id) => instance.get(`/system/${id}`),
    create: (data) => instance.post('/system', data),
    update: (id, data) => instance.put(`/system/${id}`, data),
    delete: (id) => instance.delete(`/system/${id}`)
  },

  // 文件管理API
  files: {
    upload: (data) => instance.post('/files/upload', data),
    download: (id) => instance.get(`/files/${id}/download`),
    delete: (id) => instance.delete(`/files/${id}`)
  }
}

export default api
