<?php

namespace App\Http\Controllers\Stats;

use App\Http\Controllers\Controller;
use App\Services\DashboardStatService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class StatDashboardController extends Controller
{
    public function __construct(
        private readonly DashboardStatService $statService,
    ) {}

    /**
     * 통계 대시보드
     */
    public function index(Request $request): View|Response
    {
        // Chart.js 스크립트가 @push('scripts')에 있으므로 HX-Redirect 필요
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('stats.dashboard'));
        }

        $tenantId = session('selected_tenant_id');

        $summaryCards = $this->statService->getSummaryCards($tenantId);
        $salesTrend = $this->statService->getSalesTrend($tenantId);
        $financeTrend = $this->statService->getFinanceTrend($tenantId);
        $productionTrend = $this->statService->getProductionTrend($tenantId);
        $systemTrend = $this->statService->getSystemTrend($tenantId);
        $recentAlerts = $this->statService->getRecentAlerts($tenantId);
        $monthlySummary = $this->statService->getMonthlySummary($tenantId);

        return view('stats.dashboard.index', compact(
            'summaryCards',
            'salesTrend',
            'financeTrend',
            'productionTrend',
            'systemTrend',
            'recentAlerts',
            'monthlySummary',
        ));
    }
}
