<?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
    ) {}

    /**
     * Get investment summary
     * GET /api/reports/investment-summary
     */
    public function investmentSummary(Request $request): JsonResponse
    {
        $filters = $request->validate([
            'start_date' => 'nullable|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
        ]);

        $summary = $this->reportService->getInvestmentSummary($filters);

        return response()->json([
            'success' => true,
            'data' => $summary,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
                'filters' => $filters,
            ],
        ]);
    }

    /**
     * Get investor demographics
     * GET /api/reports/investor-demographics
     */
    public function investorDemographics(): JsonResponse
    {
        $demographics = $this->reportService->getInvestorDemographics();

        return response()->json([
            'success' => true,
            'data' => $demographics,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get financial performance
     * GET /api/reports/financial-performance
     */
    public function financialPerformance(): JsonResponse
    {
        $performance = $this->reportService->getFinancialPerformance();

        return response()->json([
            'success' => true,
            'data' => $performance,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
            ],
        ]);
    }

    /**
     * Get conversion funnel
     * GET /api/reports/conversion-funnel
     */
    public function conversionFunnel(Request $request): JsonResponse
    {
        $filters = $request->validate([
            'start_date' => 'nullable|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
        ]);

        $funnel = $this->reportService->getConversionFunnel($filters);

        return response()->json([
            'success' => true,
            'data' => $funnel,
            'meta' => [
                'timestamp' => now()->toIso8601String(),
                'filters' => $filters,
            ],
        ]);
    }
}
