<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

use App\Models\{Account,Invoice,Payment,Service,Service\Adsl,Service\Voip,User};

class SearchController extends Controller
{
	/**
	 * Search from the Application Dashboard.
	 *
	 * @param Request $request
	 * @return Response
	 */
	public function search(Request $request)
	{
		// If there isnt a term value, return null
		if (! $request->input('term'))
			return [];

		$result = collect();
		$accounts = ($x=Auth::user()->all_accounts())->pluck('id');
		$users = $x->transform(function($item) { return $item->user;});

		# Look for User
		foreach (User::Search($request->input('term'))
			->whereIN('id',$users->pluck('id'))
			->orderBy('lastname')
			->orderBy('firstname')
			->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s %s',$o->aid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
		}

		# Look for Account
		foreach (Account::Search($request->input('term'))
			         ->whereIN('user_id',$users->pluck('id'))
			         ->orderBy('company')
			         ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s %s',$o->aid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
		}

		# Look for a Service
		foreach (Service::Search($request->input('term'))
			         ->whereIN('account_id',$accounts)
			         ->orderBy('id')
			         ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s (%s)',$o->name,$o->sid),'value'=>'/u/service/'.$o->id,'category'=>'Services']);
		}

		# Look for an Invoice
		foreach (Invoice::Search($request->input('term'))
			         ->whereIN('account_id',$accounts)
			         ->orderBy('id')
			         ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s: %s',$o->sid,$o->account->name),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
		}

		# Look for an ADSL/NBN Service
		foreach (Adsl::Search($request->input('term'))
			 ->whereIN('account_id',$accounts)
			 ->orderBy('service_number')
			 ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s (%s)',$o->service_name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'Broadband']);
		}

		# Look for an VOIP Service
		foreach (Voip::Search($request->input('term'))
			         ->whereIN('account_id',$accounts)
			         ->orderBy('service_number')
			         ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s (%s)',$o->service_name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'VOIP']);
		}

		# Look for Domain Name
		foreach (Service\Domain::Search($request->input('term'))
			         ->whereIN('account_id',$accounts)
			         ->orderBy('domain_name')
			         ->limit(10)->get() as $o)
		{
			$result->push(['name'=>sprintf('%s (%s)',$o->service_name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'Domains']);
		}

		if (Gate::any(['wholesaler'],new Payment)) {
			# Look for Payments
			foreach (Payment::Search($request->input('term'))
				         ->whereIN('account_id',$accounts)
				         ->limit(10)->get() as $o)
			{
				$result->push(['name'=>sprintf('%s ($%s)',$o->id,number_format($o->total,2)),'value'=>'/a/payment/addedit/'.$o->id,'category'=>'Payments']);
			}
		}

		return $result;
	}
}