import {describe, expect, beforeEach} from '@jest/globals';
import {Settings} from '@/app/lib/settings';
import { getDb } from '@/app/lib/db';

describe('Settings', () => {
    let settings: Settings;

    beforeEach(async () => {
        // Initialize your Settings class here with a fresh database instance
        const db = await getDb();
        if (!db) throw new Error("Could not get db");
        settings = new Settings(db);
    });

    afterEach(async () => {
        // Clean up the database after each test
        settings && await settings.db.executeSql('DELETE FROM settings');
    });

    describe('setHostLanguage', () => {
        it('should set the host language in the database', async () => {
            const value = 'en';
            await settings.setHostLanguage(value);

            const result = await settings.getHostLanguage();
            expect(result).toEqual(value);
        });
    });

    describe('getHostLanguage', () => {
        it('should return the host language from the database', async () => {
            const value = 'fr';
            await settings.db.executeSql(
                `INSERT INTO settings (host_language) VALUES (?)`,
                [value]
            );

            const result = await settings.getHostLanguage();
            expect(result).toEqual(value);
        });

        it('should return null if the host language is not set', async () => {
            const result = await settings.getHostLanguage();
            expect(result).not.toBeNull();
        });
    });

    describe('setLibretranslateBaseUrl', () => {
        it('should set the LibreTranslate base URL in the database', async () => {
            const value = 'https://example.com';
            await settings.setLibetransalteBaseUrl(value);

            const result = await settings.getLibretranslateBaseUrl();
            expect(result).toEqual(value);
        });
    });

    describe('getLibretranslateBaseUrl', () => {
        it('should return the LibreTranslate base URL from the database', async () => {
            const value = 'https://another-example.com';
            await settings.db.executeSql(
                `INSERT INTO settings (libretranslate_base_url) VALUES (?)`,
                [value]
            );

            const result = await settings.getLibretranslateBaseUrl();
            expect(result).toEqual(value);
        });

        it('should return null if the LibreTranslate base URL is not set', async () => {
            const result = await settings.getLibretranslateBaseUrl();
            expect(result).not.toBeNull();
        });
    });
});