<?php

namespace Modules\Dashboard\Http\Controllers;

use App\Http\Controllers\Controller;
use Modules\Dashboard\Services\KJC\KJCDashboardService;
use Modules\Dashboard\Services\PKJ\PKJDashboardService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class DashboardController extends Controller
{
    protected KJCDashboardService $kjcDashboardService;
    protected PKJDashboardService $pkjDashboardService;

    public function __construct(
        KJCDashboardService $kjcDashboardService,
        PKJDashboardService $pkjDashboardService
    ) {
        $this->kjcDashboardService = $kjcDashboardService;
        $this->pkjDashboardService = $pkjDashboardService;
    }

    /**
     * Get dashboard overview data
     */
    public function getOverview(Request $request): JsonResponse
    {
        try {
            $filters = $request->only(['date_from', 'date_to', 'unit_id', 'camp_id', 'formation_id', 'government_id']);
            $user = $request->user(); // Get authenticated user
            
            $overview = [
                'total_units' => $this->kjcDashboardService->getTotalUnits($filters),
                'total_camps' => $this->kjcDashboardService->getTotalCamps($filters),
                'total_formations' => $this->kjcDashboardService->getTotalFormations($filters),
                'kjc' => [
                    'total_assets' => $this->kjcDashboardService->getTotalAssets($filters, $user),
                    'operational_assets' => $this->kjcDashboardService->getOperationalAssets($filters, $user),
                    'under_repair' => $this->kjcDashboardService->getUnderRepairAssets($filters, $user),
                    'maintenance_due' => $this->kjcDashboardService->getMaintenanceDueAssets($filters, $user),
                    'active_repairs' => $this->kjcDashboardService->getActiveRepairs($filters),
                    'pending_reports' => $this->kjcDashboardService->getPendingReports($filters)
                ],
                'pkj' => [
                    'total_assets' => $this->pkjDashboardService->getTotalAssets($filters, $user),
                    'operational_assets' => $this->pkjDashboardService->getOperationalAssets($filters, $user),
                    'under_repair' => $this->pkjDashboardService->getUnderRepairAssets($filters, $user),
                    'maintenance_due' => $this->pkjDashboardService->getMaintenanceDueAssets($filters, $user),
                    'active_repairs' => $this->pkjDashboardService->getActiveRepairs($filters),
                    'pending_reports' => $this->pkjDashboardService->getPendingReports($filters)
                ]
            ];

            return response()->json([
                'success' => true,
                'data' => $overview
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch dashboard overview',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get stat cards data
     */
    public function getStatCards(Request $request): JsonResponse
    {
        try {
            $filters = $request->only(['date_from', 'date_to', 'unit_id', 'camp_id', 'formation_id', 'government_id']);
            $user = $request->user(); // Get authenticated user
            
            $statCards = [
                'kjc' => [
                    'total_assets' => $this->kjcDashboardService->getTotalAssets($filters, $user),
                    'operational_assets' => $this->kjcDashboardService->getOperationalAssets($filters, $user),
                    'under_repair' => $this->kjcDashboardService->getUnderRepairAssets($filters, $user),
                    'maintenance_due' => $this->kjcDashboardService->getMaintenanceDueAssets($filters, $user),
                ],
                'pkj' => [
                    'total_assets' => $this->pkjDashboardService->getTotalAssets($filters, $user),
                    'operational_assets' => $this->pkjDashboardService->getOperationalAssets($filters, $user),
                    'under_repair' => $this->pkjDashboardService->getUnderRepairAssets($filters, $user),
                    'maintenance_due' => $this->pkjDashboardService->getMaintenanceDueAssets($filters, $user),
                ],
                'overview' => [
                    'total_units' => $this->kjcDashboardService->getTotalUnits($filters),
                    'active_repairs' => $this->kjcDashboardService->getActiveRepairs($filters) + $this->pkjDashboardService->getActiveRepairs($filters),
                    'pending_reports' => $this->kjcDashboardService->getPendingReports($filters) + $this->pkjDashboardService->getPendingReports($filters),
                ]
            ];

            return response()->json([
                'success' => true,
                'data' => $statCards
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch stat cards',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}