<?php

namespace App;

use App\Traits\HasRolesAndPermissions;
use App\Traits\ReportsToAdmin;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable, HasRolesAndPermissions;
    use ReportsToAdmin;
    // use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'username',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    // public $sections;
    // public function __construct(){
    //     $this->sections = new Collection();
    // }

    public function getCompany()
    {
        return $this->belongsTo('\App\Company', 'company', 'id');
    }
    public function getReportingTo()
    {
        return $this->belongsTo('\App\User', 'reporting_to', 'id');
    }
    public function getCreatedBy()
    {
        return $this->belongsTo('\App\User', 'created_by', 'id');
    }
    public function getReportingUsers()
    {
        $data = \App\User::where('reporting_to', \Auth::user()->id)->where('status', 1)->pluck('id')->toArray();
        return $data;
    }
    public function reportingUsers()
    {
        $data = \App\User::where('reporting_to', $this->id)->where('status', 1)->get();

        return $data;
    }
    // public function roles()
    // {
    //     return $this->belongsToMany(Role::class, 'users_roles', 'user_id', 'role_id');
    // }

    public function getAllReportingUsersObj()
    {

        $sections = new Collection();

        $c_data = $this->reportingUsers();
        // dump(count($c_data));
        if (count($c_data) > 0) {
            foreach ($c_data as $section) {
                $sections->push($section);
                //dump($section);
                $c_data_ = $section->reportingUsers();
                //  echo "ll".count($c_data_);
                //dump($this->sections);
                if (count($c_data_) > 0) {
                    //echo "pp";
                    $cc = $section->getAllReportingUsersObj();
                    //dump($cc);
                    $sections = $sections->merge($cc);
                } else {
                    // $this->sections = $this->sections->merge($section->getAllChildren(1));
                }

            }
        }
        //dd($this->sections);
        return $sections;
    }
    public function getAllReportingUsersIds()
    {

        $sections = new Collection();

        $c_data = $this->reportingUsers();
        // dump(count($c_data));
        if (count($c_data) > 0) {
            foreach ($c_data as $section) {
                $sections->push($section->id);
                //dump($section);
                $c_data_ = $section->reportingUsers();
                //  echo "ll".count($c_data_);
                //dump($this->sections);
                if (count($c_data_) > 0) {
                    //echo "pp";
                    $cc = $section->getAllReportingUsersIds();
                    //dump($cc);
                    $sections = $sections->merge($cc);
                } else {
                    // $this->sections = $this->sections->merge($section->getAllChildren(1));
                }

            }
        }
        $sections = $sections->toArray();
        //dd($this->sections);
        return $sections;
    }
    public function getDailyWorkPlan()
    {
        return $this->hasMany(DailyWorkPlan::class, 'user_id', 'id');
    }
}
