package com.rugvedsarika.easymath.utils

import com.rugvedsarika.easymath.model.Question
import kotlin.random.Random

object MathGenerator {

    fun generateQuestion(category: String, level: String): Question {
        if (category == "Mixed" || category == "Exam") {
            val categories = listOf("Addition", "Subtraction", "Multiplication", "Division")
            return generateQuestion(categories.random(), level)
        }

        return when (category) {
            "Addition" -> generateAddition(level)
            "Subtraction" -> generateSubtraction(level)
            "Multiplication" -> generateMultiplication(level)
            "Division" -> generateDivision(level)
            "Geometry" -> generateGeometry(level)
            "Fractions" -> generateFractions(level)
            else -> generateAddition("Easy") // Default
        }
    }

    private fun generateAddition(level: String): Question {
        val (min, max) = getRange(level)
        val a = Random.nextInt(min, max)
        val b = Random.nextInt(min, max)
        return Question("$a + $b = ?", a + b)
    }

    private fun generateSubtraction(level: String): Question {
        val (min, max) = getRange(level)
        val a = Random.nextInt(min, max)
        val b = Random.nextInt(min, max)
        val maxVal = maxOf(a, b)
        val minVal = minOf(a, b)
        return Question("$maxVal - $minVal = ?", maxVal - minVal)
    }

    private fun generateMultiplication(level: String): Question {
        val (min, max) = when (level) {
            "Easy" -> 1 to 10
            "Medium" -> 2 to 15
            "Hard" -> 5 to 20
            else -> 1 to 10
        }
        val a = Random.nextInt(min, max)
        val b = Random.nextInt(min, max)
        return Question("$a × $b = ?", a * b)
    }

    private fun generateDivision(level: String): Question {
        val (min, max) = when (level) {
            "Easy" -> 1 to 10
            "Medium" -> 2 to 15
            "Hard" -> 5 to 20
            else -> 1 to 10
        }
        val divisor = Random.nextInt(min, max)
        val quotient = Random.nextInt(min, max)
        val dividend = divisor * quotient
        return Question("$dividend ÷ $divisor = ?", quotient)
    }

    private fun generateGeometry(level: String): Question {
        val (min, max) = when (level) {
            "Easy" -> 1 to 10
            "Medium" -> 5 to 15
            else -> 10 to 20
        }
        val side = Random.nextInt(min, max)
        // 50% chance for Area of Square, 50% for Perimeter
        return if (Random.nextBoolean()) {
            Question("Area of square side $side?", side * side)
        } else {
            Question("Perimeter of square side $side?", 4 * side)
        }
    }

    private fun generateFractions(level: String): Question {
        // Simple fractions: 1/2 + 1/2 = 1. Or find numerator?
        // Let's do simple addition with same denominator for now.
        val denominator = Random.nextInt(2, 10)
        val num1 = Random.nextInt(1, denominator)
        val num2 = Random.nextInt(1, denominator)
        
        // Question: num1/den + num2/den = ?/den. User enters numerator sum?
        // Or simpler: "1/2 of 10?". Let's do "1/N of X" where X is multiple of N.
        
        val div = Random.nextInt(2, 10) // Denominator
        val ans = Random.nextInt(1, 10) // Answer
        val total = div * ans
        
        return Question("1/$div of $total?", ans)
    }

    private fun getRange(level: String): Pair<Int, Int> {
        return when (level) {
            "Easy" -> 1 to 10       
            "Medium" -> 10 to 50    
            "Hard" -> 50 to 100     
            else -> 1 to 10
        }
    }
}
