<?php

namespace App\Http\Controllers\Api\V1;

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

class ReportController extends Controller
{
    protected ReportService $reportService;

    public function __construct(ReportService $reportService)
    {
        $this->reportService = $reportService;
    }

    /**
     * Create a new report
     */
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'reported_item_type' => 'required|integer|in:1,2,3', // 1=property, 2=user, 3=service
            'reported_item_id' => 'required|integer',
            'reason' => 'required|string|max:255',
            'description' => 'nullable|string|max:1000',
        ]);

        $report = $this->reportService->createReport($validated, $request->user());

        return response()->json([
            'success' => true,
            'message' => 'Report submitted successfully',
            'data' => $this->reportService->formatReportForResponse($report),
        ], 201);
    }

    /**
     * Get user's reports
     */
    public function getUserReports(Request $request): JsonResponse
    {
        $filters = $request->only(['status', 'item_type']);
        $perPage = $request->get('per_page', 15);

        $reports = $this->reportService->getUserReports($request->user()->id, $filters, $perPage);

        return response()->json([
            'success' => true,
            'data' => $reports,
        ]);
    }

    /**
     * Get report details
     */
    public function show(int $id): JsonResponse
    {
        $report = $this->reportService->getReportDetails($id);

        if (!$report) {
            return response()->json([
                'success' => false,
                'message' => 'Report not found',
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $report,
        ]);
    }
}
