<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Validator;
use App\Credential;
use App\Http\Requests\CredentialRequest;
use Illuminate\Support\Facades\Mail;

class CredentialController extends Controller
{

	public function index(Request $request)
    {
		
        $credentials = Credential::get();

        return response()->json($credentials, 200);

    }

    public function store(CredentialRequest $request)
    {
		
		$unique = Credential::where('email', $request->email)->get();
		
		if (count($unique)) {
			return response()->json([
					'title' => 'Credential',
					'message' => 'Email already exists. Try another one.',
				], 422);
		}

        $credential = $this->_save($request);
		
		if ($credential) {
		
			Mail::to($credential->email)->send(new \App\Mail\Credential($request->email, $request->password));

			return response()->json([
					'title' => 'Credential',
					'message' => 'Credential was added successfully.',
					'object' => $credential
				], 201);
				
		} else {
			return response()->json([
					'title' => 'Credential',
					'message' => 'It was not possible to add the Credential.'
				], 500);
		}

    }

    public function show($id)
    {

        $credential = Credential::with('employee')->find($id);

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

        return response()->json($credential, 200);

    }

    public function update(CredentialRequest $request, $id)
    {
		
		$unique = Credential::where('email', $request->email)
								->where('employee_id', '!=', $request->employee_id)
									->get();
		
		if (count($unique)) {
			return response()->json([
					'title' => 'Credential',
					'message' => 'Email already exists. Try another one.',
				], 422);
		}

        $credential = $this->_save($request, $id);
		
		if ($credential) {
		
			if ($credential->active) {
				Mail::to($credential->email)->send(new \App\Mail\Credential($request->email, $request->password));
			}

			return response()->json([
					'title' => 'Credential',
					'message' => 'Credential was edited successfully.',
					'object' => $credential
				], 200);
				
		} else {
			return response()->json([
					'title' => 'Credential',
					'message' => 'It was not possible to edit the Credential.'
				], 500);
		}

    }

    public function destroy($id)
    {
        
        $credential = Credential::find($id);

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

        $credential->delete();
		
		return response()->json([
				'title' => 'Credential',
				'message' => 'Credential was deleted successfully.'
			], 200);

    }

    private function _save(Request $request, $id = null) 
    {

        if ($id) {

            $credential = Credential::find($id);

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

        } else {

            $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 = $request->employee_id;
		$credential->email = $request->email;
		$credential->password = $request->password;
		$credential->change_password = $request->change_password;
		$credential->active = $request->active;
		
        $credential->roles = array(
                'web_access' => $request->roles['web_access'],
                'mobile_access' => $request->roles['mobile_access'],
				'mobile_access_others' => $request->roles['mobile_access_others'],
				'is_manager' => $request->roles['is_manager']
            );

        if ($credential->save()) {
			return $credential;
		}

        return null;

    }

}
