<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravelista\Comments\Comments\Traits\Comments;
use Illuminate\Support\Facades\Auth;
use App\Tasktag;


class Task extends Model
{
    //
    protected $table = 'tasks';
    protected $fillable = ['user_id', 'unit_id', 'status_id', 'draft', 'type', 'taskdata_id'];


    use Comments;


    public function pipeline()
    {
        return $this->belongsToMany('App\Pipeline', 'task_pipeline');
    }

    public function taskdata()
    {
        return $this->hasOne('App\Taskdata', 'id', 'taskdata_id');
    }


    public function createdBy()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

//    public function units()
//    {
//        $this->belongsToMany('App\Unit','task_unit','task_id','unit_id');
//    }
    public function status()
    {
        return $this->belongsTo('App\Status', 'status_id', 'id');
    }


    public function unit()
    {
        return $this->belongsTo('App\Unit', 'unit_id', 'id');

    }

    public function subtasks()
    {
        return $this->hasMany('App\Subtask', 'task_id', 'id');
    }


    public function profile()
    {
        return $this->belongsTo('App\Profile', 'profile_id', 'id');
    }

    public function isDraft()
    {
        return $this->draft;
    }

    public function attachments()
    {
        // return $this->hasMany('App\Attachment', 'task_id', 'id');
        return $this->belongsToMany('App\Attachment', "attach_task")->withPivot('active','delivered_final');

    }

    public function tasktag()
    {
        return $this->hasMany('App\Tasktag', 'relation_id', 'id');
    }


    public function userCanPassTask()
    {

        $user = Auth::user();
        if ($user->hasRole('administrator')) {
            return true;
        };
        if ($user->hasRole('teamleader')) {
            return true;
        }


        // fin del cepo a Marina
//        if ($user->hasRole('teamleader')) {
//
//            if ($this->unit->id == 1) {
//                if ($user->id == 5) {
//                    return true;
//                } else {
//                    return false;
//
//                }
//            } else {
//                return true;
//
//            }
//        };

        return false;
    }





    public function userCanEditTask()
    {

        $user = Auth::user();
        if ($user->hasRole('administrator')) {
            return true;
        };

        if ($user->hasRole('teamleader')) {
            return true;
        };

        foreach ($this->unit->teamleaders as $tl) {

            if ($user->id == $tl->id) {
                return true;
            }

        }
        return false;


    }


    public function userCanChangeStatu()
    {

        $user = Auth::user();
        if ($user->hasRole('administrator')) {
            return true;
        };

        if ($user->hasRole('teamleader')) {
            return true;
        }

        return false;


    }

    public function userCanViewTask()
    {
        $user = Auth::user();

        if ($user->hasRole('administrator')) {
            return true;
        };
        if ($user->hasRole('supervisor')) {
            return true;
        };

        if ($user->hasRole('teamleader')) {
            return true;
        };


        return false;


    }

    public function userTagCanViewTask($taskId, $unit_id)
    {

        $user = Auth::user()->id;

        $etiquetados = Tasktag::select('user_id')->where('relation_id', '=', $taskId)//busco usuarios que ya estan etiquetados en la esta tarea
        ->where('type', '=', 'task')
            ->where('user_id', '=', $user)
            ->where('unit_id', '=', $unit_id)
            ->where('active', '=', 1)->count();


        if ($etiquetados > 0) {

            return true;
        } else {

            false;
        }

    }


}
