# seed_reading_questions.py

from django.core.management.base import BaseCommand
from toefl_prep_app.models import Question, Choice, CorrectAnswer
from django.core.files import File
import os

class Command(BaseCommand):
    help = 'Seed reading questions and choices data'

    def handle(self, *args, **kwargs):
        # Sample data for reading questions and choices
        reading_questions_data = [
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'Which of the following is the main topic of passage?',
                'choices': ['Assessment of the work of specialist concerned with ecology', 'A discussion of possible causes of extinction, and of ways to make predictions about environmental change', 'The changing aspects of our environment', 'A comparison of the extinction rates of different taxa'],
                'correct_choice_index': 1  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The word susceptibility in line 3 is closest in meaning to…',
                'choices': ['insensitivity', 'receptiveness', 'immunity', 'vulnerability'],
                'correct_choice_index': 3  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'An example of a taxon would be…',
                'choices': ['a phylum', 'the rate of environmental of change', 'a fossil', 'studies of extinction'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The Author compares mammals and birds to…',
                'choices': ['Mollusks and insects', 'Phylum and class', 'Dinosaurs and reptiles', 'Ecologists and biologists'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'It can be inferred from the passage that a significant event of the Crustaceous period was…',
                'choices': ['The appearance of many taxa', 'The dramatic, effect of the dinosaur on the environment', 'The extinction of birds', 'The extinction of dinosaurs'],
                'correct_choice_index': 3  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'It can be inferred from the passage that dinosaurs…',
                'choices': ['Included species that were mammals', 'Were better represented in the fossil record than other species', 'Possessed disadvantageous traits', 'Were not susceptible to extinction'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The word dramatically in line 10 means…',
                'choices': ['strongly', 'inspiringly', 'flimsily', 'visually'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The word fields in line 12 is closest in meaning to…',
                'choices': ['areas', 'meadows', 'studies', 'careers'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'From the passage it can be inferred the disadvantageous traits are…',
                'choices': ['occurring at different rates', 'a contributing cause of extinction', 'adaptable', 'learned by mathematical formulas'],
                'correct_choice_index': 1  # Index of correct choice in choices list
            },
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The expression singled out in lines 16 is closest is meaning to…',
                'choices': ['isolated', 'blamed', 'seen', 'divided'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'According to the passage, the likelihood of a population becoming extinct can be…',
                'choices': ['Lessened by the efforts of a few concerned specialists', 'Unaffected by environmental change', 'Determined by mathematical formulas', 'Almost impossible to ascertain'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage1.png',  # Path to passage image file
                'question_text': 'The word broader in line 23 is closest in meaning to…',
                'choices': ['fatter', 'extra', 'wider', 'many'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'In which of the following publications would this passage the most likely to appear?',
                'choices': ['An anthology of English literature', 'An introductory American history book', 'A book about Eastern religions', 'A basic math textbook'],
                'correct_choice_index': 1  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'The word their in line 4 refers to…',
                'choices': ['trappings', 'preachers', 'Religious services', 'The Friends'],
                'correct_choice_index': 3  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'The word persecuted in lines 8 is closest in meaning to…',
                'choices': ['scrutinized', 'Lauded', 'harassed', 'believed'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'Where in the passage does the author give an example of Quaker involvement in social issues…',
                'choices': ['Lines 4-5', 'Lines 11-12', 'Lines 13-14', 'Lines 16-17'],
                'correct_choice_index': 3  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'Which of the following would be an example of “the outward trappings associated with Christianity” referred to in lines 6?',
                'choices': ['clergy', 'silent, leaderless worship', 'concern for social reform', 'the doctrine of “God in every man”'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'It can be inferred from the passage that early Quakers experienced official persecution because…',
                'choices': ['They were known for “quaking” during religious services', 'They helped found the state of Pennsylvania', 'They came to America from England', 'Their religious beliefs were considered subversive '],
                'correct_choice_index': 3  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'Why does the author mention execution in lines 9?',
                'choices': ['It is an example of the persecution Quakers faced', 'It is an outward trapping of Christianity', 'This serves as an example of William Penn’s policies', 'Many religions were concerned with this issue'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },            
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'The paragraph after this passage would most likely be about…',
                'choices': ['The decline of the Quaker population since World War I', 'The similarities and differences between Quakers and Puritans', 'Ways in which Quakers today show concern for others', 'Social reforms enacted by Quakers during the American Revolution'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },                        
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'From the passage, it can be inferred that the Puritans were…',
                'choices': ['friends', 'persecuted', 'intolerant', 'Executed'],
                'correct_choice_index': 2  # Index of correct choice in choices list
            },                        
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'The word penchant in line 10 most nearly means…',
                'choices': ['Appreciation', 'Propensity', 'Disinclination', 'Proposition'],
                'correct_choice_index': 1  # Index of correct choice in choices list
            },                        
            {
                'passage_image_path': 'audio/passage2.png',  # Path to passage image file
                'question_text': 'Why did the American Quaker population surge after William Penn founded Pennsylvania?',
                'choices': ['Pennsylvania tolerated the Quaker religion', 'Pennsylvania forbade religions other than Quakerism', 'The Pennsylvania government tried to help escaped slaves', 'Social reform was important to most Americans'],
                'correct_choice_index': 0  # Index of correct choice in choices list
            },                        
            # Add more reading question data here
        ]

        for question_data in reading_questions_data:
            # Create Question instance with passage image
            passage_image_path = question_data['passage_image_path']
            if os.path.exists(passage_image_path):
                with open(passage_image_path, 'rb') as f:
                    passage_image = File(f)
                    question = Question.objects.create(question_text=question_data['question_text'], passage=passage_image)
            else:
                self.stdout.write(self.style.ERROR(f'Passage image not found at {passage_image_path}. Skipping question.'))

            # Create choices for the reading question
            for choice_text in question_data['choices']:
                choice = Choice.objects.create(question=question, choice_text=choice_text)

            # Set the correct answer for the reading question
            correct_choice_index = question_data['correct_choice_index']
            correct_choice = Choice.objects.filter(question=question)[correct_choice_index]

            # Create CorrectAnswer instance and associate it with the reading question
            correct_answer = CorrectAnswer.objects.create(question=question, correct_choice=correct_choice)

        self.stdout.write(self.style.SUCCESS('Reading questions and choices seeded successfully.'))