<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Validator;
use App\Solicitation;
use App\Company;
use App\Employee;
use App\Credential;
use App\NexmoSettings;
use App\MessageTemplate;
use App\CustomerSource;
use App\Http\Requests\SolicitationRequest;
use Illuminate\Support\Facades\Mail;
use App\Mail\RegisterNewCompany;
use App\Mail\NewCompanyInfo;

class SolicitationController extends Controller
{
	
	public function checkCompany(Request $request, $name) {
		
		$company = Company::where('name', $name)->first();
		
		if ($company) {
			return response()->json(true, 200);
		}
		
		return response()->json(false, 200);
		
	}
	
	public function checkCredential(Request $request, $email) {
		
		$credential = Credential::where('email', $email)->first();
		
		if ($credential) {
			return response()->json(true, 200);
		}
		
		return response()->json(false, 200);
		
	}
	
	public function store(SolicitationRequest $request) {
		
		$company = Company::where('name', $request->company['name'])->first();
		
		if ($company) {
			return response()->json([
				'title' => 'Company Information',
				'message' => 'Company Name is already being used.'
			], 500);
		}
		
		$credential = Credential::where('email', $request->credential['email'])->first();
		
		if ($credential) {
			return response()->json([
				'title' => 'Security Information',
				'message' => 'Email is already being used.'
			], 500);
		}
		
		$information = array(
			'company' => $request->company,
			'employee' => $request->employee,
			'credential' => $request->credential
		);
		
		$solicitation = new Solicitation();
		
		$solicitation->created_at = date('Y-m-d H:i:s');
		$solicitation->updated_at = date('Y-m-d H:i:s');
		$solicitation->key = md5(date('YmdHis') . rand(100000, 999999));
		$solicitation->information = $information;
		
		if ($solicitation->save()) {
			
			$confirmation_link = env('APP_URL_WEB') . '#/solicitation/activate/' . $solicitation->key;
		
			Mail::to($information['company']['contacts']['email'])->send(new RegisterNewCompany($information, $confirmation_link));
			
			return response()->json([
					'title' => 'Register New Company',
					'message' => 'Check your email to confirm the solicitation.'
				], 200);
			
		}
		
		return response()->json([
				'title' => 'Register New Company',
				'message' => 'It was not possible to register the new Company.'
			], 500);
		
	}

    public function show($id)
    {

		$solicitation = Solicitation::where('key', $id)->first();

		if (!$solicitation) {
			return response()->json([
				'title' => 'Register New Company',
				'message' => 'Record not found'
			], 404);
		}

		return response()->json($solicitation, 200);
	
    }
	
	public function activate(Request $request, $key)
	{
		
		if (!$key) {
			return response()->json([
				'title' => 'Register New Company',
				'message' => 'Record not found'
			], 404);
		}
		
		$solicitation = Solicitation::where('key', $key)->first();

		if (!$solicitation) {
			return response()->json([
				'title' => 'Register New Company',
				'message' => 'Record not found'
			], 404);
		}
				
		$company = new Company();

		$company->created_at = date('Y-m-d H:i:s');
		$company->updated_at = date('Y-m-d H:i:s');
		$company->name = $solicitation->information['company']['name'];
		$company->contacts = $solicitation->information['company']['contacts'];
		$company->location = $solicitation->information['company']['location'];
		$company->preferences = array('image' => null);
		$company->active = 1;
		
		if ($company->save()) {
			
			$employee = new Employee();
			
			$employee->created_at = date('Y-m-d H:i:s');
			$employee->updated_at = date('Y-m-d H:i:s');
			$employee->company_id = $company->id;
			$employee->name = $solicitation->information['employee']['name'];
			$employee->contacts = $solicitation->information['employee']['contacts'];
			$employee->location = array();
			$employee->preferences = array('image' => null, 'color' => '#FFFFFF');
			
			if ($employee->save()) {
				
				$credential = new Credential();
				
				$credential->created_at = date('Y-m-d H:i:s');
				$credential->updated_at = date('Y-m-d H:i:s');
				$credential->employee_id = $employee->id;
				$credential->roles = array(
										'web_access' => true, 
										'mobile_access' => true, 
										'mobile_access_others' => true,
										'is_manager' => true
									);
				$credential->email = $solicitation->information['credential']['email'];
				$credential->password = $solicitation->information['credential']['password'];
				$credential->change_password = 0;
				
				if ($credential->save()) {
					
					$setting = new NexmoSettings();
			
					$setting->created_at = date('Y-m-d H:i:s');
					$setting->updated_at = date('Y-m-d H:i:s');
					$setting->company_id = $company->id;
					$setting->virtual_number = env('NEXMO_VIRTUAL_NUMBER');
					$setting->api_key = env('NEXMO_API_KEY');
					$setting->api_secret = env('NEXMO_API_SECRET');
					$setting->active = 1;
					
					if ($setting->save()) {
						
						$template = new MessageTemplate();
			
						$template->created_at = date('Y-m-d H:i:s');
						$template->updated_at = date('Y-m-d H:i:s');
						$template->company_id = $company->id;
						$template->category = env('MESSAGE_TEMPLATE_WELCOME_CATEGORY');
						$template->name = env('MESSAGE_TEMPLATE_WELCOME_NAME');
						$template->subject = 'Welcome To ' . $company->name;
						$template->description = 'Welcome To ' . $company->name;
						$template->attachments = null;
						
						if ($template->save()) {
							
							$arrCustomerSources = array(
								'Facebook',
								'Google Search',
								'Google Adsense',
								'Face To Face',
								'Other',
								'Website',
								'Angieslist',
								'Imported From Quickbooks'
							);
							
							foreach ($arrCustomerSources as $source) {
								
								$customer_source = new CustomerSource();
			
								$customer_source->created_at = date('Y-m-d H:i:s');
								$customer_source->updated_at = date('Y-m-d H:i:s');
								$customer_source->company_id = $company->id;
								$customer_source->name = $source;
								
								$customer_source->save();
								
							}
							
							$solicitation->delete();
							
							Mail::to(ENV('MAIL_ROUTELY'))->send(new NewCompanyInfo($company));
							
							return response()->json([
										'title' => 'Register New Company',
										'message' => 'New Company was activated successfully.'
									], 200);
									
						}
						
					}
					
				}
				
			}
			
		}
		
		return response()->json([
				'title' => 'Register New Company',
				'message' => 'It was not possible to activate the new Company.'
			], 500);
		
	}
	
}
