<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ReportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ReportController extends Controller
{
    public function __construct(protected ReportService $reportService) {}

    public function projectStatus(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->projectStatus($request->only(['status', 'project_id']));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش وضعیت پروژه‌ها',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function delayedTasks(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->delayedTasks($request->only([
                'date_from', 'date_to', 'project_id', 'user_id', 'status',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش وظایف عقب افتاده',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function teamPerformance(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->teamPerformance($request->only([
                'date_from', 'date_to', 'user_id',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش عملکرد تیم',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function workload(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->workload($request->only(['user_id']));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش بار کاری',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function sprintProgress(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->sprintProgress($request->only([
                'project_id', 'status',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش پیشرفت اسپرینت',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function timeEstimate(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->timeEstimate($request->only([
                'date_from', 'date_to', 'project_id', 'user_id', 'status',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش برآورد زمانی',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function recentActivities(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->recentActivities($request->only([
                'date_from', 'date_to', 'project_id', 'user_id', 'action',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش فعالیت‌های اخیر',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }

    public function riskyProjects(Request $request): JsonResponse
    {
        try {
            $data = $this->reportService->riskyProjects($request->only([
                'status', 'project_id',
            ]));

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'گزارش پروژه‌های پرخطر',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت گزارش',
            ], 500);
        }
    }
}
