<?php

namespace App\Components\WillsCase;

use App\Models\WillsCase\WillsCase;
use App\Models\WillsCase\WillsCaseComment;
use App\Services\HistoryService;
use Illuminate\Support\Str;
use Livewire\Component;

class CommentManager extends Component {
    public WillsCase $willsCase;
    public string $newComment = '';

    public ?int $editingId = null;
    public string $editingText = '';

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

    public function addComment(HistoryService $history): void {
        $this->validateOnly('newComment');

        $comment = WillsCaseComment::create([
            'wills_case_id'      => $this->willsCase->id,
            'comment_created_by' => auth()->id(),
            'comment_created_at' => now(),
            'comment_text'       => $this->newComment,
        ]);

        $history->add($this->willsCase, 'Comment added', [
            'comment_id' => $comment->id,
            'preview'    => Str::limit($this->newComment, 120),
        ]);

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

    public function startEdit(int $id): void {
        $comment = WillsCaseComment::where('wills_case_id', $this->willsCase->id)->findOrFail($id);
        $this->editingId = $comment->id;
        $this->editingText = $comment->comment_text;
    }

    public function cancelEdit(): void {
        $this->editingId = null;
        $this->editingText = '';
    }

    public function saveEdit(HistoryService $history): void {
        $this->validateOnly('editingText');

        if (!$this->editingId) {
            return;
        }

        $comment = WillsCaseComment::where('wills_case_id', $this->willsCase->id)->findOrFail($this->editingId);
        $before = $comment->comment_text;
        $comment->update(['comment_text' => $this->editingText]);

        $history->add($this->willsCase, 'Comment edited', [
            'comment_id' => $comment->id,
            'from'       => Str::limit($before, 120),
            'to'         => Str::limit($this->editingText, 120),
        ]);

        $this->cancelEdit();
        $this->dispatch('toast', title: 'Comment updated', type: 'success');
    }

    public function deleteComment(int $id, HistoryService $history): void {
        $comment = WillsCaseComment::where('wills_case_id', $this->willsCase->id)->findOrFail($id);
        $preview = Str::limit($comment->comment_text, 120);
        $comment->delete();

        $history->add($this->willsCase, 'Comment deleted', [
            'comment_id' => $id,
            'preview'    => $preview,
        ]);

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

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