import { describe, it, expect, vi, beforeEach } from 'vitest';

// Aether test/demo base URL: 'http://demo.localhost:5173'
// Aether development API: 'https://dev-api.oneskyit.com'
// Aether test/demo account: '_XY7DXtc9MY' (1) "One Sky IT Demo"
// Aether test/demo event: 'pjrcghqwert' (1) "Demo One Sky IT Conference"

// Mock the api module used by the ae_events file
const mockCreateNested = vi.fn();
vi.mock('$lib/ae_api/api', () => ({ api: { create_nested_obj: mockCreateNested } }));

// Avoid touching IndexedDB caching in this unit test
vi.mock('$lib/ae_core/core__idb_dexie', () => ({ db_save_ae_obj_li__ae_obj: vi.fn() }));

// Import the function under test after mocks
import { create_ae_obj__event_badge } from '$lib/ae_events/ae_events__event_badge';

describe('create_ae_obj__event_badge', () => {
    beforeEach(() => {
        mockCreateNested.mockReset();
    });

    it('calls api.create_nested_obj with the correct params and returns the result', async () => {
        const fakeResult = { event_badge_id: 'eb123', full_name: 'Test User' };
        mockCreateNested.mockResolvedValue(fakeResult);

        const api_cfg = { base_url: 'http://localhost', headers: {} };
        const event_id = 'evt1';
        const data_kv = { full_name_override: 'Test User', email: 't@example.com' };

        const result = await create_ae_obj__event_badge({ api_cfg, event_id, data_kv, try_cache: false });

        expect(mockCreateNested).toHaveBeenCalled();
        const calledWith = mockCreateNested.mock.calls[0][0];
        expect(calledWith.parent_type).toBe('event');
        expect(calledWith.parent_id).toBe(event_id);
        expect(calledWith.child_type).toBe('event_badge');
        expect(calledWith.fields).toEqual(data_kv);

        expect(result).toEqual(fakeResult);
    });
});
