<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Community extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'user_id',
        'name',
        'slug',
        'description',
        'logo_path',
        'category',
        'location',
        'type',
    ];

    // Relationships
    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
    
    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'community_user')
            ->withPivot('role', 'joined_at')
            ->withTimestamps();
    }

    public function moderators(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'community_user')
            ->wherePivot('role', 'moderator')
            ->withPivot('joined_at');
    }

    public function admins(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'community_user')
            ->wherePivot('role', 'admin')
            ->withPivot('joined_at');
    }

    public function posts(): BelongsToMany
    {
        return $this->belongsToMany(RateMyPay::class, 'community_rate_my_pay')
            ->withTimestamps();
    }

    public function getLogoUrlAttribute(): ?string
    {
        return $this->logo_path ? asset('storage/' . $this->logo_path) : null;
    }

    public function getMembersCountAttribute(): int
    {
        return $this->members()->count();
    }

    public function getPostsCountAttribute(): int
    {
        return $this->posts()->count();
    }

    public function isMember(User $user): bool
    {
        return $this->members()->where('user_id', $user->id)->exists();
    }

    public function isModerator(User $user): bool
    {
        return $this->members()
            ->where('user_id', $user->id)
            ->wherePivot('role', 'moderator')
            ->exists();
    }

    public function isAdmin(User $user): bool
    {
        return $this->members()
            ->where('user_id', $user->id)
            ->wherePivot('role', 'admin')
            ->exists();
    }

    public function getMemberRole(User $user): ?string
    {
        $member = $this->members()->where('user_id', $user->id)->first();
        return $member ? $member->pivot->role : null;
    }

    public function addMember(User $user, string $role = 'member'): void
    {
        $this->members()->attach($user->id, [
            'role' => $role,
            'joined_at' => now(),
        ]);
    }

    public function removeMember(User $user): void
    {
        $this->members()->detach($user->id);
    }

    public function updateMemberRole(User $user, string $role): void
    {
        $this->members()->updateExistingPivot($user->id, [
            'role' => $role,
        ]);
    }

    public function addPost(RateMyPay $post): void
    {
        $this->posts()->attach($post->id);
    }

    public function removePost(RateMyPay $post): void
    {
        $this->posts()->detach($post->id);
    }

    public function hasPost(RateMyPay $post): bool
    {
        return $this->posts()->where('rate_my_pay_id', $post->id)->exists();
    }
}