<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\V1\Community;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\PostReport;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class PostReportController extends Controller
{
    public function store(Request $request, string $postId): JsonResponse
    {
        $user = $request->user();
        if (! $user) throw new AccessDeniedHttpException('LOGIN_REQUIRED');

        $post = Post::findOrFail($postId);

        $data = $request->validate([
            'reason' => ['required', 'in:spam,abuse,illegal,adult,copyright,other'],
            'note' => ['nullable', 'string', 'max:500'],
            'comment_id' => ['nullable', 'uuid'],
        ]);

        $report = PostReport::create([
            'post_id' => $post->id,
            'comment_id' => $data['comment_id'] ?? null,
            'reporter_user_id' => $user->id,
            'reason' => $data['reason'],
            'note' => $data['note'] ?? null,
            'status' => 'pending',
        ]);

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