<?php

namespace App\Models;

use App\Auth\XenForoUser;
use App\Services\XenforoService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * @property int $id
 * @property string $name
 * @property string $slug
 * @property string|null $website
 * @property int|null $user_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Entry> $entries
 * @property-read int|null $entries_count
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Author whereWebsite($value)
 * @mixin \Eloquent
 */
class Author extends Model
{
    protected $fillable = [
        'name', 'slug', 'user_id', 'website'
    ];

    public function entries(): BelongsToMany
    {
        return $this->belongsToMany(Entry::class, 'entry_authors');
    }

    public function user(): ?XenForoUser
    {
        if( !$this->user_id )
            return null;

        return app(XenforoService::class)->getXfUser($this->user_id);
    }

}
