import { describe, test, it, expect } from 'vitest'
import {RuntimeConfig} from "@nuxt/schema";
import {AnyAbility} from "@casl/ability/dist/types";
import {AccessProfile, organizationState} from "~/types/interfaces";
import EducationalMenuBuilder from "~/services/layout/menuBuilder/educationalMenuBuilder";
import {MenuGroup} from "~/types/layout";
import {MENU_LINK_TYPE} from "~/types/enum/layout";

let runtimeConfig: RuntimeConfig
let ability: AnyAbility
let organizationProfile: organizationState
let accessProfile: AccessProfile
let menuBuilder: EducationalMenuBuilder

beforeEach(()=> {
    runtimeConfig = vi.fn() as any as RuntimeConfig
    ability = vi.fn() as any as AnyAbility
    organizationProfile = vi.fn() as any as organizationState
    accessProfile = vi.fn() as any as AccessProfile

    runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'

    menuBuilder = new EducationalMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
})


describe('getMenuName', () => {
    test('validate name', () => {
        expect(menuBuilder.getMenuName()).toEqual("Educational")
    })
})


describe('build', () => {
    test('has all items', () => {
        ability.can = vi.fn(() => true)

        // Should return a MenuGroup
        const result = menuBuilder.build() as MenuGroup

        expect(result.label).toEqual('education_state')
        expect(result.icon).toEqual({name: 'fas fa-graduation-cap'})
        // @ts-ignore
        expect(result.children.length).toEqual(7)
    })

    test('has no items', () => {
        ability.can = vi.fn(() => false)
        expect(menuBuilder.build()).toEqual(null)
    })

    test('has only rights for menu criteria_notations', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'criteria_notations_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'criteria_notations', icon: {name: 'fas fa-bars'}, to: 'https://mydomain.com/criteria_notations/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu seizure_period', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'seizure_period_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'seizure_period', icon: {name: 'fas fa-calendar-alt'}, to: 'https://mydomain.com/education_teachers/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu test_seizure', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'test_seizure_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'test_seizure', icon: {name: 'fas fa-pencil-alt'}, to: 'https://mydomain.com/education_input/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu test_validation', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'test_validation_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'test_validation', icon: {name: 'fas fa-check'}, to: 'https://mydomain.com/education_notations/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu examen_results', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'examen_results_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'examen_results', icon: {name: 'fas fa-graduation-cap'}, to: 'https://mydomain.com/examen_convocations/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

    test('has only rights for menu education_by_student_validation', () => {
        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'education_by_student_validation_page')

        expect(menuBuilder.build()).toEqual(
            {label: 'education_by_student_validation', icon: {name: 'fas fa-check-square'}, to: 'https://mydomain.com/education_by_student/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })
})
