import { ref } from 'vue'
import type { EditableKuaishouEticketShop } from './types'
import type { AdminKuaishouEticketShopConfigItem } from '@/types/admin'

export function useKuaishouEticketShopManager() {
  const kuaishouEticketShops = ref<EditableKuaishouEticketShop[]>([])
  const expandedKuaishouEticketShopIds = ref<string[]>([])

  function addKuaishouEticketShop() {
    const shop = createEmptyKuaishouEticketShop()
    kuaishouEticketShops.value.unshift(shop)
    return { shop, newSelectedId: shop.id }
  }

  function removeKuaishouEticketShop(id: string, currentSelectedId: string) {
    kuaishouEticketShops.value = kuaishouEticketShops.value.filter((item) => item.id !== id)
    collapseKuaishouEticketShop(id)
    if (currentSelectedId === id) {
      return kuaishouEticketShops.value[0]?.id || ''
    }
    return currentSelectedId
  }

  function isKuaishouEticketShopExpanded(id: string) {
    return expandedKuaishouEticketShopIds.value.includes(id)
  }

  function expandKuaishouEticketShop(id: string) {
    if (expandedKuaishouEticketShopIds.value.includes(id)) {
      return
    }
    expandedKuaishouEticketShopIds.value = [id, ...expandedKuaishouEticketShopIds.value]
  }

  function collapseKuaishouEticketShop(id: string) {
    expandedKuaishouEticketShopIds.value = expandedKuaishouEticketShopIds.value.filter(
      (item) => item !== id,
    )
  }

  function toggleKuaishouEticketShop(id: string) {
    if (isKuaishouEticketShopExpanded(id)) {
      collapseKuaishouEticketShop(id)
      return
    }
    expandKuaishouEticketShop(id)
  }

  function expandAllKuaishouEticketShops() {
    expandedKuaishouEticketShopIds.value = kuaishouEticketShops.value.map((item) => item.id)
  }

  function collapseAllKuaishouEticketShops() {
    expandedKuaishouEticketShopIds.value = []
  }

  function describeKuaishouEticketShop(shop: EditableKuaishouEticketShop) {
    const status = shop.cookie.trim() ? 'Cookie 已就绪' : '待补 Cookie'
    const identity = shop.shopId.trim() ? '已识别店铺' : '待读取信息'
    return `${status} · ${identity}`
  }

  function getKuaishouEticketShopInitial(shop: EditableKuaishouEticketShop) {
    const label = shop.kshopName.trim() || shop.shopId.trim() || '店'
    return label.slice(0, 1).toUpperCase()
  }

  function setShopsFromConfig(shops: AdminKuaishouEticketShopConfigItem[]) {
    kuaishouEticketShops.value = shops.map(mapEditableKuaishouEticketShop)
    return kuaishouEticketShops.value
  }

  function findShopByShopId(shopId: string) {
    return kuaishouEticketShops.value.find((item) => item.shopId && item.shopId === shopId)
  }

  return {
    kuaishouEticketShops,
    expandedKuaishouEticketShopIds,
    addKuaishouEticketShop,
    removeKuaishouEticketShop,
    isKuaishouEticketShopExpanded,
    expandKuaishouEticketShop,
    collapseKuaishouEticketShop,
    toggleKuaishouEticketShop,
    expandAllKuaishouEticketShops,
    collapseAllKuaishouEticketShops,
    describeKuaishouEticketShop,
    getKuaishouEticketShopInitial,
    setShopsFromConfig,
    findShopByShopId,
  }
}

function createEmptyKuaishouEticketShop(): EditableKuaishouEticketShop {
  return {
    id: crypto.randomUUID(),
    shopId: '',
    kshopName: '',
    cookie: '',
    userAvatar: '',
    enabled: true,
  }
}

function mapEditableKuaishouEticketShop(
  item: AdminKuaishouEticketShopConfigItem,
): EditableKuaishouEticketShop {
  return {
    id: crypto.randomUUID(),
    shopId: item.shopId,
    kshopName: item.kshopName,
    cookie: item.cookie,
    userAvatar: item.userAvatar,
    enabled: item.enabled !== false,
  }
}
