<?php

namespace Coolsam\NestedComments\Filament\Infolists;

use Closure;
use Coolsam\NestedComments\Models\Comment;
use Coolsam\NestedComments\NestedComments;
use Filament\Infolists\Components\Entry;
use Illuminate\Database\Eloquent\Model;

class CommentCardEntry extends Entry
{
    /** @phpstan-ignore-next-line */
    protected string $view = 'nested-comments::filament.infolists.comment-card-entry';

    protected bool | Closure $isLabelHidden = true;

    protected string | Closure | null $userNameUsingClosure = null;

    protected string | Closure | null $userAvatarUsingClosure = null;

    protected array $columnSpan = ['default' => 'full'];

    public static function make(?string $name = null): static
    {
        $name = $name ?? 'body';

        return parent::make($name); // TODO: Change the autogenerated stub
    }

    public function getUserNameUsing(Closure | string | null $callback = null): static
    {
        $this->userNameUsingClosure = $callback;

        return $this;
    }

    public function getUserAvatarUsing(Closure | string | null $callback = null): static
    {
        $this->userAvatarUsingClosure = $callback;

        return $this;
    }

    public function evaluateUserAvatar(): ?string
    {
        return $this->evaluate($this->userAvatarUsingClosure
            ?? fn (Comment | Model $record) => app(NestedComments::class)
                ->getDefaultUserAvatar($record->getAttribute('user')
                    ?? $record->getAttribute('guest_name') ?? __('nested-comments::nested-comments.comments.general.guest')));
    }

    public function evaluateUserName(): ?string
    {
        return $this->evaluate($this->userNameUsingClosure
            ?? fn (Comment | Model | null $record) => $record?->getAttribute('user')?->getAttribute('name')
            ?? $record->getAttribute('guest_name'));
    }
}
