<?php

namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\Orders;

class Reports extends BaseController
{
    protected $helpers = ['form'];
    protected $Orders;

    public function __construct()
    {
        
        $this->Orders = new Orders();
    }


    public function getrevenue()
    {   
        
        $clientKey = $this->request->getGet('client_key'); // Ambil filter dari URL parameter
        $ordersModel = new Orders();
        $orders = $ordersModel->getOrdersWithSummary($clientKey); // Gunakan fungsi khusus di model





        $groupedData = [];

        foreach ($orders as $order) {
            $tahun = date('Y', strtotime($order['tanggal_order']));
            $bulan = date('F', strtotime($order['tanggal_order'])); // Ubah angka jadi nama bulan
            $minggu = date('W', strtotime($order['tanggal_order']));
            $hari = date('d', strtotime($order['tanggal_order']));

            $netAmount = floatval($order['net_amount']);

            if (!isset($groupedData[$tahun])) {
                $groupedData[$tahun] = ['sum_net_amount' => 0, 'bulan' => []];
            }
            $groupedData[$tahun]['sum_net_amount'] += $netAmount;

            if (!isset($groupedData[$tahun]['bulan'][$bulan])) {
                $groupedData[$tahun]['bulan'][$bulan] = ['sum_net_amount' => 0, 'minggu' => []];
            }
            $groupedData[$tahun]['bulan'][$bulan]['sum_net_amount'] += $netAmount;

            if (!isset($groupedData[$tahun]['bulan'][$bulan]['minggu'][$minggu])) {
                $groupedData[$tahun]['bulan'][$bulan]['minggu'][$minggu] = ['sum_net_amount' => 0, 'hari' => []];
            }
            $groupedData[$tahun]['bulan'][$bulan]['minggu'][$minggu]['sum_net_amount'] += $netAmount;

            $groupedData[$tahun]['bulan'][$bulan]['minggu'][$minggu]['hari'][$hari][] = $order;
        }

        $result = [
            'data' => array_map(function ($tahun, $bulanData) {
                return [
                    'tahun' => [
                        'tahun' => $tahun,
                        'sum_net_amount' => $bulanData['sum_net_amount'],
                        'bulan' => array_map(function ($bulan, $mingguData) {
                            return [
                                'bulan' => $bulan,
                                'sum_net_amount' => $mingguData['sum_net_amount'],
                                'minggu' => array_map(function ($minggu, $hariData) {
                                    return [
                                        'minggu' => $minggu,
                                        'sum_net_amount' => $hariData['sum_net_amount'],
                                        'hari' => $hariData['hari']
                                    ];
                                }, array_keys($mingguData['minggu']), $mingguData['minggu'])
                            ];
                        }, array_keys($bulanData['bulan']), $bulanData['bulan'])
                    ]
                ];
            }, array_keys($groupedData), $groupedData)
        ];
        return $this->response->setJSON($result);   
    }
}