import { requestClient } from '#/api/request'

enum Api {
  // 站点管理
  SiteList = '/adminapi/site/lists',
  SiteInfo = '/adminapi/site/info',
  SiteAdd = '/adminapi/site/add',
  SiteEdit = '/adminapi/site/edit',
  SiteDelete = '/adminapi/site/del',
  SiteClose = '/adminapi/site/close',
  SiteOpen = '/adminapi/site/open',
  SiteInit = '/adminapi/site/init',
  SiteStatusList = '/adminapi/site/statuslist',
  SiteAllowChange = '/adminapi/site/allow_change',
  SitePutAllowChange = '/adminapi/site/put_allow_change',
  
  // 站点分组管理
  SiteGroupList = '/adminapi/site_group/lists',
  SiteGroupInfo = '/adminapi/site_group/info',
  SiteGroupAdd = '/adminapi/site_group/add',
  SiteGroupEdit = '/adminapi/site_group/edit',
  SiteGroupDelete = '/adminapi/site_group/del',
  SiteGroupAll = '/adminapi/site_group/all',
  SiteGroupUser = '/adminapi/site_group/user',
  
  // 站点用户管理
  SiteUserList = '/adminapi/site/user',
  SiteUserInfo = '/adminapi/site/user/info',
  SiteUserAdd = '/adminapi/site/user/add',
  SiteUserEdit = '/adminapi/site/user/edit',
  SiteUserLock = '/adminapi/site/user/lock',
  SiteUserUnlock = '/adminapi/site/user/unlock',
  SiteUserDelete = '/adminapi/site/user/del',
  
  // 操作日志
  SiteLogList = '/adminapi/site/log',
  SiteLogInfo = '/adminapi/site/log/info',
  SiteLogDestroy = '/adminapi/site/log/destroy',
  
  // 账单管理
  SiteAccountList = '/adminapi/site/account',
  SiteAccountInfo = '/adminapi/site/account/info',
  SiteAccountStat = '/adminapi/site/account/stat',
  SiteAccountType = '/adminapi/site/account/type',
  
  // 应用和插件
  SiteAddons = '/adminapi/site/addons',
  SiteShowApp = '/adminapi/site/showApp',
  SiteShowMarketing = '/adminapi/site/showMarketing',
  
  // 验证码
  Captcha = '/adminapi/captcha'
}

/**
 * 站点管理 API
 */
export const useSiteApi = () => {
  return {
    // 获取站点列表
    getSiteList: (params: any) => requestClient.get(Api.SiteList, { params }),
    
    // 获取站点详情
    getSiteInfo: (siteId: number) => requestClient.get(`${Api.SiteInfo}/${siteId}`),
    
    // 添加站点
    addSite: (params: any) => requestClient.post(Api.SiteAdd, params),
    
    // 编辑站点
    editSite: (params: any) => requestClient.put(Api.SiteEdit, params),
    
    // 删除站点
    deleteSite: (params: any) => requestClient.delete(`${Api.SiteDelete}/${params.site_id}`, { 
      params: { 
        captcha_code: params.captcha_code, 
        captcha_key: params.captcha_key 
      } 
    }),
    
    // 关闭站点
    closeSite: (params: any) => requestClient.put(`${Api.SiteClose}/${params.site_id}`, params),
    
    // 开启站点
    openSite: (params: any) => requestClient.put(`${Api.SiteOpen}/${params.site_id}`, params),
    
    // 初始化站点
    initSite: (params: any) => requestClient.post(Api.SiteInit, {
      site_id: params.site_id,
      captcha_code: params.captcha_code,
      captcha_key: params.captcha_key
    }),
    
    // 获取状态列表
    getStatusList: () => requestClient.get(Api.SiteStatusList),
    
    // 获取是否允许切换站点
    getSiteAllowChange: () => requestClient.get(Api.SiteAllowChange),
    
    // 设置是否允许切换站点
    putSiteAllowChange: (params: any) => requestClient.put(Api.SitePutAllowChange, params),
    
    // 获取站点分组列表
    getSiteGroupList: (params: any) => requestClient.get(Api.SiteGroupList, { params }),
    
    // 获取站点分组详情
    getSiteGroupInfo: (groupId: number) => requestClient.get(`${Api.SiteGroupInfo}/${groupId}`),
    
    // 添加站点分组
    addSiteGroup: (params: any) => requestClient.post(Api.SiteGroupAdd, params),
    
    // 编辑站点分组
    editSiteGroup: (params: any) => requestClient.put(Api.SiteGroupEdit, params),
    
    // 删除站点分组
    deleteSiteGroup: (groupId: number) => requestClient.delete(`${Api.SiteGroupDelete}/${groupId}`),
    
    // 获取所有站点分组
    getSiteGroupAll: (params: any = {}) => requestClient.get(Api.SiteGroupAll, { params }),
    
    // 获取用户站点分组（包含站点数量）
    getUserSiteGroupAll: (params: any = {}) => requestClient.get(Api.SiteGroupUser, { params }),
    
    // 获取站点用户列表
    getUserList: (params: any) => requestClient.get(Api.SiteUserList, { params }),
    
    // 获取站点用户详情
    getUserInfo: (uid: number) => requestClient.get(`${Api.SiteUserInfo}/${uid}`),
    
    // 添加用户
    addUser: (params: any) => requestClient.post(Api.SiteUserAdd, params),
    
    // 编辑用户
    editUser: (params: any) => requestClient.put(Api.SiteUserEdit, params),
    
    // 锁定用户
    lockUser: (uid: number) => requestClient.put(`${Api.SiteUserLock}/${uid}`),
    
    // 解锁用户
    unlockUser: (uid: number) => requestClient.put(`${Api.SiteUserUnlock}/${uid}`),
    
    // 删除用户
    deleteUser: (uid: number) => requestClient.delete(`${Api.SiteUserDelete}/${uid}`),
    
    // 获取操作日志列表
    getLogList: (params: any) => requestClient.get(Api.SiteLogList, { params }),
    
    // 获取操作日志详情
    getLogInfo: (id: number) => requestClient.get(`${Api.SiteLogInfo}/${id}`),
    
    // 清空操作日志
    logDestroy: () => requestClient.delete(Api.SiteLogDestroy),
    
    // 获取账单列表
    getAccountList: (params: any) => requestClient.get(Api.SiteAccountList, { params }),
    
    // 获取账单详情
    getAccountInfo: (id: number) => requestClient.get(`${Api.SiteAccountInfo}/${id}`),
    
    // 获取账单统计
    getAccountStat: () => requestClient.get(Api.SiteAccountStat),
    
    // 获取账单类型
    getAccountType: () => requestClient.get(Api.SiteAccountType),
    
    // 获取站点应用
    getSiteAddons: () => requestClient.get(Api.SiteAddons),
    
    // 获取显示应用
    getShowApp: () => requestClient.get(Api.SiteShowApp),
    
    // 获取营销工具
    getShowMarketing: () => requestClient.get(Api.SiteShowMarketing),
    
    // 获取验证码
    getCaptcha: () => requestClient.get(Api.Captcha)
  }
}

/**
 * 站点管理类型定义
 */
export interface SiteInfo {
  site_id: number
  site_name: string
  group_id: number
  group_name: string
  keywords: string
  app_type: string
  logo: string
  desc: string
  status: number
  status_name: string
  create_time: string
  expire_time: string
  site_domain: string
  meta_title: string
  meta_desc: string
  meta_keyword: string
  app: string
  addons: string
  initalled_addon: string
  admin: {
    username: string
    real_name: string
  }
}

export interface SiteGroupInfo {
  group_id: number
  group_name: string
  group_desc: string
  app: string
  addon: string
  app_list: Array<{
    key: string
    title: string
    icon: string
  }>
  addon_list: Array<{
    key: string
    title: string
    icon: string
  }>
  create_time: string
  update_time: string
  site_count?: number
}

export interface SiteUserInfo {
  uid: number
  username: string
  real_name: string
  head_img: string
  status: number
  create_time: string
  last_login_time: string
  site_id: number
}

export interface SiteLogInfo {
  id: number
  uid: number
  username: string
  action: string
  ip: string
  create_time: string
  user_agent: string
}

export interface SiteAccountInfo {
  id: number
  site_id: number
  type: string
  money: number
  trade_no: string
  remark: string
  create_time: string
}

export interface CaptchaInfo {
  captcha_img: string
  captcha_key: string
} 