import type { Page } from '@playwright/test'
import {
  mockLoginResponse,
  mockAdminInfo,
  mockMachines,
  mockCameras,
  mockDashboardStats,
  wrapResponse,
  wrapPageResponse,
  wrapArrayResponse
} from '../../fixtures'

/**
 * Mock login API responses
 * POST /admin/auth/login
 */
export async function mockLoginAPI(page: Page) {
  await page.route('**/admin/auth/login', async (route) => {
    const request = route.request()

    if (request.method() !== 'POST') {
      await route.continue()
      return
    }

    const body = request.postDataJSON()

    if (body?.username === 'admin' && body?.password === '123456') {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify(wrapResponse(mockLoginResponse))
      })
    } else {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({
          code: 401,
          message: '用户名或密码错误',
          data: null,
          timestamp: Date.now()
        })
      })
    }
  })
}

/**
 * Mock logout API responses
 * POST /admin/auth/logout
 */
export async function mockLogoutAPI(page: Page) {
  await page.route('**/admin/auth/logout', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapResponse(null))
    })
  })
}

/**
 * Mock admin info API responses
 * GET /admin/auth/info
 */
export async function mockAdminInfoAPI(page: Page) {
  await page.route('**/admin/auth/info', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapResponse(mockAdminInfo))
    })
  })
}

/**
 * Mock machines list API responses (paginated)
 * POST /admin/machines/list
 */
export async function mockMachinesListAPI(page: Page) {
  await page.route('**/admin/machines/list', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapPageResponse(mockMachines))
    })
  })
}

/**
 * Mock machines listAll API responses (non-paginated)
 * GET /admin/machines/listAll
 */
export async function mockMachinesListAllAPI(page: Page) {
  await page.route('**/admin/machines/listAll', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapArrayResponse(mockMachines))
    })
  })
}

/**
 * Mock machine detail API responses
 * GET /admin/machines/detail
 */
export async function mockMachineDetailAPI(page: Page) {
  await page.route('**/admin/machines/detail**', async (route) => {
    const url = new URL(route.request().url())
    const id = parseInt(url.searchParams.get('id') || '1')
    const machine = mockMachines.find((m) => m.id === id) || mockMachines[0]
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapResponse(machine))
    })
  })
}

/**
 * Mock cameras list API responses (paginated)
 * POST /admin/cameras/list
 */
export async function mockCamerasListAPI(page: Page) {
  await page.route('**/admin/cameras/list', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapPageResponse(mockCameras))
    })
  })
}

/**
 * Mock cameras listAll API responses (non-paginated)
 * GET /admin/cameras/listAll
 */
export async function mockCamerasListAllAPI(page: Page) {
  await page.route('**/admin/cameras/listAll', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapArrayResponse(mockCameras))
    })
  })
}

/**
 * Mock camera list API (controller)
 * POST /camera/list
 */
export async function mockCameraListAPI(page: Page) {
  await page.route('**/camera/list', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapArrayResponse(mockCameras))
    })
  })
}

/**
 * Mock dashboard stats API responses
 * GET /admin/stats/dashboard
 */
export async function mockDashboardStatsAPI(page: Page) {
  await page.route('**/admin/stats/dashboard', async (route) => {
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(wrapResponse(mockDashboardStats))
    })
  })
}

/**
 * Setup all common API mocks for authenticated pages
 */
export async function setupAuthenticatedMocks(page: Page) {
  await Promise.all([
    mockAdminInfoAPI(page),
    mockLogoutAPI(page),
    mockMachinesListAPI(page),
    mockMachinesListAllAPI(page),
    mockMachineDetailAPI(page),
    mockCamerasListAPI(page),
    mockCamerasListAllAPI(page),
    mockCameraListAPI(page),
    mockDashboardStatsAPI(page)
  ])
}

/**
 * Setup all API mocks including login
 */
export async function setupAllMocks(page: Page) {
  await mockLoginAPI(page)
  await setupAuthenticatedMocks(page)
}
