import { describe, it, expect, beforeAll, beforeEach, afterAll, vi } from 'vitest'

// The public app pulls in the websocket plugin which opens real WS upgrades on
// requests — out of scope for HTTP route tests. Mock it to a no-op.
vi.mock('../../src/plugins/websocket.js', () => ({
  sendToUser: vi.fn(() => false),
  sendToSessionParticipant: vi.fn(() => false),
  registerWebSocketPlugin: vi.fn(async () => {}),
  registerWebSocketRoute: vi.fn(),
  isUserOnlineWs: vi.fn(() => false),
  getSessionConnections: vi.fn(() => ({})),
}))

vi.mock('../../src/services/notification.service.js', () => ({
  sendPushNotification: vi.fn(async () => true),
  registerDeviceToken: vi.fn(async () => {}),
}))

const { buildPublic } = await import('../helpers/server.js')
const { resetDb, resetAppConfig, db } = await import('../helpers/db.js')
const { createCustomer } = await import('../helpers/fixtures.js')
const { customerJwt, authHeader } = await import('../helpers/jwt.js')
const { PaymentRequestStatus } = await import('../../src/constants.js')

describe('POST /api/client/payment-requests', () => {
  let app
  let customer
  let token

  beforeAll(async () => {
    await resetAppConfig()
    app = await buildPublic()
  })

  beforeEach(async () => {
    await resetDb()
    // Phone-verified customer (phone non-null) is required for first-session-discount
    // eligibility under the Phase 4 predicate.
    // Random suffix avoids the unique-phone constraint clashing with parallel test files.
    const phone = `+628${Math.floor(Math.random() * 1e10).toString().padStart(10, '0')}`
    customer = await createCustomer({ callName: 'PaymentTester', phone })
    token = customerJwt(customer.id)
  })

  afterAll(async () => {
    await app?.close()
  })

  it('happy path returns 201 + a pending payment-session row at the discounted price for an eligible customer', async () => {
    const res = await app.inject({
      method: 'POST',
      url: '/api/client/payment-requests',
      headers: authHeader(token),
      // Discount duration default is 12 minutes (config seed). Eligible customer →
      // amount forced to actual_price_idr (2000), is_first_session_discount=true.
      payload: { duration_minutes: 12 },
    })

    expect(res.statusCode).toBe(201)
    const body = res.json()
    expect(body.success).toBe(true)
    expect(body.data.status).toBe(PaymentRequestStatus.PENDING)
    expect(body.data.duration_minutes).toBe(12)
    expect(body.data.is_first_session_discount).toBe(true)
    expect(body.data.amount).toBe(2000)
    expect(body.data.is_extension).toBe(false)
    expect(body.data.mode).toBe('chat')

    // Verify persistence
    const sql = db()
    const [row] = await sql`SELECT * FROM payment_requests WHERE id = ${body.data.id}`
    expect(row).toBeDefined()
    expect(row.customer_id).toBe(customer.id)
  })

  it('non-eligible customer pays the standard tier price', async () => {
    // Drop first-session-discount eligibility by inserting a completed session.
    const sql = db()
    await sql`
      INSERT INTO chat_sessions (customer_id, status, duration_minutes, price)
      VALUES (${customer.id}, 'completed', 12, 12000)
    `

    const res = await app.inject({
      method: 'POST',
      url: '/api/client/payment-requests',
      headers: authHeader(token),
      payload: { duration_minutes: 12 },
    })

    expect(res.statusCode).toBe(201)
    const body = res.json()
    expect(body.data.is_first_session_discount).toBe(false)
    // 12-minute tier in Phase 4 chat tiers = 12000 IDR.
    expect(body.data.amount).toBe(12000)
  })

  it('POST /:id/confirm transitions the row and returns 200', async () => {
    // Use a non-discount tier (5 min @ 5000 IDR) so we exercise the regular confirm path.
    const createRes = await app.inject({
      method: 'POST',
      url: '/api/client/payment-requests',
      headers: authHeader(token),
      payload: { duration_minutes: 5 },
    })
    expect(createRes.statusCode).toBe(201)
    const created = createRes.json().data
    expect(created.status).toBe(PaymentRequestStatus.PENDING)
    expect(created.is_first_session_discount).toBe(false)
    expect(created.amount).toBe(5000)

    const confirmRes = await app.inject({
      method: 'POST',
      url: `/api/client/payment-requests/${created.id}/confirm`,
      headers: authHeader(token),
      payload: {},
    })

    expect(confirmRes.statusCode).toBe(200)
    const confirmed = confirmRes.json().data
    expect(confirmed.id).toBe(created.id)
    expect(confirmed.status).toBe(PaymentRequestStatus.CONFIRMED)
    expect(confirmed.confirmed_at).toBeTruthy()
  })

  it('call-mode payment session uses the call tier price group', async () => {
    // 20-minute call tier in Phase 4 = 17000 IDR.
    const res = await app.inject({
      method: 'POST',
      url: '/api/client/payment-requests',
      headers: authHeader(token),
      payload: { duration_minutes: 20, mode: 'call' },
    })

    expect(res.statusCode).toBe(201)
    const body = res.json()
    expect(body.data.mode).toBe('call')
    // Eligible customer but discount modes default = ['chat'], so call is full price.
    expect(body.data.is_first_session_discount).toBe(false)
    expect(body.data.amount).toBe(17000)
  })
})
