<?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\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

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

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

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'title',
        'slug',
        'short_description',
        'content',
        'view_count',
        'user_id',
        'status',
        'priority',
        'comment_permission',
        'is_featured',
        'is_pinned',
        'like_count',
        'comment_count',
        'last_activity_at',
    ];

    /**
     * The attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'view_count'       => 'integer',
            'like_count'       => 'integer',
            'comment_count'    => 'integer',
            'is_featured'      => 'boolean',
            'is_pinned'        => 'boolean',
            'last_activity_at' => 'datetime',
        ];
    }

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

        // When a topic is deleted, also soft delete its related data
        static::deleted(function ($topic) {
            // Soft delete all comments
            $topic->comments()->delete();
        });

        // When a topic is restored, also restore its related data
        static::restored(function ($topic) {
            // Restore all comments that were deleted at the same time
            $topic->comments()->onlyTrashed()->restore();
        });
    }

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

    /**
     * Get the categories associated with this topic through pivot table.
     */
    public function categories(): BelongsToMany
    {
        return $this->belongsToMany(TopicCategory::class, 'topic_category_details', 'topic_id', 'topic_category_id')
            ->withTimestamps();
    }

    /**
     * Get the tags associated with this topic through pivot table.
     */
    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(TopicTag::class, 'topic_tag_details', 'topic_id', 'topic_tag_id')
            ->withTimestamps();
    }

    /**
     * Get all comments for this topic.
     */
    public function comments(): HasMany
    {
        return $this->hasMany(TopicComment::class);
    }

    /**
     * Get only root comments (not replies).
     */
    public function rootComments(): HasMany
    {
        return $this->hasMany(TopicComment::class)->where('parent_id', 0);
    }

    /**
     * Get users who are following this topic.
     */
    public function followers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'topic_follows', 'topic_id', 'user_id')
            ->withTimestamps();
    }

    /**
     * Get follow records for this topic.
     */
    public function follows(): HasMany
    {
        return $this->hasMany(TopicFollow::class);
    }

    /**
     * Scope for published topics only.
     */
    public function scopePublished($query)
    {
        return $query->where('status', 'published');
    }

    /**
     * Scope for featured topics.
     */
    public function scopeFeatured($query)
    {
        return $query->where('is_featured', true);
    }

    /**
     * Scope for pinned topics.
     */
    public function scopePinned($query)
    {
        return $query->where('is_pinned', true);
    }

    /**
     * Scope for urgent topics.
     */
    public function scopeUrgent($query)
    {
        return $query->where('priority', 'urgent');
    }

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

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

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

    /**
     * Increment view count.
     */
    public function incrementViews(): void
    {
        $this->increment('view_count');
    }

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

    /**
     * Update last activity timestamp.
     */
    public function updateLastActivity(): void
    {
        $this->update(['last_activity_at' => now()]);
    }

    /**
     * Check if comments are allowed for this topic.
     */
    public function commentsAllowed(): bool
    {
        return $this->comment_permission !== 'closed';
    }

    /**
     * Check if user can comment on this topic.
     */
    public function canUserComment(User $user): bool
    {
        if ($this->comment_permission === 'closed') {
            return false;
        }

        if ($this->comment_permission === 'all') {
            return true;
        }

        if ($this->comment_permission === 'experts_only') {
            return $user->hasRole(['administrator', 'editor', 'expert']);
        }

        if ($this->comment_permission === 'users_only') {
            return $user->hasRole(['subscriber', 'author', 'contributor', 'editor', 'administrator']);
        }

        return false;
    }

    /**
     * Get the followers count attribute.
     */
    public function getFollowersCountAttribute(): int
    {
        return $this->follows()->count();
    }

    /**
     * Check if a user is following this topic.
     */
    public function isFollowedBy(User $user): bool
    {
        return $this->followers()->where('user_id', $user->id)->exists();
    }

    /**
     * Follow this topic by a user.
     */
    public function followBy(User $user): void
    {
        if (! $this->isFollowedBy($user)) {
            $this->followers()->attach($user->id);
        }
    }

    /**
     * Unfollow this topic by a user.
     */
    public function unfollowBy(User $user): void
    {
        $this->followers()->detach($user->id);
    }
}
