<?php
namespace App\Controllers;

use App\Models\ProductModel;
use App\Models\SaleModel;
use App\Models\UserModel;
use App\Models\CajaModel;

class DashboardController extends BaseController
{
    public function index()
    {
        if (!session()->get('user_id')) {
            return redirect()->to('/login');
        }

        $productModel = new ProductModel();
        $saleModel = new SaleModel();
        $userModel = new UserModel();

        $data['title'] = 'Dashboard';
        $data['totalProducts'] = $productModel->countAll();
        $data['lowStockCount'] = count($productModel->getLowStockProducts());
        $data['lowStockProducts'] = $productModel->getLowStockProducts();

        if (session()->get('role') == 'admin') {
            $data['totalSalesToday'] = $saleModel->getTotalSalesToday();
            $data['totalUsers'] = $userModel->countAll();
            $data['recentSales'] = $saleModel->getRecentSales(5);
            $data['chartSalesWeek'] = $saleModel->getSalesLast7Days();
            $data['chartCategory'] = $saleModel->getSalesByCategory();
            $data['chartPayment'] = $saleModel->getSalesByPaymentMethod();

            return view('dashboard/admin', $data);
        } else {
            $data['mySalesToday'] = $saleModel->getSalesByUserToday(session()->get('user_id'));
            $data['myRecentSales'] = $saleModel->getSalesByUser(session()->get('user_id'), 5);
            $data['chartMySalesWeek'] = $saleModel->getUserSalesLast7Days(session()->get('user_id'));

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