<?php
namespace App\Domain\Feeds\Models;

use App\Models\User;
use App\Models\Model;
use App\Domain\Tags\Models\Tag;
use Spatie\MediaLibrary\HasMedia;
use App\Domain\Feeds\Enums\StatusEnum;
use App\Domain\Comments\Models\Comment;
use App\Domain\Complaints\Models\Complaint;
use Spatie\MediaLibrary\InteractsWithMedia;
use App\Domain\Feeds\Observers\FeedObserver;
use Illuminate\Database\Eloquent\SoftDeletes;

class Feed extends Model implements HasMedia
{
    use InteractsWithMedia, SoftDeletes;

    public function getUserTypeAttribute()
    {
        return $this->user->type;
    }

    protected $casts = [
        'is_paid' => 'integer',
        'is_ads' => 'boolean',
        'status' => StatusEnum::class,
    ];

    protected static function boot()
    {
        parent::boot();

        self::observe(FeedObserver::class);
    }

    public function feedable()
    {
        return $this->morphTo();
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'feed_tags');
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function views()
    {
        return $this->belongsToMany(User::class, 'users_feeds_view');
    }

    public function likes()
    {
        return $this->belongsToMany(User::class, 'users_feeds_like');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function who_boughts()
    {
        return $this->belongsToMany(User::class, 'user_feed_purchase')->withPivot('amount')->withTimestamps();
    }

    public function complaints()
    {
        return $this->hasMany(Complaint::class);
    }
}
