# Viartas Comments Package

Comments package for Laravel with admin panel and frontend widget. Features tree-structured replies, ratings, activity logging, and spam protection.

## Installation

```bash
composer require viartas/comments
```

## Publish Assets

```bash
# All
php artisan vendor:publish --tag=comments

# Or individually
php artisan vendor:publish --tag=comments-config
php artisan vendor:publish --tag=comments-migrations
php artisan vendor:publish --tag=comments-views
php artisan vendor:publish --tag=comments-assets
php artisan vendor:publish --tag=comments-translations
```

## Run Migrations

```bash
php artisan migrate
```

## Configuration

Edit `config/comments.php`:

```php
'model_map' => [
    'pages' => App\Models\Page::class,
    'products' => App\Models\Product::class,
    'posts' => App\Models\Post::class,
],
```

## Add Trait to Models

```php
use Viartas\Comments\Traits\HasComments;

class Page extends Model
{
    use HasComments;
}
```

## Usage

### Admin Panel

Access comments at:
```
/comments/pages/123    - Comments for Page #123
/comments/products/456 - Comments for Product #456
```

### Frontend Widget

Include in your Blade template:

```blade
@include('comments::frontend.comments', [
    'model' => $page,
    'modelType' => 'pages',
    'showRating' => true,
    'allowGuests' => true,
])
```

### Available Methods on Models

```php
$page->comments              // All comments
$page->approvedComments      // Only approved
$page->rootComments          // No replies
$page->approvedRootComments  // Approved root with nested replies
$page->pendingCommentsCount  // Count pending
$page->averageRating         // Average rating
$page->ratingDistribution    // [5 => 10, 4 => 5, ...]
$page->commentsCount         // Total approved count
```

## Activity Logging

Comments automatically log to Spatie Activity Log:
- Created
- Updated (content, status, rating)
- Deleted

View in Activity Log package with `commentable_type` and `commentable_id` in properties.

## Features

### Admin
- Tree view with nested replies
- Approve/Reject/Spam actions
- Bulk actions
- Filter by status, answered, rating
- Search
- Reply as admin

### Frontend
- Star ratings (1-5)
- Guest comments with name/email
- Nested replies
- Honeypot spam protection
- Rate limiting
- Auto-approve for logged in users
- Pagination with load more

## Routes

### Admin Routes
- GET `/comments/{model_type}/{model_id}` - List comments
- POST `/comments/{model_type}/{model_id}` - Create reply
- POST `/comments/{model_type}/{model_id}/bulk` - Bulk action
- GET `/comments/{model_type}/{model_id}/{id}` - Show comment
- POST `/comments/{model_type}/{model_id}/{id}/approve` - Approve
- POST `/comments/{model_type}/{model_id}/{id}/reject` - Reject
- POST `/comments/{model_type}/{model_id}/{id}/spam` - Mark spam
- DELETE `/comments/{model_type}/{model_id}/{id}/destroy` - Delete

### Frontend API Routes
- GET `/api/comments/{model_type}/{model_id}` - Get comments (JSON)
- POST `/api/comments/{model_type}/{model_id}` - Submit comment

## Translations

Available in: English, Lithuanian, Russian

## License

MIT
