<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models;
use Session;


class SystemCredentialsController extends Controller{



  public function index(){
      $data = [
          'page_title' => 'Credentials',
      ];
      return view('sys_credentials.index', $data);

  }
  public function getCredListJson(Request $request){
      //$this->log_query();
      $credentials_arr = \DB::table('sys_credentials')
          ->join('auth_users AS userModify', 'userModify.id', '=', 'sys_credentials.last_modified_by_id')
          ->join('auth_users AS userCreate', 'userCreate.id', '=', 'sys_credentials.created_by_id')
          ->join('clients', 'clients.id', '=', 'sys_credentials.client_id')
          ->join('network_operators', 'network_operators.id', '=', 'sys_credentials.network_operator_id')
          ->select('sys_credentials.id', 'clients.name AS clientName',  'network_operators.name AS networkOpsName', 'sys_credentials.service', 'userCreate.name AS created_by_user', 'userModify.name AS modified_by_user', 'sys_credentials.client_id','sys_credentials.network_operator_id', 'sys_credentials.url_endpoint', 'sys_credentials.credentials', 'sys_credentials.expiry_date', 'sys_credentials.remarks', 'sys_credentials.created_at', 'sys_credentials.updated_at')
          ->orderBy('sys_credentials.service', 'ASC')
          ->paginate(20);

      if($request->has('keyword')){
          $keyword = $request->keyword;
          $credentials_arr = \DB::table('sys_credentials')
          ->join('auth_users AS userModify', 'userModify.id', '=', 'sys_credentials.last_modified_by_id')
          ->join('auth_users AS userCreate', 'userCreate.id', '=', 'sys_credentials.created_by_id')
          ->join('clients', 'clients.id', '=', 'sys_credentials.client_id')
          ->join('network_operators', 'network_operators.id', '=', 'sys_credentials.network_operator_id')
          ->select('sys_credentials.id', 'clients.name AS clientName', 'network_operators.name AS networkOpsName', 'sys_credentials.service', 'userCreate.name AS created_by_user',  'userModify.name AS modified_by_user', 'sys_credentials.client_id','sys_credentials.network_operator_id', 'sys_credentials.url_endpoint', 'sys_credentials.credentials', 'sys_credentials.expiry_date', 'sys_credentials.remarks', 'sys_credentials.created_at', 'sys_credentials.updated_at')
          ->whereRaw("sys_credentials.service LIKE '%$keyword%' OR userCreate.name LIKE '%$keyword%' OR sys_credentials.url_endpoint LIKE '%$keyword%' OR sys_credentials.expiry_date LIKE '%$keyword%' OR userModify.name LIKE '%$keyword%' OR sys_credentials.created_at LIKE '%$keyword%' OR sys_credentials.remarks LIKE '%$keyword%' OR sys_credentials.updated_at LIKE '%$keyword%' OR network_operators.name LIKE '%$keyword%'")
          ->orderBy('sys_credentials.service', 'ASC')
          ->paginate(15);
      }
      return response()->json($credentials_arr);
  }



  public function create(){
    $networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->get()->toArray();
    $network_arr = array_pluck($networks, 'network', 'id');
    $services = Models\Service::pluck('name', 'name');
    $clients = Models\Client::pluck('name', 'id');
    $auth_users = Models\SystemUser::pluck('name', 'id');

    $data = [
        'page_title' => 'System Credentialss',
        'clients' => $clients,
        'network_arr' => $network_arr,
        'services' => $services,
        'auth_users' => $auth_users
    ];
    return view('sys_credentials.create', $data);
  }
  public function store(Request $request){
      $request->validate([
          'service' => 'required',
          'client_id' => 'required',
          'network_operator_id' => 'required',
          'url_endpoint' => 'required',
          'credentials' => 'required',
          'expiry_date' => 'required',
          'remarks' => 'sometimes',
      ]);
      // dump($request->all());

      $credentials_arr = $request->except('_token');
      $credentials_arr['last_modified_by_id'] = session('current_user.id');
      $credentials_arr['created_by_id'] = session('current_user.id');

      // dd($credentials_arr);
      // TODO: add user activity here
      $result = Models\SystemCredential::create($credentials_arr);
      Session::flash('success_message', 'Credentials successfully added');
      return redirect(url('systemcreds'));
  }

  public function edit($id){
      $credential = Models\SystemCredential::find($id);

      $networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->get()->toArray();
      $network_arr = array_pluck($networks, 'network', 'id');
      $clients = Models\Client::pluck('name', 'id');
      $auth_users = Models\SystemUser::pluck('name', 'id');


      $data = [
          'page_title' => 'System Credentials',
          'credential' => $credential,
          'clients' => $clients,
          'network_arr' => $network_arr,
          'auth_users' => $auth_users
      ];
      return view('sys_credentials.edit', $data);
  }

  public function update(Request $request, $id){
      $request->validate([
          'friendly_name' => 'required',
          'server_id' => 'required',
          'public_ip_address' => 'sometimes|ip',
          'private_ip_address' => 'sometimes|nullable|ip',
          'main_use' => 'required',
          'remarks' => 'nullable',
          'status' => 'required',
          'server_number' => 'required',
      ]);
      $server = Models\ClickServer::findOrFail($id);
      $server_arr = $request->except('_token', 'server_id', 'server_number');
      $server_arr['last_modified_by_id'] = session('current_user.id');
      $server_arr['server_id'] = $request->server_number;
      $result = $server->update($server_arr);
      Session::flash('success_message', 'Server Details successfully updated');
      return redirect(url('infrastructure/server-list'));
  }

  public function reveal_password($id){
      $server = Models\ClickServer::find($id);
      if ($server) {
          $response_arr = [
              'code' => 1,
              'password' => decrypt($server->root_password)
          ];
      }
      else{
          $response_arr = [
              'code' => 3,
              'msg' => 'not found'
          ];
      }
      return response()->json($response_arr);
  }

}
