<?php

namespace Modules\Dashboard\Http\Controllers\PKJ;

use App\Http\Controllers\Controller;
use Modules\Dashboard\Services\PKJ\PKJDashboardService;
use Modules\Dashboard\Repositories\Contracts\PKJDashboardRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class PKJDashboardController extends Controller
{
    protected PKJDashboardService $pkjDashboardService;
    protected PKJDashboardRepositoryInterface $pkjDashboardRepository;

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

    /**
     * Get PKJ entitlement vs holdings pie chart
     */
    public function getEntitlementVsHoldingsPieChart(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
            $chartData = $this->pkjDashboardService->getEntitlementVsHoldingsPieChart($filters, $user);

            return response()->json([
                'success' => true,
                'data' => $chartData
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch PKJ entitlement vs holdings chart',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get PKJ holdings status breakdown pie chart
     */
    public function getHoldingsStatusBreakdownPieChart(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
            $chartData = $this->pkjDashboardService->getHoldingsStatusBreakdownPieChart($filters, $user);

            return response()->json([
                'success' => true,
                'data' => $chartData
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch PKJ holdings status breakdown chart',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get PKJ monthly trends chart
     */
    public function getMonthlyTrendsChart(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
            $chartData = $this->pkjDashboardService->getMonthlyTrendsChart($filters, $user);

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

    /**
     * Get PKJ holdings by category bar chart
     */
    public function getHoldingsByCategoryBarChart(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
            $chartData = $this->pkjDashboardService->getHoldingsByCategoryBarChart($filters, $user);

            return response()->json([
                'success' => true,
                'data' => $chartData
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch PKJ holdings by category chart',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get PKJ category metrics bar chart (drill-down)
     */
    public function getCategoryMetricsBarChart(Request $request, int $categoryId): JsonResponse
    {
        try {
            $filters = $request->only(['date_from', 'date_to', 'unit_id', 'camp_id', 'formation_id', 'government_id']);
            $user = $request->user(); // Get authenticated user
            $chartData = $this->pkjDashboardService->getCategoryMetricsBarChart($categoryId, $filters, $user);

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

    /**
     * Get PKJ category status breakdown pie chart (drill-down)
     */
    public function getCategoryStatusBreakdownPieChart(Request $request, int $categoryId): JsonResponse
    {
        try {
            $filters = $request->only(['date_from', 'date_to', 'unit_id', 'camp_id', 'formation_id', 'government_id']);
            $user = $request->user(); // Get authenticated user
            $chartData = $this->pkjDashboardService->getCategoryStatusBreakdownPieChart($categoryId, $filters, $user);

            return response()->json([
                'success' => true,
                'data' => $chartData
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch PKJ category status breakdown chart',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get PKJ asset details by category and status (drill-down)
     */
    public function getAssetDetailsByCategoryAndStatus(Request $request, int $categoryId, string $status): JsonResponse
    {
        try {
            $filters = $request->only(['date_from', 'date_to', 'unit_id', 'camp_id', 'formation_id', 'government_id', 'page', 'per_page']);
            $user = $request->user(); // Get authenticated user
            
            // Convert 'null' string to empty string for null status handling
            // Trim whitespace to ensure exact matching
            $status = trim($status);
            $status = $status === 'null' ? '' : $status;
            
            $details = $this->pkjDashboardRepository->getAssetDetailsByCategoryAndStatus($categoryId, $status, $filters, $user);

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