<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Transaction;
use Carbon\Carbon;

class DashboardController extends Controller
{
    public function index()
    {
        $today = Carbon::today();

        $todayTransactions = Transaction::with('details')
            ->whereDate('transaction_date', $today)
            ->orderBy('transaction_date', 'desc')
            ->get();

        $totalSales = $todayTransactions->sum('total');

        $totalTransactions = $todayTransactions->count();

        $recentTransactions = Transaction::with('details')
            ->orderBy('transaction_date', 'desc')
            ->limit(3)
            ->get();

        return response()->json([
            'success' => true,
            'data' => [
                'today_sales' => [
                    'total_sales' => $totalSales,
                    'total_transactions' => $totalTransactions,
                ],
                'recent_transactions' => $recentTransactions,
                'today_transactions' => $todayTransactions,
            ],
        ]);
    }
}