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

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

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/'
    accessProfile.id = 123

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


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


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

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

        expect(result.label).toEqual('my_account')

        // @ts-ignore
        expect(result.children.length).toEqual(15)
        // @ts-ignore
        expect(result.actions.length).toEqual(1)


        // Has the logout action
        // @ts-ignore
        expect(result.actions).toEqual([
            {label: 'logout', icon: undefined, to: 'https://mydomain.com/logout', type: MENU_LINK_TYPE.V1, active: false}
        ])
    })

    test('has profile icon : mister)', () => {
        ability.can = vi.fn(() => false)
        accessProfile.avatarId = 100
        accessProfile.gender = GENDER.MISTER

        const result = menuBuilder.build() as MenuGroup

        expect(result.icon).toEqual({avatarId: 100, avatarByDefault: '/images/default/men-1.png'})
    })

    test('has profile icon : miss)', () => {
        ability.can = vi.fn(() => false)
        accessProfile.avatarId = 100
        accessProfile.gender = GENDER.MISS

        const result = menuBuilder.build() as MenuGroup

        expect(result.icon).toEqual({avatarId: 100, avatarByDefault: '/images/default/women-1.png'})
    })

    test('has no items', () => {
        ability.can = vi.fn(() => false)
        const group = menuBuilder.build()
        // AccountMenuBuilder retourne toujours un groupe
        // @ts-ignore
        expect(group.children).toEqual([])

        // Still has the logout action
        // @ts-ignore
        expect(group.actions).toEqual([
            {label: 'logout', icon: undefined, to: 'https://mydomain.com/logout', type: MENU_LINK_TYPE.V1, active: false}
        ])
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_schedule_page', icon: undefined, to: 'https://mydomain.com/my_calendar', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'attendance_bookings_menu', icon: undefined, to: 'https://mydomain.com/own_attendance', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_attendance', icon: undefined, to: 'https://mydomain.com/my_attendances/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_invitation', icon: undefined, to: 'https://mydomain.com/my_invitations/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_students', icon: undefined, to: 'https://mydomain.com/my_students/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_students_education_students', icon: undefined, to: 'https://mydomain.com/my_students_education_students/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_education_students', icon: undefined, to: 'https://mydomain.com/main/my_profile/123/dashboard/my_education_students/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'send_an_email', icon: undefined, to: 'https://mydomain.com/list/create/emails', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_documents', icon: undefined, to: 'https://mydomain.com/main/my_profile/123/dashboard/show/my_access_file', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_profile', icon: undefined, to: 'https://mydomain.com/main/my_profile/123/dashboard', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'adherent_list', icon: undefined, to: 'https://mydomain.com/adherent_contacts/list/', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'my_bills', icon: undefined, to: 'https://mydomain.com/main/my_profile/123/dashboard/show/my_bills', type: MENU_LINK_TYPE.V1, active: false}
        )
    })

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

        // @ts-ignore
        expect(menuBuilder.build().children[0]).toEqual(
            {label: 'print_my_licence', icon: undefined, to: 'https://mydomain.com/licence_cmf/user', type: MENU_LINK_TYPE.V1, active: false}
        )
    })
})
