<?php

namespace WoWPress\Models;

use Wenprise\Eloquent\Model;

class Complaint extends Model{

    protected $table = "wowpress_complaints";
    public $timestamps = true;
    protected $primaryKey = 'ID';
    protected $guarded = ['ID'];


    public static function getAll(){
        if(current_user_can('wowpress_edit_complaints')){
            return static::all()->sortBy(fn ($a) => $a->deleted_at);
        }else{
            return static::where('user_id',get_current_user_id())
            ->where('deleted_at',null)
            ->get();
        }
    }

    public function canTrash(){
        return current_user_can('wowpress_edit_complaints') || $this->user_id == get_current_user_id();
    }

    public function trash(){
        $this->deleted_at = date('Y-m-d H:i:s');
        $this->save();
    }

    public function restore(){
        $this->deleted_at = null;
        $this->save();
    }

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

    public function canEdit(){
        return current_user_can('wowpress_edit_complaints');# && (get_current_user_id() != $this->user_id);
    }


}