<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Sale;
use Inertia\Inertia;

class ProfitController extends Controller
{
    public function index()
    {
        $profits = Sale::with('product')
            ->selectRaw('sales.*, (products.selling_price - products.cost_price) * sales.quantity as profit')
            ->join('products', 'sales.product_id', '=', 'products.id')
            ->latest()
            ->get();

        $totalProfit = $profits->sum('profit');

        return Inertia::render('Admin/Pages/Profit/Index', [
            'profits' => $profits,
            'totalProfit' => $totalProfit,
        ]);
    }
}