<?php

namespace App\Http\Controllers;

use App\Concerns\HasRoleChecks;
use App\Enums\Role;
use App\Services\AnalysisService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AnalysisController extends Controller
{
    use HasRoleChecks;

    public function __construct(
        private AnalysisService $service
    ) {}

    public function index(Request $request): Response
    {
        $user = $request->user();
        $isManager = self::hasAnyRole([Role::OWNER, Role::DEVELOPER, Role::ADMIN_TOKO, Role::DIREKTUR]);

        $startDate = $request->input('start_date');
        $endDate = $request->input('end_date');

        $attendance = $this->service->getAttendanceStats($startDate, $endDate, $user);
        $myAttendance = $this->service->getMyAttendance($user, $startDate, $endDate);
        $cashOverview = $this->service->getCashOverview($startDate, $endDate);
        $rawMaterialStock = $this->service->getRawMaterialStock();
        $productStock = $this->service->getProductStock();
        $revenueSummary = $this->service->getRevenueSummary($startDate, $endDate, $user);
        $monthlyRevenue = $this->service->getMonthlyRevenue($startDate, $endDate, $user);
        $monthlyRevenueByChannel = $this->service->getMonthlyRevenueByChannel($startDate, $endDate, $user);
        $revenueByPaymentType = $this->service->getRevenueByPaymentType($startDate, $endDate, $user);
        $expenseSummary = $this->service->getExpenseSummary($startDate, $endDate, $user);
        $monthlyExpense = $this->service->getMonthlyExpense($startDate, $endDate, $user);
        $busyHours = $this->service->getBusyHours($startDate, $endDate, $user);
        $profitMetrics = $this->service->getProfitMetrics($startDate, $endDate, $user);
        $topSuppliers = $this->service->getTopSuppliers($startDate, $endDate);
        $topCustomers = $this->service->getTopCustomers($startDate, $endDate, $user);
        $topProducts = $this->service->getTopProducts($startDate, $endDate, $user);
        $marketingSales = $this->service->getMarketingSales($startDate, $endDate, $user);
        $orderStats = $this->service->getOrderStats($startDate, $endDate, $user);
        $revenueTrend = $this->service->getRevenueTrend($startDate, $endDate, $user);

        return Inertia::render('admin/analysis/index', [
            'filters' => [
                'start_date' => $startDate,
                'end_date' => $endDate,
            ],
            'attendance' => $attendance,
            'myAttendance' => $myAttendance,
            'isManager' => $isManager,
            'cashOverview' => $cashOverview,
            'rawMaterialStock' => $rawMaterialStock,
            'productStock' => $productStock,
            'revenueSummary' => $revenueSummary,
            'monthlyRevenue' => $monthlyRevenue,
            'monthlyRevenueByChannel' => $monthlyRevenueByChannel,
            'revenueByPaymentType' => $revenueByPaymentType,
            'expenseSummary' => $expenseSummary,
            'monthlyExpense' => $monthlyExpense,
            'busyHours' => $busyHours,
            'profitMetrics' => $profitMetrics,
            'topSuppliers' => $topSuppliers,
            'topCustomers' => $topCustomers,
            'topProducts' => $topProducts,
            'marketingSales' => $marketingSales,
            'orderStats' => $orderStats,
            'revenueTrend' => $revenueTrend,
        ]);
    }
}
