<?php

namespace App\Models;
use App\Models\ValidationModel;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Follow extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'follows';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'related_id', 'related_type'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'user_id'      => 'required',
        'related_id'   => 'required',
        'related_type' => 'required',
    ];

    /**
     * Automatically append the user id when creating a new follow.
     *
     * @var void
     */
    public static function boot()
    {
        static::creating(function ($model) {
            $model->user_id = Auth::user()->id;
        });

        parent::boot();
    }

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