@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun ExpensesGraph(paddingValues: PaddingValues) {
    val db = FirebaseFirestore.getInstance()

    // Define your timeframes
    val timeFrames = listOf("Daily", "Weekly", "Monthly", "Yearly")
    val selectedIndex = remember { mutableStateOf(0) }

    // Fetch and process the expenses data
    val chartEntries = remember { mutableStateOf(entriesOf(*arrayOf<Float>())) }
    var chartModelProducer = ChartEntryModelProducer(chartEntries.value)

    var xAxisLabels = remember {
        mutableStateOf(listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
    }

    var bottomAxisValueFormatter =
        AxisValueFormatter<AxisPosition.Horizontal.Bottom> { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }

    val expensesMap = mutableMapOf<String, Float>().apply {
        xAxisLabels.value.forEach { put(it, 0f) }
    }

    fun fetchExpensesData(timeFrame: String) {
        val startCalendar = Calendar.getInstance()
        val endCalendar = Calendar.getInstance()
        var dateFormat = SimpleDateFormat("EEEE_dd_MMM_yyyy", Locale.getDefault())

        when (timeFrame) {
            "Daily" -> {
                // Daily: Showing 7 days in a week
                startCalendar.set(Calendar.DAY_OF_WEEK, startCalendar.firstDayOfWeek + 1)
                startCalendar.set(Calendar.HOUR_OF_DAY, 0)
                startCalendar.set(Calendar.MINUTE, 0)
                startCalendar.set(Calendar.SECOND, 0)
                startCalendar.set(Calendar.MILLISECOND, 0)
                endCalendar.set(Calendar.DAY_OF_WEEK, startCalendar.firstDayOfWeek)
                endCalendar.add(Calendar.DATE, 6)
                endCalendar.set(Calendar.HOUR_OF_DAY, 0)
                endCalendar.set(Calendar.MINUTE, 0)
                endCalendar.set(Calendar.SECOND, 0)
                endCalendar.set(Calendar.MILLISECOND, 0)
                dateFormat = SimpleDateFormat("EEEE_dd_MMM_yyyy", Locale.getDefault())

                val c = Calendar.getInstance()
                c.time = startCalendar.time
                while (c.timeInMillis < endCalendar.timeInMillis) {
                    val date = dateFormat.format(c.time)
                    expensesMap[date] = 0f
                    c.add(Calendar.DATE, 1)
                }

                xAxisLabels.value = listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
                bottomAxisValueFormatter =
                    AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }
            }

            "Weekly" -> {
                // Weekly: Showing 4 weeks in a month
                startCalendar.set(Calendar.DAY_OF_MONTH, 1)
                startCalendar.set(Calendar.HOUR_OF_DAY, 0)
                startCalendar.set(Calendar.MINUTE, 0)
                startCalendar.set(Calendar.SECOND, 0)
                startCalendar.set(Calendar.MILLISECOND, 0)
                endCalendar.set(
                    Calendar.DAY_OF_MONTH,
                    startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH),
                )
                endCalendar.set(Calendar.HOUR_OF_DAY, 0)
                endCalendar.set(Calendar.MINUTE, 0)
                endCalendar.set(Calendar.SECOND, 0)
                endCalendar.set(Calendar.MILLISECOND, 0)
                dateFormat = SimpleDateFormat("W_MMM_yyyy", Locale.getDefault())

                xAxisLabels.value = (1..4).map { "Week $it" }
                println(xAxisLabels.value)
                bottomAxisValueFormatter =
                    AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }
            }

            "Monthly" -> {
                // Monthly: Showing 12 months in a year
                startCalendar.set(Calendar.MONTH, 0)
                startCalendar.set(Calendar.DAY_OF_MONTH, 1)
                startCalendar.set(Calendar.HOUR_OF_DAY, 0)
                startCalendar.set(Calendar.MINUTE, 0)
                startCalendar.set(Calendar.SECOND, 0)
                startCalendar.set(Calendar.MILLISECOND, 0)
                endCalendar.set(Calendar.MONTH, 11)
                endCalendar.set(
                    Calendar.DAY_OF_MONTH,
                    endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)
                )
                endCalendar.set(Calendar.HOUR_OF_DAY, 0)
                endCalendar.set(Calendar.MINUTE, 0)
                endCalendar.set(Calendar.SECOND, 0)
                endCalendar.set(Calendar.MILLISECOND, 0)
                dateFormat = SimpleDateFormat("MMM_yyyy", Locale.getDefault())

                xAxisLabels.value = listOf(
                    "Jan",
                    "Feb",
                    "Mar",
                    "Apr",
                    "May",
                    "June",
                    "July",
                    "Aug",
                    "Sep",
                    "Oct",
                    "Nov",
                    "Dec"
                )
                bottomAxisValueFormatter =
                    AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }
            }

            "Yearly" -> {
                startCalendar.set(Calendar.MONTH, 0)
                startCalendar.set(Calendar.DAY_OF_MONTH, -1)
                startCalendar.set(Calendar.HOUR_OF_DAY, 0)
                startCalendar.set(Calendar.MINUTE, 0)
                startCalendar.set(Calendar.SECOND, 0)
                startCalendar.set(Calendar.MILLISECOND, 0)
                endCalendar.set(Calendar.MONTH, 11)
                endCalendar.set(Calendar.DAY_OF_MONTH, 31)
                endCalendar.set(Calendar.HOUR_OF_DAY, 0)
                endCalendar.set(Calendar.MINUTE, 0)
                endCalendar.set(Calendar.SECOND, 0)
                endCalendar.set(Calendar.MILLISECOND, 0)
                dateFormat = SimpleDateFormat("yyyy", Locale.getDefault())
            }

            else -> {
                throw IllegalArgumentException("Unsupported time frame: $timeFrame")
            }
        }

        val expensesMap = mutableMapOf<String, Float>().apply {
            xAxisLabels.value.forEach { put(it, 0f) }
        }

        val query = db.collection("expenses")
            .document("Kantin")
            .collection(timeFrame.lowercase())

        query.get().addOnSuccessListener { result ->
            val expenses = mutableListOf<Pair<Float, Date>>()
            for (document in result) {
                val total = document.getDouble("total")?.toFloat() ?: 0f
                val date = dateFormat.parse(document.id) ?: Date()
                expenses.add(total to date)
            }

            expenses.groupBy({ dateFormat.format(it.second) }, { it.first }).forEach { date, values ->
                values.sum().let { expenseSum ->
                    expensesMap[date] = expensesMap.getOrDefault(date, 0f) + expenseSum
                }
            }

            for (expense in expenses) {
                val dateFormatted = dateFormat.format(expense.second)
                expensesMap[dateFormatted] = expense.first
            }

            chartEntries.value =
                entriesOf(*expensesMap.entries.mapIndexed { index, (_, expense) -> index.toFloat() to expense }
                    .toTypedArray())
        }
    }

        LaunchedEffect(Unit) {
            fetchExpensesData(timeFrames[0])
        }

    Column(modifier = Modifier.padding(paddingValues)) {
        timeFrames.forEachIndexed { index, timeFrame ->
            Button(
                onClick = {
                    selectedIndex.value = index
                    fetchExpensesData(timeFrame)
                },
                colors = ButtonDefaults.buttonColors(
                    containerColor = if (selectedIndex.value == index) DarkGray else LightGray
                )
            ) {
                Text(timeFrame)
            }
        }
        val AXIS_VALUE_OVERRIDER_Y_FRACTION = 1.2f
        val axisValueOverrider =
            AxisValuesOverrider.adaptiveYValues(
                yFraction = AXIS_VALUE_OVERRIDER_Y_FRACTION,
                round = true
            )

        ProvideChartStyle(chartStyle = m3ChartStyle()) {
            Chart(
                chart = columnChart(
                    axisValuesOverrider = axisValueOverrider
                ),
                chartModelProducer = chartModelProducer,
                startAxis = startAxis(),
                bottomAxis = bottomAxis(
                    valueFormatter = bottomAxisValueFormatter
                ),
                marker = rememberMarker()
            )
        }
    }
}

it is still showing two weeks for the daily graph and 6 weeks for the weekly graph, maybe there is something wrong with the query?