<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class TopicComment extends Model
{
    /** @use HasFactory<\Database\Factories\TopicCommentFactory> */
    use HasFactory, SoftDeletes;

    /**
     * The table associated with the model.
     */
    protected $table = 'topic_comments';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'topic_id',
        'user_id',
        'content',
        'parent_id',
        'quote_content',
        'like_count',
        'dislike_count',
        'is_approved',
        'is_solution',
    ];

    /**
     * The attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'like_count'    => 'integer',
            'dislike_count' => 'integer',
            'is_approved'   => 'boolean',
            'is_solution'   => 'boolean',
            'parent_id'     => 'integer',
        ];
    }

    /**
     * Boot the model and set up event listeners.
     */
    protected static function boot(): void
    {
        parent::boot();

        // When a comment is created, update topic's last activity and comment count
        static::created(function ($comment) {
            $comment->topic->increment('comment_count');
            $comment->topic->updateLastActivity();
        });

        // When a comment is deleted, update topic's comment count
        static::deleted(function ($comment) {
            $comment->topic->decrement('comment_count');
        });

        // When a comment is restored, update topic's comment count
        static::restored(function ($comment) {
            $comment->topic->increment('comment_count');
        });
    }

    /**
     * Get the topic this comment belongs to.
     */
    public function topic(): BelongsTo
    {
        return $this->belongsTo(Topic::class);
    }

    /**
     * Get the user who wrote this comment.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the parent comment (if this is a reply).
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(TopicComment::class, 'parent_id');
    }

    /**
     * Get all replies to this comment.
     */
    public function replies(): HasMany
    {
        return $this->hasMany(TopicComment::class, 'parent_id');
    }

    /**
     * Scope for root comments only (not replies).
     */
    public function scopeRootComments($query)
    {
        return $query->where('parent_id', 0);
    }

    /**
     * Scope for approved comments only.
     */
    public function scopeApproved($query)
    {
        return $query->where('is_approved', true);
    }

    /**
     * Scope for solution comments.
     */
    public function scopeSolutions($query)
    {
        return $query->where('is_solution', true);
    }

    /**
     * Scope for ordering by latest.
     */
    public function scopeLatest($query)
    {
        return $query->orderBy('created_at', 'desc');
    }

    /**
     * Scope for ordering by most liked.
     */
    public function scopeMostLiked($query)
    {
        return $query->orderBy('like_count', 'desc');
    }

    /**
     * Check if this is a root comment (not a reply).
     */
    public function isRootComment(): bool
    {
        return $this->parent_id == 0;
    }

    /**
     * Check if this is a reply to another comment.
     */
    public function isReply(): bool
    {
        return $this->parent_id > 0;
    }

    /**
     * Increment like count.
     */
    public function incrementLikes(): void
    {
        $this->increment('like_count');
    }

    /**
     * Increment dislike count.
     */
    public function incrementDislikes(): void
    {
        $this->increment('dislike_count');
    }

    /**
     * Mark this comment as solution.
     */
    public function markAsSolution(): void
    {
        $this->update(['is_solution' => true]);

        // Update topic status to resolved if not already
        if ($this->topic->status !== 'resolved') {
            $this->topic->update(['status' => 'resolved']);
        }
    }

    /**
     * Unmark this comment as solution.
     */
    public function unmarkAsSolution(): void
    {
        $this->update(['is_solution' => false]);
    }
}
