// Verify that Vue frontend pages call the correct backend API endpoints.
// This test catches mismatched endpoint names (e.g. /api/bugs vs /api/bug-reports).

const ADMIN_TOKEN = 'c86ae7afbe10fabe3c1d5e1a7fee74feaadfd5dc7be2ab62'

describe('API Endpoint Parity', () => {
  function visitAsAdmin(path) {
    cy.visit(path, {
      onBeforeLoad(win) {
        win.localStorage.setItem('oil_auth_token', ADMIN_TOKEN)
      }
    })
  }

  it('search page loads recipes from /api/recipes', () => {
    cy.intercept('GET', '/api/recipes').as('recipes')
    visitAsAdmin('/')
    cy.wait('@recipes').its('response.statusCode').should('eq', 200)
  })

  it('search page loads oils from /api/oils', () => {
    cy.intercept('GET', '/api/oils').as('oils')
    visitAsAdmin('/')
    cy.wait('@oils').its('response.statusCode').should('eq', 200)
  })

  it('oil reference page loads oils', () => {
    cy.intercept('GET', '/api/oils').as('oils')
    visitAsAdmin('/oils')
    cy.wait('@oils').its('response.statusCode').should('eq', 200)
  })

  it('audit log page loads from /api/audit-log', () => {
    cy.intercept('GET', '/api/audit-log*').as('audit')
    visitAsAdmin('/audit')
    cy.wait('@audit').its('response.statusCode').should('eq', 200)
  })

  it('audit log page does NOT call /api/audit-logs (wrong endpoint)', () => {
    cy.intercept('GET', '/api/audit-logs*').as('wrongAudit')
    visitAsAdmin('/audit')
    cy.wait(2000)
    cy.get('@wrongAudit.all').should('have.length', 0)
  })

  it('bug tracker page loads from /api/bug-reports', () => {
    cy.intercept('GET', '/api/bug-reports').as('bugs')
    visitAsAdmin('/bugs')
    cy.wait('@bugs').its('response.statusCode').should('eq', 200)
  })

  it('bug tracker page does NOT call /api/bugs (wrong endpoint)', () => {
    cy.intercept('GET', '/api/bugs').as('wrongBugs')
    visitAsAdmin('/bugs')
    cy.wait(2000)
    cy.get('@wrongBugs.all').should('have.length', 0)
  })

  it('user management page loads from /api/users', () => {
    cy.intercept('GET', '/api/users').as('users')
    visitAsAdmin('/users')
    cy.wait('@users').its('response.statusCode').should('eq', 200)
  })

  it('categories load from /api/categories', () => {
    cy.intercept('GET', '/api/categories').as('cats')
    visitAsAdmin('/')
    cy.wait(3000)
    // Categories may or may not be fetched depending on page logic
    // Just verify no /api/category-modules calls
    cy.intercept('GET', '/api/category-modules').as('wrongCats')
    cy.get('@wrongCats.all').should('have.length', 0)
  })
})
