<?php

namespace App\Components\PensionCase;

use App\Models\PensionCase\PensionCase;
use App\Services\PensionCaseService;
use Livewire\Component;

class CommentManager extends Component {
    public PensionCase $pensionCase;
    public string $newComment = '';

    protected function rules(): array {
        return [
            'newComment' => 'required|string|min:1|max:10000',
        ];
    }

    public function addComment(PensionCaseService $service): void {
        $this->validate();

        $service->addComment($this->pensionCase, $this->newComment);

        $this->newComment = '';
        $this->dispatch('toast', title: 'Comment added', type: 'success');
    }

    public function render() {
        $comments = $this->pensionCase->comments()->with('author')->get();
        return view('components.pension-case.comment-manager', compact('comments'));
    }
}
