<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\MarketRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProfitLossController extends Controller
{
    public function index(){
        $date = request()->query('date') ?? date("Y-m-d");
        
        $marketRecords = MarketRecord::with('market')->select(
            'market_id',
            DB::raw('SUM(amount) as total_amount'),
            DB::raw('SUM(win_amount) as total_win_amount')
        )->whereDate('created_at', $date)
        ->groupBy('market_id')
        ->get();

        $marketNumbers = MarketRecord::select(
            'number',
            DB::raw('SUM(amount) as total_amount'),
            DB::raw('SUM(win_amount) as total_win_amount')
        )->whereDate('created_at', $date)
        ->groupBy('number')
        ->get();

        return view('dashboard.profit-loss.index', compact('marketRecords', 'marketNumbers' , 'date'));
    }
}
