// #region MaintenanceApiTests [C:2] [TYPE Module] [SEMANTICS test, api, maintenance, url, contract]
// @BRIEF Verify that API client functions construct correct URLs (no double `/api/` etc.)
import { describe, it, expect, vi, beforeEach } from 'vitest';

const { mockRequestApi } = vi.hoisted(() => ({ mockRequestApi: vi.fn() }));
vi.mock('$lib/api.js', () => ({ requestApi: mockRequestApi }));

import { startMaintenance, endMaintenance, endAllMaintenance, getSettings, updateSettings, listEvents, listDashboardBanners } from '$lib/api/maintenance.js';

describe('MaintenanceApi URL construction', () => {
    beforeEach(() => vi.clearAllMocks());

    it('startMaintenance → POST /maintenance/start', async () => {
        mockRequestApi.mockResolvedValue({ task_id: 't-1', maintenance_id: 'm-1', status: 'pending' });
        await startMaintenance({ tables: ['raw.sales'], start_time: '2026-05-21T22:00:00Z', end_time: '2026-05-22T02:00:00Z' });
        expect(mockRequestApi).toHaveBeenCalledWith('/maintenance/start', 'POST', expect.any(Object));
    });

    it('endMaintenance → POST /maintenance/{id}/end', async () => {
        mockRequestApi.mockResolvedValue({ task_id: 't-1', status: 'pending' });
        await endMaintenance('ev-abc');
        expect(mockRequestApi).toHaveBeenCalledWith('/maintenance/ev-abc/end', 'POST');
    });

    it('endAllMaintenance → POST /maintenance/end-all', async () => {
        mockRequestApi.mockResolvedValue({ task_id: 't-1', status: 'pending' });
        await endAllMaintenance();
        expect(mockRequestApi).toHaveBeenCalledWith('/maintenance/end-all', 'POST');
    });

    it('listEvents → GET /maintenance/events', async () => {
        mockRequestApi.mockResolvedValue({ active: [], completed: [] });
        await listEvents();
        expect(mockRequestApi).toHaveBeenCalledWith('/maintenance/events', 'GET');
    });

    it('listDashboardBanners → GET /maintenance/dashboard-banners', async () => {
        mockRequestApi.mockResolvedValue([]);
        await listDashboardBanners();
        expect(mockRequestApi).toHaveBeenCalledWith('/maintenance/dashboard-banners', 'GET');
    });
});
