<?php

namespace Modules\SystemCore\App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Settings;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\SystemCore\App\Models\CoreCustomer;
use Modules\SystemCore\App\Models\CoreProduct;
use Modules\SystemCore\App\Models\CorePurchaseOrder;
use Modules\SystemCore\App\Models\CoreSalesOrder;
use Modules\SystemCore\App\Models\CoreSupplier;

class DashboardController extends Controller
{
    public function index()
    {
        $settings = Settings::first();

        // Basic counts
        $statistics = [
            'total_customers' => CoreCustomer::count(),
            'active_customers' => CoreCustomer::active()->count(),
            'total_products' => CoreProduct::count(),
            'active_products' => CoreProduct::active()->count(),
            'total_suppliers' => CoreSupplier::count(),
            'active_suppliers' => CoreSupplier::active()->count(),
            'total_sales_orders' => CoreSalesOrder::count(),
            'total_purchase_orders' => CorePurchaseOrder::count(),
        ];

        // Orders today
        $statistics['orders_today'] = CoreSalesOrder::whereDate('created_at', today())->count();
        $statistics['purchase_orders_today'] = CorePurchaseOrder::whereDate('created_at', today())->count();

        // This month totals
        $statistics['sales_this_month'] = CoreSalesOrder::whereMonth('created_at', now()->month)
            ->whereYear('created_at', now()->year)
            ->sum('total_amount');

        // Sales orders by status
        $salesByStatus = CoreSalesOrder::select('status', DB::raw('count(*) as count'))
            ->groupBy('status')
            ->pluck('count', 'status')
            ->toArray();

        // Purchase orders by status
        $purchaseByStatus = CorePurchaseOrder::select('status', DB::raw('count(*) as count'))
            ->groupBy('status')
            ->pluck('count', 'status')
            ->toArray();

        // Recent orders
        $recentSalesOrders = CoreSalesOrder::with('customer:id,name')
            ->select('id', 'order_number', 'customer_id', 'total_amount', 'status', 'created_at')
            ->latest()
            ->take(5)
            ->get();

        $recentPurchaseOrders = CorePurchaseOrder::with('supplier:id,name')
            ->select('id', 'order_number', 'supplier_id', 'total_amount', 'status', 'created_at')
            ->latest()
            ->take(5)
            ->get();

        // Low stock products (if stock tracking is available)
        $lowStockCount = 0;
        if (Schema::hasColumn('core_products', 'stock_quantity')) {
            $lowStockCount = CoreProduct::where('stock_quantity', '<', 10)
                ->where('stock_quantity', '>', 0)
                ->count();
        }

        return view('systemcore::dashboard', compact(
            'statistics',
            'salesByStatus',
            'purchaseByStatus',
            'recentSalesOrders',
            'recentPurchaseOrders',
            'lowStockCount',
            'settings'
        ));
    }
}
