from django.db import models


class QuestionType(models.TextChoices):
    MCQ_SINGLE = "mcq_single", "MCQ (Single Correct)"
    MCQ_MULTI = "mcq_multi", "MCQ (Multiple Correct)"
    TRUE_FALSE = "true_false", "True / False"
    FILL_BLANK = "fill_blank", "Fill in the Blank"
    SHORT_ANSWER = "short_answer", "Short Answer"
    LONG_ANSWER = "long_answer", "Long / Descriptive Answer"
    MATCH_FOLLOWING = "match_following", "Match the Following"
    ASSERTION_REASON = "assertion_reason", "Assertion & Reason"


class Difficulty(models.TextChoices):
    EASY = "easy", "Easy"
    MEDIUM = "medium", "Medium"
    HARD = "hard", "Hard"


class Topic(models.Model):
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name


class QuestionBatch(models.Model):
    STATUS_PENDING, STATUS_SUCCESS, STATUS_FAILED = "pending", "success", "failed"
    STATUS_CHOICES = [(STATUS_PENDING, "Pending"), (STATUS_SUCCESS, "Success"), (STATUS_FAILED, "Failed")]

    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name="batches")
    question_type = models.CharField(max_length=32, choices=QuestionType.choices)
    difficulty = models.CharField(max_length=16, choices=Difficulty.choices, default=Difficulty.MEDIUM)
    requested_count = models.PositiveIntegerField(default=5)
    status = models.CharField(max_length=16, choices=STATUS_CHOICES, default=STATUS_PENDING)
    error_message = models.TextField(blank=True)
    raw_response = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)


class Question(models.Model):
    batch = models.ForeignKey(QuestionBatch, on_delete=models.CASCADE, related_name="questions")
    question_type = models.CharField(max_length=32, choices=QuestionType.choices)
    question_text = models.TextField()
    options = models.JSONField(blank=True, null=True)
    correct_answer = models.JSONField(blank=True, null=True)
    explanation = models.TextField(blank=True)
    difficulty = models.CharField(max_length=16, choices=Difficulty.choices, default=Difficulty.MEDIUM)
    created_at = models.DateTimeField(auto_now_add=True)