import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { mount, flushPromises } from '@vue/test-utils'
import { nextTick } from 'vue'
import { useRefundItemStore } from '@/stores/refund-item'
import type { RefundItemRecord } from '@/stores/refund-item'

// jsdom 不支持 matchMedia，Ant Design Vue 响应式布局需要
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: (query: string) => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: () => {},
    removeListener: () => {},
    addEventListener: () => {},
    removeEventListener: () => {},
    dispatchEvent: () => false,
  }),
})

const mockRouterPush = vi.fn()
vi.mock('vue-router', () => ({
  useRoute: () => ({ query: {} }),
  useRouter: () => ({ push: mockRouterPush }),
}))

vi.mock('@/utils/request', () => ({
  api: {
    get: vi.fn(),
    post: vi.fn(),
    put: vi.fn(),
    patch: vi.fn(),
  },
}))

const mockRefundItems: RefundItemRecord[] = [
  {
    id: 301,
    company_id: 1,
    platform_id: 1,
    store_id: 1,
    refund_id: 201,
    platform_refund_id: 'RF-20260101-001',
    platform_order_id: 'ORD-20260101-001',
    platform_sub_order_id: 'SUB-001',
    platform_product_id: 'PROD-001',
    refund_status_id: 1,
    refund_type_id: 1,
    quantity: 2,
    refund_amount: '99.99',
    currency: 'CNY',
    created_date: '2026-01-01T10:00:00Z',
  },
  {
    id: 302,
    company_id: 2,
    platform_id: 1,
    store_id: 2,
    refund_id: 202,
    platform_refund_id: 'RF-20260201-002',
    platform_order_id: 'ORD-20260201-002',
    platform_sub_order_id: null,
    platform_product_id: 'PROD-002',
    refund_status_id: 5,
    refund_type_id: 2,
    quantity: 1,
    refund_amount: '59.00',
    currency: 'USD',
    created_date: '2026-02-01T14:00:00Z',
  },
]

const mockPaginatedResponse = {
  items: mockRefundItems,
  total: 2,
  page: 1,
  per_page: 15,
}

const mockRefundItemDetail = {
  ...mockRefundItems[0],
  platform_parent_refund_id: 'RF-PARENT-001',
  reason: '商品质量问题',
  buyer_user_id: 'buyer-001',
  order_created_date: '2025-12-30T08:00:00Z',
  order_paid_date: '2025-12-30T08:05:00Z',
  updated_date: '2026-01-02T10:00:00Z',
  completed_date: '2026-01-03T12:00:00Z',
  ext: null,
  created_at: '2026-01-01T10:00:00Z',
  updated_at: '2026-01-02T10:00:00Z',
}

const mockLookupCompanies = [
  { id: 1, name: 'Company A', label: '公司A' },
  { id: 2, name: 'Company B', label: '公司B' },
]
const mockLookupPlatforms = [{ id: 1, name: 'Tmall', label: '天猫', developer_id: 1 }]
const mockLookupStores = [
  { id: 1, company_id: 1, platform_id: 1, name: 'Store 1', label: '店铺1' },
  { id: 2, company_id: 2, platform_id: 1, name: 'Store 2', label: '店铺2' },
]

// ─── Store Tests ───────────────────────────────────────

describe('useRefundItemStore', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    vi.restoreAllMocks()
  })

  describe('initial state', () => {
    it('starts with empty refundItems and default pagination', () => {
      const store = useRefundItemStore()
      expect(store.refundItems).toEqual([])
      expect(store.loading).toBe(false)
      expect(store.pagination.page).toBe(1)
      expect(store.pagination.per_page).toBe(15)
      expect(store.pagination.total).toBe(0)
      expect(store.filters.refund_id).toBeUndefined()
      expect(store.filters.refund_status_id).toBeUndefined()
      expect(store.filters.refund_type_id).toBeUndefined()
      expect(store.filters.platform_refund_id).toBe('')
      expect(store.filters.platform_order_id).toBe('')
      expect(store.filters.platform_product_id).toBe('')
      expect(store.filters.created_date_range).toBeNull()
    })
  })

  describe('fetchRefundItems', () => {
    it('loads data and updates state', async () => {
      const { api } = await import('@/utils/request')
      vi.mocked(api.get).mockResolvedValueOnce(mockPaginatedResponse)

      const store = useRefundItemStore()
      await store.fetchRefundItems()

      expect(api.get).toHaveBeenCalledWith(
        '/api/v1/refund-items',
        expect.objectContaining({
          page: 1,
          per_page: 15,
          refund_id: undefined,
          refund_status_id: undefined,
          refund_type_id: undefined,
          platform_refund_id: undefined,
          platform_order_id: undefined,
          platform_product_id: undefined,
          created_date_from: undefined,
          created_date_to: undefined,
        }),
      )
      expect(store.refundItems).toEqual(mockRefundItems)
      expect(store.pagination.total).toBe(2)
    })

    it('passes filter params correctly', async () => {
      const { api } = await import('@/utils/request')
      vi.mocked(api.get).mockResolvedValueOnce(mockPaginatedResponse)

      const store = useRefundItemStore()
      store.filters.refund_id = 201
      store.filters.refund_status_id = 1
      store.filters.refund_type_id = 2
      store.filters.platform_refund_id = 'RF-001'
      store.filters.platform_order_id = 'ORD-001'
      store.filters.platform_product_id = 'PROD-001'
      store.filters.created_date_range = ['2026-01-01', '2026-01-31']
      store.cascadeValue.company_id = 1
      store.cascadeValue.platform_id = 2
      store.cascadeValue.store_id = 3

      await store.fetchRefundItems()

      expect(api.get).toHaveBeenCalledWith(
        '/api/v1/refund-items',
        expect.objectContaining({
          refund_id: 201,
          refund_status_id: 1,
          refund_type_id: 2,
          platform_refund_id: 'RF-001',
          platform_order_id: 'ORD-001',
          platform_product_id: 'PROD-001',
          created_date_from: '2026-01-01',
          created_date_to: '2026-01-31',
          company_id: 1,
          platform_id: 2,
          store_id: 3,
        }),
      )
    })

    it('shows error message on failure', async () => {
      const { api } = await import('@/utils/request')
      const { message } = await import('ant-design-vue')
      const errorSpy = vi.spyOn(message, 'error')
      vi.mocked(api.get).mockRejectedValueOnce(new Error('Network error'))

      const store = useRefundItemStore()
      await store.fetchRefundItems()

      expect(errorSpy).toHaveBeenCalledWith('Network error')
      expect(store.refundItems).toEqual([])
    })

    it('clears refundItems on fetch failure', async () => {
      const { api } = await import('@/utils/request')
      const { message } = await import('ant-design-vue')
      vi.spyOn(message, 'error')

      const store = useRefundItemStore()
      vi.mocked(api.get).mockResolvedValueOnce(mockPaginatedResponse)
      await store.fetchRefundItems()
      expect(store.refundItems.length).toBe(2)

      vi.mocked(api.get).mockRejectedValueOnce(new Error('Server error'))
      await store.fetchRefundItems()

      expect(store.refundItems).toEqual([])
      expect(store.pagination.total).toBe(0)
    })
  })

  describe('resetFilters', () => {
    it('clears all filters and resets page to 1', () => {
      const store = useRefundItemStore()
      store.filters.refund_id = 201
      store.filters.refund_status_id = 1
      store.filters.refund_type_id = 2
      store.filters.platform_refund_id = 'RF-001'
      store.filters.platform_order_id = 'ORD-001'
      store.filters.platform_product_id = 'PROD-001'
      store.filters.created_date_range = ['2026-01-01', '2026-01-31']
      store.cascadeValue.company_id = 1
      store.cascadeValue.platform_id = 2
      store.cascadeValue.store_id = 3
      store.pagination.page = 5

      store.resetFilters()

      expect(store.filters.refund_id).toBeUndefined()
      expect(store.filters.refund_status_id).toBeUndefined()
      expect(store.filters.refund_type_id).toBeUndefined()
      expect(store.filters.platform_refund_id).toBe('')
      expect(store.filters.platform_order_id).toBe('')
      expect(store.filters.platform_product_id).toBe('')
      expect(store.filters.created_date_range).toBeNull()
      expect(store.cascadeValue.company_id).toBeUndefined()
      expect(store.cascadeValue.platform_id).toBeUndefined()
      expect(store.cascadeValue.store_id).toBeUndefined()
      expect(store.pagination.page).toBe(1)
    })
  })

  describe('name maps', () => {
    it('builds lookup maps from loaded data', async () => {
      const { api } = await import('@/utils/request')
      vi.mocked(api.get)
        .mockResolvedValueOnce(mockLookupCompanies)
        .mockResolvedValueOnce(mockLookupPlatforms)
        .mockResolvedValueOnce(mockLookupStores)

      const store = useRefundItemStore()
      await store.loadLookups()

      expect(store.companyMap.get(1)).toBe('公司A')
      expect(store.platformMap.get(1)).toBe('天猫')
      expect(store.storeMap.get(1)).toBe('店铺1')
    })

    it('filters null and "null" labels in lookup maps', async () => {
      const { api } = await import('@/utils/request')
      vi.mocked(api.get)
        .mockResolvedValueOnce([{ id: 1, name: 'CompanyA', label: null }, { id: 2, name: 'CompanyB', label: 'null' }])
        .mockResolvedValueOnce([{ id: 1, name: 'Tmall', label: 'null', developer_id: 1 }])
        .mockResolvedValueOnce([{ id: 1, company_id: 1, platform_id: 1, name: 'Store1', label: null }])

      const store = useRefundItemStore()
      await store.loadLookups()

      expect(store.companyMap.get(1)).toBe('CompanyA')
      expect(store.companyMap.get(2)).toBe('CompanyB')
      expect(store.platformMap.get(1)).toBe('Tmall')
      expect(store.storeMap.get(1)).toBe('Store1')
    })

    it('handles loadLookups failure gracefully', async () => {
      const { api } = await import('@/utils/request')
      const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
      vi.mocked(api.get).mockRejectedValueOnce(new Error('Network error'))

      const store = useRefundItemStore()
      await store.loadLookups()

      expect(warnSpy).toHaveBeenCalledWith('加载查找表数据失败', expect.any(Error))
      expect(store.companyMap.size).toBe(0)
      warnSpy.mockRestore()
    })
  })
})

// ─── Page Component Tests ──────────────────────────────

describe('RefundItemsPage', () => {
  let wrapper: ReturnType<typeof mount>

  beforeEach(() => {
    setActivePinia(createPinia())
    vi.restoreAllMocks()
    mockRouterPush.mockClear()
    document.body.innerHTML = ''
  })

  afterEach(() => {
    wrapper?.unmount()
    document.body.innerHTML = ''
  })

  async function mountPage() {
    const { api } = await import('@/utils/request')
    vi.mocked(api.get).mockImplementation((url: string) => {
      if (url === '/api/v1/refund-items') return Promise.resolve(mockPaginatedResponse) as never
      if (url === '/api/v1/companies') return Promise.resolve(mockLookupCompanies) as never
      if (url === '/api/v1/platforms') return Promise.resolve(mockLookupPlatforms) as never
      if (url === '/api/v1/stores') return Promise.resolve(mockLookupStores) as never
      return Promise.resolve([]) as never
    })

    const { default: RefundItemsPage } = await import('../index.vue')

    wrapper = mount(RefundItemsPage, {
      attachTo: document.body,
      global: {
        stubs: {
          SearchOutlined: { template: '<span />' },
          ReloadOutlined: { template: '<span />' },
          EyeOutlined: { template: '<span />' },
          CopyOutlined: { template: '<span class="copy-icon-stub" />' },
          CascadeFilter: { template: '<div class="cascade-filter-stub" />' },
        },
      },
    })
    await flushPromises()
    await nextTick()
    return { wrapper, api }
  }

  it('renders page title and action buttons', async () => {
    await mountPage()

    expect(wrapper.text()).toContain('退款项管理')
    const buttons = wrapper.findAll('.ant-btn')
    const buttonTexts = buttons.map((b) => b.text())
    expect(buttonTexts.some((t) => t.includes('搜索'))).toBe(true)
    expect(buttonTexts.some((t) => t.includes('重置'))).toBe(true)
  }, 15000)

  it('calls fetchRefundItems and loadLookups on mount', async () => {
    const { api } = await mountPage()

    expect(api.get).toHaveBeenCalledWith(
      '/api/v1/refund-items',
      expect.objectContaining({
        page: 1,
        per_page: 15,
      }),
    )
    expect(api.get).toHaveBeenCalledWith('/api/v1/companies')
    expect(api.get).toHaveBeenCalledWith('/api/v1/platforms')
    expect(api.get).toHaveBeenCalledWith('/api/v1/stores')
  })

  it('renders table with refund item data', async () => {
    await mountPage()

    const html = wrapper.html()
    expect(html).toContain('RF-20260101-001')
    expect(html).toContain('ORD-20260101-001')
    expect(html).toContain('99.99')
    expect(html).toContain('PROD-001')
  })

  it('renders refund status tags correctly', async () => {
    await mountPage()

    const html = wrapper.html()
    expect(html).toContain('退款申请中')
    expect(html).toContain('退款成功')
  })

  it('renders refund type tags correctly', async () => {
    await mountPage()

    const html = wrapper.html()
    expect(html).toContain('未发货前退款')
    expect(html).toContain('退货退款')
  })

  it('renders company/platform/store names from lookup maps', async () => {
    await mountPage()

    const html = wrapper.html()
    expect(html).toContain('公司A')
    expect(html).toContain('店铺1')
  })

  it('renders refund_id as clickable link', async () => {
    await mountPage()

    const html = wrapper.html()
    // refund_id 201 should appear as a link
    expect(html).toContain('201')
  })

  it('renders CascadeFilter component', async () => {
    await mountPage()

    expect(wrapper.find('.cascade-filter-stub').exists()).toBe(true)
  })

  it('search button resets page to 1 and fetches', async () => {
    const { api } = await mountPage()

    const store = useRefundItemStore()
    store.pagination.page = 3
    store.filters.platform_refund_id = 'RF-001'
    vi.mocked(api.get).mockClear()
    vi.mocked(api.get).mockResolvedValue(mockPaginatedResponse)

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const searchBtn = buttons.find((b) => b.textContent?.trim().includes('搜索'))
    searchBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    expect(store.pagination.page).toBe(1)
    expect(api.get).toHaveBeenCalledWith(
      '/api/v1/refund-items',
      expect.objectContaining({
        page: 1,
        platform_refund_id: 'RF-001',
      }),
    )
  })

  it('resets filters when clicking reset button', async () => {
    await mountPage()

    const store = useRefundItemStore()
    store.filters.refund_id = 201
    store.filters.refund_status_id = 1
    store.filters.refund_type_id = 2
    store.filters.platform_refund_id = 'RF-001'
    store.filters.platform_order_id = 'ORD-001'
    store.filters.platform_product_id = 'PROD-001'
    store.filters.created_date_range = ['2026-01-01', '2026-01-31']

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const resetBtn = buttons.find((b) => b.textContent?.trim().includes('重置'))
    expect(resetBtn).toBeDefined()

    resetBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    expect(store.filters.refund_id).toBeUndefined()
    expect(store.filters.refund_status_id).toBeUndefined()
    expect(store.filters.refund_type_id).toBeUndefined()
    expect(store.filters.platform_refund_id).toBe('')
    expect(store.filters.platform_order_id).toBe('')
    expect(store.filters.platform_product_id).toBe('')
    expect(store.filters.created_date_range).toBeNull()
  })

  it('opens detail drawer when clicking view button', async () => {
    const { api } = await mountPage()
    vi.mocked(api.get).mockResolvedValueOnce(mockRefundItemDetail)

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const viewBtn = buttons.find((b) => b.textContent?.trim() === '查看')
    expect(viewBtn).toBeDefined()

    viewBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    const drawer = document.body.querySelector('.ant-drawer')
    expect(drawer).toBeTruthy()
  })

  it('displays refund item detail info in drawer', async () => {
    const { api } = await mountPage()
    vi.mocked(api.get).mockResolvedValueOnce(mockRefundItemDetail)

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const viewBtn = buttons.find((b) => b.textContent?.trim() === '查看')
    viewBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    const drawerHtml = document.body.querySelector('.ant-drawer')?.innerHTML || ''
    expect(drawerHtml).toContain('退款项详情')
    expect(drawerHtml).toContain('RF-20260101-001')
    expect(drawerHtml).toContain('商品质量问题')
    expect(drawerHtml).toContain('buyer-001')
    expect(drawerHtml).toContain('退款申请中')
    expect(drawerHtml).toContain('RF-PARENT-001')
  })

  it('displays ext JSON in drawer when available', async () => {
    const { api } = await mountPage()
    const detailWithExt = {
      ...mockRefundItemDetail,
      ext: { return_logistics: 'SF1234567890', warehouse: 'WH-01' },
    }
    vi.mocked(api.get).mockResolvedValueOnce(detailWithExt)

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const viewBtn = buttons.find((b) => b.textContent?.trim() === '查看')
    viewBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    const drawerHtml = document.body.querySelector('.ant-drawer')?.innerHTML || ''
    expect(drawerHtml).toContain('return_logistics')
    expect(drawerHtml).toContain('SF1234567890')
  })

  it('shows placeholder when ext is null', async () => {
    const { api } = await mountPage()
    vi.mocked(api.get).mockResolvedValueOnce({ ...mockRefundItemDetail, ext: null })

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const viewBtn = buttons.find((b) => b.textContent?.trim() === '查看')
    viewBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    const drawerHtml = document.body.querySelector('.ant-drawer')?.innerHTML || ''
    expect(drawerHtml).toContain('暂无数据')
  })

  it('shows error message when detail request fails', async () => {
    const { api } = await mountPage()
    const { message } = await import('ant-design-vue')
    const errorSpy = vi.spyOn(message, 'error')
    vi.mocked(api.get).mockRejectedValueOnce(new Error('Not found'))

    const buttons = Array.from(document.body.querySelectorAll('.ant-btn'))
    const viewBtn = buttons.find((b) => b.textContent?.trim() === '查看')
    viewBtn?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    await flushPromises()
    await nextTick()

    expect(errorSpy).toHaveBeenCalledWith('获取退款项详情失败')
  })

  it('filter form has filter-form class', async () => {
    await mountPage()

    const form = wrapper.find('form.filter-form')
    expect(form.exists()).toBe(true)
  })

  it('renders platform name from lookup map (not hardcoded fallback)', async () => {
    await mountPage()

    const html = wrapper.html()
    expect(html).toContain('天猫')
    expect(html).not.toContain('平台 #1')
  })

  it('renders copy buttons before text in platform_refund_id column', async () => {
    await mountPage()

    const cells = wrapper.findAll('td')
    const refundIdCell = cells.find((c) => c.text().includes('RF-20260101-001'))
    expect(refundIdCell).toBeDefined()

    const cellHtml = refundIdCell!.html()
    const copyPos = cellHtml.indexOf('copy-icon-stub')
    const textPos = cellHtml.indexOf('RF-20260101-001')
    expect(copyPos).toBeLessThan(textPos)
  })

  it('renders copy button for platform_sub_order_id when value exists', async () => {
    await mountPage()

    const cells = wrapper.findAll('td')
    const subOrderCell = cells.find((c) => c.text().includes('SUB-001'))
    expect(subOrderCell).toBeDefined()
    expect(subOrderCell!.find('.copy-icon-stub').exists()).toBe(true)
  })

  it('does not render copy button for platform_sub_order_id when value is null', async () => {
    await mountPage()

    const cells = wrapper.findAll('td')
    const dashCells = cells.filter((c) => c.text().trim() === '-')
    const noCopyDash = dashCells.some((c) => !c.find('.copy-icon-stub').exists())
    expect(noCopyDash).toBe(true)
  })

  it('renders copy button for platform_product_id column', async () => {
    await mountPage()

    const cells = wrapper.findAll('td')
    const prodIdCell = cells.find((c) => c.text().includes('PROD-001'))
    expect(prodIdCell).toBeDefined()
    expect(prodIdCell!.find('.copy-icon-stub').exists()).toBe(true)
  })

  it('handleGoToOrder includes auto_submit in router push', async () => {
    await mountPage()

    const links = wrapper.findAll('a')
    const orderLink = links.find((l) => l.text().includes('ORD-20260101-001'))
    expect(orderLink).toBeDefined()

    await orderLink!.trigger('click')
    await flushPromises()

    expect(mockRouterPush).toHaveBeenCalledWith({
      path: '/orders',
      query: { platform_order_id: 'ORD-20260101-001', auto_submit: '1' },
    })
  })

  it('handleGoToRefund passes platform_refund_id and auto_submit', async () => {
    await mountPage()

    // refund_id column renders as clickable link
    const links = wrapper.findAll('a')
    const refundLink = links.find((l) => l.text().trim() === '201')
    expect(refundLink).toBeDefined()

    await refundLink!.trigger('click')
    await flushPromises()

    expect(mockRouterPush).toHaveBeenCalledWith({
      path: '/refunds',
      query: { platform_refund_id: 'RF-20260101-001', auto_submit: '1' },
    })
  })
})
