/**
 * Copyright (c) 2025 SoftCorporation LLC. All rights reserved.
 *
 * The Software License, Version 1.0
 *
 * SoftCorporation LLC. grants you ("Licensee") a non-exclusive, royalty free,
 * license to use, modify and redistribute this software in source and binary
 * code form, provided that the following conditions are met:
 *
 * 1. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        SoftCorporation LLC. (http://www.softcorporation.com)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 2. This software and SoftCorporation name must not be used to
 *    promote products derived from the software without prior
 *    written permission. For written permission, please contact
 *    info@softcorporation.com.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * IN NO EVENT SHALL THE SOFTCORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
 *
 */
package com.softcorporation.sandi.base.service;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.test.util.ReflectionTestUtils;

import com.softcorporation.sandi.base.service.solr.SolrSynonymService;
import com.softcorporation.sandi.base.util.SynonymException;

/**
 * Demonstration test showing the working stemming functionality
 */
public class SynonymServiceStemmingDemoTest {

    @TempDir
    Path tempDir;
    
    private SolrSynonymService synonymService;
    
    @BeforeEach
    public void setUp() throws IOException {
        synonymService = new SolrSynonymService();
        
        ReflectionTestUtils.setField(synonymService, "sandiLanguages", "en,fr,de,ru,it,es");
        ReflectionTestUtils.setField(synonymService, "synonymsDirectory", tempDir.toString());
        ReflectionTestUtils.setField(synonymService, "stemmingEnabled", true);
        
        createTestSynonymFiles();
        
        synonymService.init();
    }
    
    private void createTestSynonymFiles() throws IOException {
        // English synonyms
        File englishFile = tempDir.resolve("english.txt").toFile();
        try (FileWriter writer = new FileWriter(englishFile)) {
            writer.write("running => run, jog, sprint\n");
            writer.write("cars => car, automobile, vehicle\n");
            writer.write("books => book, novel, publication\n");
            writer.write("playing => play, game, sport\n");
            writer.write("walking => walk, stroll, hike\n");
        }
        
        // French synonyms (using Porter stemmer)
        File frenchFile = tempDir.resolve("french.txt").toFile();
        try (FileWriter writer = new FileWriter(frenchFile)) {
            writer.write("livres => livre, ouvrage\n");
            writer.write("voitures => voiture, auto\n");
            writer.write("courrir => course, sprint\n");
        }
        
        // German synonyms (using Porter stemmer)
        File germanFile = tempDir.resolve("german.txt").toFile();
        try (FileWriter writer = new FileWriter(germanFile)) {
            writer.write("bücher => buch, werk\n");
            writer.write("autos => auto, wagen\n");
            writer.write("laufen => rennen, joggen\n");
        }
        
        // Russian synonyms (using Porter stemmer)
        File russianFile = tempDir.resolve("russian.txt").toFile();
        try (FileWriter writer = new FileWriter(russianFile)) {
            writer.write("книги => книга, произведение\n");
            writer.write("машины => машина, автомобиль\n");
            writer.write("бегать => бег, пробежка\n");
        }
        
        // Italian synonyms (using Porter stemmer)
        File italianFile = tempDir.resolve("italian.txt").toFile();
        try (FileWriter writer = new FileWriter(italianFile)) {
            writer.write("libri => libro, opera\n");
            writer.write("macchine => macchina, automobile\n");
            writer.write("correre => corsa, jogging\n");
        }
        
        // Spanish synonyms (using Porter stemmer)
        File spanishFile = tempDir.resolve("spanish.txt").toFile();
        try (FileWriter writer = new FileWriter(spanishFile)) {
            writer.write("libros => libro, obra\n");
            writer.write("coches => coche, automóvil\n");
            writer.write("correr => carrera, jogging\n");
        }
    }

    @Test
    public void demonstrateEnglishStemming() throws SynonymException {
        System.out.println("=== English Stemming Demonstration ===");
        
        // Test exact matching (original functionality)
        List<String> exactSynonyms = synonymService.getAllSynonyms("running", "english");
        System.out.println("Exact match for 'running': " + exactSynonyms);
        assertEquals(3, exactSynonyms.size());
        assertTrue(exactSynonyms.contains("run"));
        assertTrue(exactSynonyms.contains("jog"));
        assertTrue(exactSynonyms.contains("sprint"));

    }

}