package com.example.boidelov3;

import org.junit.Test;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Tests unitaires pour la classe Questions.
 * Couvre les getters/setters et la gestion de la liste de questions.
 */
public class QuestionsClassTest {

    @Test
    public void testDefaultConstructor_createsEmptyQuestions() {
        Questions questions = new Questions();

        assertNull("Version should be null by default", questions.getVersion());
        assertNull("Questions list should be null by default", questions.getQuestions());
    }

    @Test
    public void testSetVersion_getVersion_returnsCorrectValue() {
        Questions questions = new Questions();
        questions.setVersion("1.0");

        assertEquals("Version should be 1.0", "1.0", questions.getVersion());
    }

    @Test
    public void testSetQuestions_getQuestions_returnsCorrectValue() {
        Questions questions = new Questions();
        List<Question> questionList = Arrays.asList(
            createQuestion(1, "Question 1"),
            createQuestion(2, "Question 2"),
            createQuestion(3, "Question 3")
        );
        questions.setQuestions(questionList);

        assertNotNull("Questions list should not be null", questions.getQuestions());
        assertEquals("Questions list size should be 3", 3, questions.getQuestions().size());
        assertEquals("First question ID should be 1", 1, questions.getQuestions().get(0).getId());
    }

    @Test
    public void testSetQuestions_withEmptyList() {
        Questions questions = new Questions();
        List<Question> emptyList = Arrays.asList();
        questions.setQuestions(emptyList);

        assertNotNull("Questions list should not be null", questions.getQuestions());
        assertTrue("Questions list should be empty", questions.getQuestions().isEmpty());
    }

    @Test
    public void testSetQuestions_withNull() {
        Questions questions = new Questions();
        questions.setQuestions(null);

        assertNull("Questions list should be null", questions.getQuestions());
    }

    @Test
    public void testCompleteQuestions_withAllFields() {
        Questions questions = new Questions();
        questions.setVersion("2.5");

        List<Question> questionList = Arrays.asList(
            createQuestion(1, "Première question"),
            createQuestion(2, "Deuxième question")
        );
        questions.setQuestions(questionList);

        assertEquals("Version should be 2.5", "2.5", questions.getVersion());
        assertEquals("Questions list size should be 2", 2, questions.getQuestions().size());
        assertEquals("First question text should match", "Première question", questions.getQuestions().get(0).getQuestion());
    }

    @Test
    public void testSetQuestions_multipleCalls() {
        Questions questions = new Questions();

        // First set
        List<Question> firstList = Arrays.asList(createQuestion(1, "Q1"));
        questions.setQuestions(firstList);
        assertEquals("First list size should be 1", 1, questions.getQuestions().size());

        // Second set (replace)
        List<Question> secondList = Arrays.asList(
            createQuestion(1, "Q1"),
            createQuestion(2, "Q2"),
            createQuestion(3, "Q3")
        );
        questions.setQuestions(secondList);
        assertEquals("Second list size should be 3", 3, questions.getQuestions().size());
    }

    @Test
    public void testVersion_withEmptyString() {
        Questions questions = new Questions();
        questions.setVersion("");

        assertEquals("Version should be empty string", "", questions.getVersion());
    }

    @Test
    public void testVersion_withSpecialCharacters() {
        Questions questions = new Questions();
        String version = "v1.2.3-beta";
        questions.setVersion(version);

        assertEquals("Version should handle special characters", version, questions.getVersion());
    }

    @Test
    public void testQuestionsList_isModifiable() {
        Questions questions = new Questions();
        List<Question> originalList = new ArrayList<>();
        originalList.add(createQuestion(1, "Q1"));
        questions.setQuestions(originalList);

        // Modify the returned list
        questions.getQuestions().add(createQuestion(2, "Q2"));

        // The modification should affect the Questions object
        assertEquals("List should be modifiable", 2, questions.getQuestions().size());
    }

    @Test
    public void testQuestions_withLargeList() {
        Questions questions = new Questions();
        List<Question> largeList = Arrays.asList(
            createQuestion(1, "Q1"),
            createQuestion(2, "Q2"),
            createQuestion(3, "Q3"),
            createQuestion(4, "Q4"),
            createQuestion(5, "Q5"),
            createQuestion(6, "Q6"),
            createQuestion(7, "Q7"),
            createQuestion(8, "Q8"),
            createQuestion(9, "Q9"),
            createQuestion(10, "Q10")
        );
        questions.setQuestions(largeList);

        assertEquals("Should handle large lists", 10, questions.getQuestions().size());
    }

    // Helper method

    private Question createQuestion(int id, String text) {
        Question q = new Question();
        q.setId(id);
        q.setQuestion(text);
        return q;
    }
}
