<?php

namespace App\Repositories\Core\Auth;

use App\Models\Core\Auth\Team;
use App\Models\Core\Auth\TeamUser;
use App\Repositories\Users;
use App\Traits\Model\Basic;
use Laratrust\Traits\LaratrustUserTrait;

class Teams
{
    use Basic, LaratrustUserTrait;

    public static function getTeamsByUser($user_id = false)
    {
        $user_id = $user_id ? $user_id : Users::getId();

        return TeamUser::byUser($user_id);
    }

    public static function getUsersByTeam($id)
    {
        return TeamUser::byTeam($id)->get();
    }

    public static function getUsersIdByTeam($id)
    {
        return self::getUsersByTeam($id)->pluck('user_id');
    }

    public static function getUsersByTeam2($id)
    {
        return Team::find($id)->users();
    }

    public static function getByName($name)
    {
        return Team::where('name', $name)->first();
    }

    public static function delete($id)
    {
        return Team::destroy($id);
    }

    public static function destroyBySociete($id)
    {
        $teams = Team::bySociete($id)->get();
        foreach ($teams as $team) {
            self::delete($team->id);
        }
    }

    public static function getModel()
    {
        return Team::query();
    }
}
