/*
 * @Copyright 2018-2026 HardBackNutter
 * @License GNU General Public License
 *
 * This file is part of NeverTooManyBooks.
 *
 * NeverTooManyBooks is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NeverTooManyBooks is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NeverTooManyBooks. If not, see <http://www.gnu.org/licenses/>.
 */

package com.hardbacknutter.nevertoomanybooks.booklist.style;

import java.util.Locale;
import java.util.Set;

import com.hardbacknutter.nevertoomanybooks.BaseDBTest;
import com.hardbacknutter.nevertoomanybooks.booklist.DBExpr;
import com.hardbacknutter.nevertoomanybooks.core.database.DaoWriteException;
import com.hardbacknutter.nevertoomanybooks.core.database.Sort;
import com.hardbacknutter.nevertoomanybooks.core.storage.StorageException;
import com.hardbacknutter.nevertoomanybooks.database.dao.BookshelfDao;
import com.hardbacknutter.nevertoomanybooks.entities.Bookshelf;
import com.hardbacknutter.nevertoomanybooks.utils.AppLocale;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DomainExpressionsTest
        extends BaseDBTest {

    private BookshelfDao bookshelfDao;
    private Bookshelf bookshelf;
    private UserStyle style;

    @BeforeEach
    void setup()
            throws DaoWriteException, StorageException {
        super.setup(AppLocale.SYSTEM_LANGUAGE);

        style = (UserStyle) getBuiltinStyle().clone(context);
        style.setName("test");
        serviceLocator.getStyles().insertOrUpdate(context, style);

        bookshelfDao = serviceLocator.getBookshelfDao();
        bookshelf = bookshelfDao.getDefault();
        bookshelf.setStyle(context, style);
        bookshelfDao.update(context, bookshelf, Locale.UK);
    }

    @AfterEach
    void breakdown()
            throws DaoWriteException {
        bookshelf.setStyle(context, getBuiltinStyle());
        bookshelfDao.update(context, bookshelf, Locale.UK);
        serviceLocator.getStyles().delete(style);
    }

    /**
     * The Style.Screen.List fields supporting visibility
     * must all have DomainExpressions.
     */
    @Test
    void visibilityKeysHaveDomainExpressions() {
        // force all fields
        style.setFieldVisibility(FieldVisibility.Screen.List, Long.MAX_VALUE);

        final Set<String> keys = style.getFieldVisibilityKeys(FieldVisibility.Screen.List, true);
        assertFalse(keys.isEmpty());

        final long expressionCount = keys
                .stream()
                .map(key -> DBExpr.forBookLevelField(key, Sort.Unsorted, style))
                .filter(list -> !list.isEmpty())
                .count();

        // some keys generate two expressions, don't bother checking
        // exact number as we're testing on NOT throwing an exception for key not found
        assertTrue(expressionCount > 0);
    }

    /**
     * The Style.Screen.List fields supporting sorting
     * must all have DomainExpressions.
     */
    @Test
    void sortableBookLevelKeysHaveDomainExpressions() {
        // all supported fields will be there.
        final Set<String> keys = style.getBookLevelFieldsOrderBy().keySet();
        assertFalse(keys.isEmpty());

        final long expressionCount = keys
                .stream()
                .map(key -> DBExpr.forBookLevelField(key, Sort.Unsorted, style))
                .filter(list -> !list.isEmpty())
                .count();

        // some keys generate two expressions, don't bother checking
        // exact number as we're testing on NOT throwing an exception for key not found
        assertTrue(expressionCount > 0);
    }
}