/**
 * Tests de guards de rol para rutas protegidas.
 * Verifica las condiciones de redirección para client_viewer e client_editor.
 */
import { describe, it, expect } from 'vitest'
import type { AuthUser } from '@/auth/types'

// ─── Helper: construir un usuario de prueba con un rol específico ──────────────

function makeUser(platformRole: AuthUser['platformRole']): AuthUser {
  return {
    id: 'user-test',
    email: 'test@test.com',
    name: 'Test User',
    platformRole,
  }
}

// ─── Guard de workspace: client_viewer → redirige a /report/$processId ────────

function workspaceGuardShouldRedirect(user: AuthUser | null): boolean {
  return user?.platformRole === 'client_viewer'
}

describe('workspace route guard', () => {
  it('client_viewer debe redirigir al reporte', () => {
    expect(workspaceGuardShouldRedirect(makeUser('client_viewer'))).toBe(true)
  })

  it('client_editor NO debe redirigir', () => {
    expect(workspaceGuardShouldRedirect(makeUser('client_editor'))).toBe(false)
  })

  it('platform_admin NO debe redirigir', () => {
    expect(workspaceGuardShouldRedirect(makeUser('platform_admin'))).toBe(false)
  })

  it('member NO debe redirigir', () => {
    expect(workspaceGuardShouldRedirect(makeUser('member'))).toBe(false)
  })

  it('usuario null NO debe redirigir (no autenticado)', () => {
    expect(workspaceGuardShouldRedirect(null)).toBe(false)
  })
})

// ─── Guard de importación: solo platform_admin y member pueden importar ────────

function importGuardShouldRedirect(user: AuthUser | null): boolean {
  if (!user) return false  // RootComponent maneja el redirect a /login
  const allowed: AuthUser['platformRole'][] = ['platform_admin', 'member']
  return !allowed.includes(user.platformRole)
}

describe('import route guard', () => {
  it('client_editor debe redirigir a /', () => {
    expect(importGuardShouldRedirect(makeUser('client_editor'))).toBe(true)
  })

  it('client_viewer debe redirigir a /', () => {
    expect(importGuardShouldRedirect(makeUser('client_viewer'))).toBe(true)
  })

  it('guest debe redirigir a /', () => {
    expect(importGuardShouldRedirect(makeUser('guest'))).toBe(true)
  })

  it('platform_admin NO debe redirigir', () => {
    expect(importGuardShouldRedirect(makeUser('platform_admin'))).toBe(false)
  })

  it('member NO debe redirigir', () => {
    expect(importGuardShouldRedirect(makeUser('member'))).toBe(false)
  })
})

// ─── Cálculo de tab por defecto en ReportPage ─────────────────────────────────

function computeDefaultTab(user: AuthUser | null): 'actual' | 'roi' {
  return user?.platformRole === 'client_viewer' ? 'roi' : 'actual'
}

describe('ReportPage defaultTab', () => {
  it('client_viewer arranca en tab ROI', () => {
    expect(computeDefaultTab(makeUser('client_viewer'))).toBe('roi')
  })

  it('platform_admin arranca en tab actual', () => {
    expect(computeDefaultTab(makeUser('platform_admin'))).toBe('actual')
  })

  it('client_editor arranca en tab actual', () => {
    expect(computeDefaultTab(makeUser('client_editor'))).toBe('actual')
  })

  it('member arranca en tab actual', () => {
    expect(computeDefaultTab(makeUser('member'))).toBe('actual')
  })
})
