/**
 * roleNormalizer.spec.js
 *
 * Função pura — zero mock. Garante contrato único entre guards.js e
 * tenantStore.js (evita regressão do V#4 resolvido).
 */
import { describe, it, expect } from 'vitest';
import { normalizeRole } from '../roleNormalizer.js';

describe('normalizeRole — tenant_admin/admin + kind', () => {
    it('tenant_admin + kind=therapist → therapist', () => {
        expect(normalizeRole('tenant_admin', 'therapist')).toBe('therapist');
    });

    it('tenant_admin + kind=saas → therapist (saas é alias de therapist)', () => {
        expect(normalizeRole('tenant_admin', 'saas')).toBe('therapist');
    });

    it('tenant_admin + kind=supervisor → supervisor', () => {
        expect(normalizeRole('tenant_admin', 'supervisor')).toBe('supervisor');
    });

    it('tenant_admin + kind=clinic → clinic_admin (fallback)', () => {
        expect(normalizeRole('tenant_admin', 'clinic')).toBe('clinic_admin');
    });

    it('tenant_admin sem kind → clinic_admin (legado)', () => {
        expect(normalizeRole('tenant_admin', null)).toBe('clinic_admin');
        expect(normalizeRole('tenant_admin', '')).toBe('clinic_admin');
        expect(normalizeRole('tenant_admin')).toBe('clinic_admin');
    });

    it('admin (alias legado) segue mesmas regras de tenant_admin', () => {
        expect(normalizeRole('admin', 'therapist')).toBe('therapist');
        expect(normalizeRole('admin', 'supervisor')).toBe('supervisor');
        expect(normalizeRole('admin', null)).toBe('clinic_admin');
    });
});

describe('normalizeRole — pass-through', () => {
    it('clinic_admin canônico → clinic_admin', () => {
        expect(normalizeRole('clinic_admin')).toBe('clinic_admin');
        expect(normalizeRole('clinic_admin', 'qualquer')).toBe('clinic_admin');
    });

    it('therapist explícito → therapist', () => {
        expect(normalizeRole('therapist')).toBe('therapist');
    });

    it('supervisor explícito → supervisor', () => {
        expect(normalizeRole('supervisor')).toBe('supervisor');
    });

    it('roles não mapeados retornam intactos', () => {
        expect(normalizeRole('saas_admin')).toBe('saas_admin');
        expect(normalizeRole('portal_user')).toBe('portal_user');
        expect(normalizeRole('editor')).toBe('editor');
        expect(normalizeRole('patient')).toBe('patient');
    });
});

describe('normalizeRole — entradas inválidas', () => {
    it('null/undefined retornam string vazia', () => {
        expect(normalizeRole(null)).toBe('');
        expect(normalizeRole(undefined)).toBe('');
        expect(normalizeRole()).toBe('');
    });

    it('string vazia retorna vazia', () => {
        expect(normalizeRole('')).toBe('');
        expect(normalizeRole('   ')).toBe('');
    });

    it('trim é aplicado', () => {
        expect(normalizeRole('  therapist  ')).toBe('therapist');
        expect(normalizeRole(' tenant_admin ', ' therapist ')).toBe('therapist');
    });

    it('números são coercidos para string', () => {
        // Não deveria acontecer em produção, mas não quebra
        expect(normalizeRole(123)).toBe('123');
    });
});
