<?php

namespace App\Http\Controllers\Api\Posts;

use App\Events\Posts\ArtworkShared;
use App\Http\Controllers\Controller;
use App\Http\Requests\Posts\ShareArtworkRequest;
use App\Models\Artwork;
use App\Services\Posts\PostFeedService;
use App\Services\Posts\PostShareService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\RateLimiter;

class PostShareController extends Controller
{
    public function __construct(
        private PostShareService $shareService,
        private PostFeedService  $feedService,
    ) {}

    /**
     * POST /api/posts/share/artwork/{artwork_id}
     * payload: { body?, visibility }
     */
    public function shareArtwork(ShareArtworkRequest $request, int $artworkId): JsonResponse
    {
        $user    = $request->user();
        $artwork = Artwork::findOrFail($artworkId);

        // Rate limit: 10 artwork shares per hour
        $key = 'share_artwork:' . $user->id;
        if (RateLimiter::tooManyAttempts($key, 10)) {
            $seconds = RateLimiter::availableIn($key);
            return response()->json([
                'message' => "You're sharing too quickly. Please wait {$seconds} seconds.",
            ], 429);
        }
        RateLimiter::hit($key, 3600);

        $post = $this->shareService->shareArtwork(
            user:       $user,
            artwork:    $artwork,
            body:       $request->input('body'),
            visibility: $request->input('visibility', 'public'),
        );

        $post->load(['user', 'user.profile', 'targets', 'targets.artwork', 'targets.artwork.user', 'targets.artwork.user.profile', 'reactions']);

        // Notify original artwork owner (unless self-share)
        if ($artwork->user_id !== $user->id) {
            event(new ArtworkShared($post, $artwork, $user));
        }

        return response()->json([
            'post' => $this->feedService->formatPost($post, $user->id),
        ], 201);
    }
}
