<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Report;
use App\Support\Moderation\ReportTargetResolver;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class ReportController extends Controller
{
    public function __construct(private readonly ReportTargetResolver $targets) {}

    public function store(Request $request): JsonResponse
    {
        $user = $request->user();

        $data = $request->validate([
            'target_type' => ['required', Rule::in($this->targets->supportedTargetTypes())],
            'target_id' => 'required|integer|min:1',
            'reason' => 'required|string|max:120',
            'details' => 'nullable|string|max:4000',
        ]);

        $targetType = $data['target_type'];
        $targetId = (int) $data['target_id'];

        $this->targets->validateForReporter($user, $targetType, $targetId);

        $report = Report::query()->create([
            'reporter_id' => $user->id,
            'target_type' => $targetType,
            'target_id' => $targetId,
            'reason' => $data['reason'],
            'details' => $data['details'] ?? null,
            'status' => 'open',
        ]);

        return response()->json(['id' => $report->id, 'status' => $report->status], 201);
    }
}
