const mockStorageData = new Map<string, any>();
jest.mock('../storage', () => ({
    __esModule: true,
    default: () => ({
        hasData: async (key: string) => mockStorageData.has(key),
        getData: async <T>(key: string) => mockStorageData.get(key) as T,
        setData: async <T>(key: string, val: T) => void mockStorageData.set(key, val),
    }),
    storageReady: Promise.resolve(),
}));

import { getDateForWeekIndex, getMenuKey, getEmptyData } from '../service';
import { formatDate } from '../utils';

// Pin "today" to 2025-01-10 (Friday, week 2) for deterministic tests
// Monday of that week = 2025-01-06, ..., Friday = 2025-01-10

describe('getDateForWeekIndex', () => {
    beforeAll(() => {
        jest.useFakeTimers();
        jest.setSystemTime(new Date('2025-01-10'));
    });

    afterAll(() => {
        jest.useRealTimers();
    });
    test('index 0 (pondělí) vrátí 2025-01-06', () => {
        expect(formatDate(getDateForWeekIndex(0))).toBe('2025-01-06');
    });

    test('index 4 (pátek) vrátí 2025-01-10', () => {
        expect(formatDate(getDateForWeekIndex(4))).toBe('2025-01-10');
    });

    test('index 2 (středa) vrátí 2025-01-08', () => {
        expect(formatDate(getDateForWeekIndex(2))).toBe('2025-01-08');
    });

    test('neplatný index (-1) vrátí dnešek bez vyhození chyby', () => {
        const result = getDateForWeekIndex(-1);
        expect(result).toBeInstanceOf(Date);
    });

    test('neplatný index (5) vrátí dnešek bez vyhození chyby', () => {
        const result = getDateForWeekIndex(5);
        expect(result).toBeInstanceOf(Date);
    });
});

describe('getMenuKey', () => {
    test('vrátí klíč ve tvaru menu_RRRR_TT', () => {
        const date = new Date('2025-01-10');
        const key = getMenuKey(date);
        expect(key).toMatch(/^menu_\d{4}_\d+$/);
    });

    test('dvě data ve stejném týdnu mají stejný klíč', () => {
        expect(getMenuKey(new Date('2025-01-06'))).toBe(getMenuKey(new Date('2025-01-10')));
    });

    test('dvě data z různých týdnů mají různé klíče', () => {
        expect(getMenuKey(new Date('2025-01-06'))).not.toBe(getMenuKey(new Date('2025-01-13')));
    });
});

describe('getEmptyData', () => {
    test('vrátí strukturu s prázdnými choices', () => {
        const data = getEmptyData(new Date('2025-01-10'));
        expect(data.choices).toEqual({});
    });

    test('vrátí dayIndex=4 pro pátek', () => {
        const data = getEmptyData(new Date('2025-01-10'));
        expect(data.dayIndex).toBe(4);
    });

    test('isWeekend=false pro pracovní den', () => {
        const data = getEmptyData(new Date('2025-01-10'));
        expect(data.isWeekend).toBe(false);
    });

    test('isWeekend=true pro víkend', () => {
        const data = getEmptyData(new Date('2025-01-11'));
        expect(data.isWeekend).toBe(true);
    });
});
