<?php

namespace App\Controllers;

use App\Models\DashboardModel;
use App\Models\ItemModel;
use App\Controllers\BaseController;
use App\Models\CategoryModel;

class Dashboard extends BaseController
{
    protected $dashboardModel;

    public function __construct()
    {

        $this->dashboardModel = new DashboardModel();
    }

    public function index()
{
    $itemModel = new ItemModel();

    $gudangSummary = $itemModel->select('gudang.nama as gudang_nama, COUNT(items.id) as total_item, SUM(items.stok) as total_stok')
        ->join('gudang', 'gudang.id = items.id_gudang', 'left')
        ->groupBy('items.id_gudang')
        ->findAll();

    $categoryModel = new CategoryModel();
    $kategori = $categoryModel->findAll();

    $produkPerKategori = [];
    $produkPerKategoriLabel = [];
    $produkPerKategoriJumlah = [];

    foreach ($kategori as $k) {
        $jumlah = $itemModel->where('id_category', $k['id'])->countAllResults();
        $produkPerKategori[] = [
            'kategori' => $k['nama'],
            'jumlah'   => $jumlah
        ];

        $produkPerKategoriLabel[] = $k['nama'];
        $produkPerKategoriJumlah[] = $jumlah;
    }

    // Stok per barang
    $stokPerBarangData = $itemModel->select('nama, stok')
        ->orderBy('stok', 'DESC')
        ->findAll();

    $stokBarangLabels = array_column($stokPerBarangData, 'nama');
    $stokBarangJumlah = array_column($stokPerBarangData, 'stok');

    // Filter hanya bulan
    $bulan = $this->request->getGet('bulan');

    if ($bulan) {
        $produkTerlaris = $this->dashboardModel->getTopProdukTerjual(5, $bulan);
    } else {
        $produkTerlaris = $this->dashboardModel->getTop5Sales();
    }

    $produkTerlarisLabel = array_column($produkTerlaris, 'nama_produk');
    $produkTerlarisJumlah = array_column($produkTerlaris, 'total_terjual');
    $totalStock = $itemModel->selectSum('stok')->first()['stok'];

    $saleModel = new \App\Models\SaleModel();
    $totalSales = $saleModel->selectSum('total_bayar')->first()['total_bayar'] ?? 0;
    $saledetailModel = new \App\Models\SaleDetailModel();
    $totalQtySold = $saledetailModel->selectSum('qty')->first()['qty'];
    $totalTransaksi = $saleModel->countAll();

    $tahun = $this->request->getGet('tahun') ?? date('Y');
    $penjualanBulanan = $this->dashboardModel->getMonthlySales($tahun);


    $data = [
        'title'                => 'Dashboard',
        'totalProducts'        => $this->dashboardModel->countItems(),
        'totalSuppliers'       => $this->dashboardModel->countSuppliers(),
        'totalCustomers'       => $this->dashboardModel->countCustomers(),
        'totalUsers'           => $this->dashboardModel->countUsers(),
        'gudangArea'           => $this->dashboardModel->getTotalGudangByJenis('area'),
        'gudangProduksi'       => $this->dashboardModel->getTotalGudangByJenis('produksi'),
        'topProducts'          => $produkTerlaris,
        'produkPerKategori'    => $produkPerKategori,
        'kategoriLabels'       => $produkPerKategoriLabel,    
        'kategoriJumlah'       => $produkPerKategoriJumlah,
        'gudangSummary'        => $gudangSummary,
        'produkTerlarisLabel'  => $produkTerlarisLabel,
        'produkTerlarisJumlah' => $produkTerlarisJumlah,
        'stokPerBarang'        => $stokPerBarangData,
        'stokBarangLabels'     => $stokBarangLabels,
        'stokBarangJumlah'     => $stokBarangJumlah,
        'filter_bulan'         => $bulan,
        'totalStock'           => $totalStock,
        'totalSales'           => $totalSales,
        'totalQtySold'         => $totalQtySold,
        'totalTransaksi'       => $totalTransaksi,
        'penjualanBulanan'     => $penjualanBulanan,
        'filter_tahun'         => $tahun,

    ];

    return view('dashboard/index', $data);
}



}
