<commit_before><?php namespace App\Http\Controllers\API;

use App\Models\AnalyticsEvent;
use App\Models\ApplicationNote;
use App\Models\GroupMessage;
use App\Models\InterestSignup;
use App\Models\User;
use App\Http\Controllers\Controller;
use App\Services\Notifier;
use Auth;
use Validator;
use Illuminate\Http\Request;
use App\Models\Application;
use Input;
use Log;
use DB;
use App\Models\Team;
use App\Models\Role;
use App\Models\Event;
use App\Models\ApplicationRating;
use AWS;
use Carbon\Carbon;
use \Eluceo\iCal\Component\Calendar;

class ExecController extends Controller {

	public function __construct() {
       // $this->middleware('jwt.auth', ['except' => ['generateCalendar']]);
	}
    public function getInterestData() {
        return InterestSignup::all();
    }
	public function getHackers() {
		$users = User::whereHas('roles', function($q)
		{
		    $q->where('name', 'hacker');
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user['application']['rating_info']=$user->application->ratingInfo();
		}
		return $users;
	}
	public function getHackersBulk(Request $request)
	{
		$ids = $request->all();
		$users = User::whereHas('roles', function($q) use ($ids)
		{
		    $q->where('name', 'hacker')->whereIn('id',$ids);
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user['application']['rating_info']=$user->application->ratingInfo();
		}
		return $users;
	}
	public function putHackersBulk(Request $request)
	{
		$ids = $request->all()['hackers'];
		$decision = $request->all()['decision'];
		$users = User::whereHas('roles', function($q) use ($ids)
		{
		    $q->where('name', 'hacker')->whereIn('id',$ids);
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user->application->decision=$decision;
			$user->application->save();
		}
		return $users;
	}
	public function getUsers() {
        $users = User::all();
        foreach ($users as $eachUser) {
            $eachUser->roles = $eachUser->roles()->lists('name');
        }
        return $users;
	}
	public function getUser($id)
	{
		$user = User::find($id);
		$application = null;
		if($user->hasRole('hacker'))
			$application=$user->getApplication(true);
		return [
			'user'=>$user,
			'application'=>$application,
			'roles'=>$user->roles()->lists('name'),
			'isHacker'=>$user->hasRole('hacker'),
			];
	}
	public function getUserAnalytics($id)
	{
		$events = AnalyticsEvent::where('user_id',$id)->get();
		return $events;
	}
	public function doAction(Request $request, $id)
	{
		$user = User::find($id);
		switch ($request->action) {
			case "password-reset":
				$user->sendPasswordResetEmail();
				return ['status'=>'ok'];
				break;
			case "check-in":
				//todo
				return ['status'=>'error','message'=>'todo'];
				break;
		}
	}
	public function getGroupMessages()
	{
		return GroupMessage::all();
	}
	public function sendGroupMessage(Request $request)
	{
		switch ($request->group) {
			case "all":
				$roles = ['exec','hacker','sponsor'];
				break;
			case "hackers":
				$roles = ['exec','hacker'];
				break;
			case "sponsors":
				$roles = ['exec','sponsor'];
				break;
			case "exec":
				$roles = ['exec'];
				break;
		}
		$users = User::whereHas('roles', function($q) use($roles)
		{
			$q->whereIn('name', $roles);
		})->get();

		foreach($users as $u)
		{
			$n = new Notifier($u);
			$n->sendSMS($request->message,'group-message');
		}
		$log = new GroupMessage();
		$log->group = $request->group;
		$log->message = $request->message;
		$log->num_recipients = $users->count();
		$log->save();
		return ['status'=>'ok','message'=>'message sent to'.$users->count().'users'];
	}
	public function getNextApplicationID()
	{
		$user = Auth::user();
		foreach(Application::orderBy(DB::raw('RAND()'))->get() as $app)
		{
			//we must find the applications that are completed and have fewer than 3 reviews and that i didn't review
			if($app->completed)
				if($app->reviews < 3)
					if(!ApplicationRating::where('application_id',$app->id)->where('user_id',$user->id)->first())
						return($app->id);
		}
		return null;

	}
	public function getApplication($id)
	{
		$user = Auth::user();
		$app = Application::with('user','school','team','notes.user')->find($id);
        
        
		$app->resumeURL = GeneralController::resumeUrl($app->user->id,'get');
		$app->myrating = ApplicationRating::where('application_id',$id)->where('user_id',$user->id)->first();
        $app->github_summary = $app->getGithubSummary();
		return $app;
	}
	public function rateApplication(Request $request, $id)
	{
		$user = Auth::user();
		$rating = $request->all()['rating'];
		$ranking = ApplicationRating::firstOrNew(['application_id'=>intval($id),'user_id'=>$user->id]);<commit_msg>return validation details to exec application detail<commit_after><?php namespace App\Http\Controllers\API;

use App\Models\AnalyticsEvent;
use App\Models\ApplicationNote;
use App\Models\GroupMessage;
use App\Models\InterestSignup;
use App\Models\User;
use App\Http\Controllers\Controller;
use App\Services\Notifier;
use Auth;
use Validator;
use Illuminate\Http\Request;
use App\Models\Application;
use Input;
use Log;
use DB;
use App\Models\Team;
use App\Models\Role;
use App\Models\Event;
use App\Models\ApplicationRating;
use AWS;
use Carbon\Carbon;
use \Eluceo\iCal\Component\Calendar;

class ExecController extends Controller {

	public function __construct() {
       // $this->middleware('jwt.auth', ['except' => ['generateCalendar']]);
	}
    public function getInterestData() {
        return InterestSignup::all();
    }
	public function getHackers() {
		$users = User::whereHas('roles', function($q)
		{
		    $q->where('name', 'hacker');
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user['application']['rating_info']=$user->application->ratingInfo();
		}
		return $users;
	}
	public function getHackersBulk(Request $request)
	{
		$ids = $request->all();
		$users = User::whereHas('roles', function($q) use ($ids)
		{
		    $q->where('name', 'hacker')->whereIn('id',$ids);
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user['application']['rating_info']=$user->application->ratingInfo();
		}
		return $users;
	}
	public function putHackersBulk(Request $request)
	{
		$ids = $request->all()['hackers'];
		$decision = $request->all()['decision'];
		$users = User::whereHas('roles', function($q) use ($ids)
		{
		    $q->where('name', 'hacker')->whereIn('id',$ids);
		})->with('application','application.school')->get();
		foreach ($users as $user) {
			$user->application->decision=$decision;
			$user->application->save();
		}
		return $users;
	}
	public function getUsers() {
        $users = User::all();
        foreach ($users as $eachUser) {
            $eachUser->roles = $eachUser->roles()->lists('name');
        }
        return $users;
	}
	public function getUser($id)
	{
		$user = User::find($id);
		$application = null;
		if($user->hasRole('hacker'))
			$application=$user->getApplication(true);
		return [
			'user'=>$user,
			'application'=>$application,
			'roles'=>$user->roles()->lists('name'),
			'isHacker'=>$user->hasRole('hacker'),
			];
	}
	public function getUserAnalytics($id)
	{
		$events = AnalyticsEvent::where('user_id',$id)->get();
		return $events;
	}
	public function doAction(Request $request, $id)
	{
		$user = User::find($id);
		switch ($request->action) {
			case "password-reset":
				$user->sendPasswordResetEmail();
				return ['status'=>'ok'];
				break;
			case "check-in":
				//todo
				return ['status'=>'error','message'=>'todo'];
				break;
		}
	}
	public function getGroupMessages()
	{
		return GroupMessage::all();
	}
	public function sendGroupMessage(Request $request)
	{
		switch ($request->group) {
			case "all":
				$roles = ['exec','hacker','sponsor'];
				break;
			case "hackers":
				$roles = ['exec','hacker'];
				break;
			case "sponsors":
				$roles = ['exec','sponsor'];
				break;
			case "exec":
				$roles = ['exec'];
				break;
		}
		$users = User::whereHas('roles', function($q) use($roles)
		{
			$q->whereIn('name', $roles);
		})->get();

		foreach($users as $u)
		{
			$n = new Notifier($u);
			$n->sendSMS($request->message,'group-message');
		}
		$log = new GroupMessage();
		$log->group = $request->group;
		$log->message = $request->message;
		$log->num_recipients = $users->count();
		$log->save();
		return ['status'=>'ok','message'=>'message sent to'.$users->count().'users'];
	}
	public function getNextApplicationID()
	{
		$user = Auth::user();
		foreach(Application::orderBy(DB::raw('RAND()'))->get() as $app)
		{
			//we must find the applications that are completed and have fewer than 3 reviews and that i didn't review
			if($app->completed)
				if($app->reviews < 3)
					if(!ApplicationRating::where('application_id',$app->id)->where('user_id',$user->id)->first())
						return($app->id);
		}
		return null;

	}
	public function getApplication($id)
	{
		$user = Auth::user();
		$app = Application::with('user','school','team','notes.user')->find($id);
        
        
		$app->resumeURL = GeneralController::resumeUrl($app->user->id,'get');
		$app->myrating = ApplicationRating::where('application_id',$id)->where('user_id',$user->id)->first();
		$app['validation'] = $app->validationDetails();
        $app->github_summary = $app->getGithubSummary();
		return $app;
	}
	public function rateApplication(Request $request, $id)
	{
		$user = Auth::user();
		$rating = $request->all()['rating'];
		$ranking = ApplicationRating::firstOrNew(['application_id'=>intval($id),'user_id'=>$user->id]);
{'fraction_non_alphanumeric': 0.20358289323806567, 'fraction_numerical': 0.0004142073107590349, 'mean_word_length': 4.344770337576093, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 162, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2878852', 'n_tokens_mistral': 3552, 'n_tokens_neox': 3206, 'n_words': 597}