<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\System\AnalysisService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AnalysisController extends Controller
{
    public function __construct(
        private readonly AnalysisService $analysisService,
    ) {}

    public function index(Request $request): Response
    {
        $startDate = $request->query('start_date') ? Carbon::parse($request->query('start_date'))->startOfDay() : null;
        $endDate = $request->query('end_date') ? Carbon::parse($request->query('end_date'))->endOfDay() : null;
        $user = $request->user();

        return Inertia::render('admin/Analysis', [
            'filters' => [
                'start_date' => $request->query('start_date', ''),
                'end_date' => $request->query('end_date', ''),
            ],
            'attendance' => $this->analysisService->getAttendance(),
            'myAttendance' => $user ? $this->analysisService->getMyAttendance($user, $startDate, $endDate) : null,
            'isManager' => $this->analysisService->isManager($user),
            'cashOverview' => $this->analysisService->getCashOverview(),
            'rawMaterialStock' => $this->analysisService->getRawMaterialStock(),
            'productStock' => $this->analysisService->getProductStock(),
            'revenueSummary' => $this->analysisService->getRevenueSummary($startDate, $endDate),
            'monthlyRevenue' => $this->analysisService->getMonthlyRevenue($startDate, $endDate),
            'expenseSummary' => $this->analysisService->getExpenseSummary($startDate, $endDate),
            'monthlyExpense' => $this->analysisService->getMonthlyExpense($startDate, $endDate),
            'busyHours' => $this->analysisService->getBusyHours($startDate, $endDate),
            'profitMetrics' => $this->analysisService->getProfitMetrics($startDate, $endDate),
            'topSuppliers' => $this->analysisService->getTopSuppliers($startDate, $endDate),
            'topCustomers' => $this->analysisService->getTopCustomers($startDate, $endDate),
            'topProducts' => $this->analysisService->getTopProducts($startDate, $endDate),
        ]);
    }
}
