<?php

namespace App\Services;

use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;

class CommentService
{
    public function forPost(Post $post, int $perPage = 10): LengthAwarePaginator
    {
        return $post->comments()
            ->with([
                'user',
                'likes.user',
                'replies' => fn($q) => $q->with(['user', 'likes'])->withCount('likes'),
            ])
            ->withCount('likes')
            ->latest()
            ->paginate($perPage);
    }

    public function create(User $user, Post $post, string $content): Comment
    {
        return Comment::create([
            'post_id' => $post->id,
            'user_id' => $user->id,
            'content' => $content,
        ]);
    }

    public function update(Comment $comment, string $content): Comment
    {
        $comment->update(['content' => $content]);
        return $comment;
    }

    public function delete(Comment $comment): void
    {
        $comment->delete();
    }
}
