<?php

namespace App\Http\Controllers;

use App\Models\AppSession;
use App\Models\CompanyClient;
use App\Models\Note;
use App\Models\NoteTemplate;
use App\Models\Pro;
use App\Models\ProProAccess;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Http;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $performer = null;

    protected $pro = null;

    protected $pros = null;

    public function __construct()
    {
        $this->performer = get_current_session();
        if($this->performer && $this->performer->pro) {
            $this->pro = $this->performer->pro;
            view()->share('pro', $this->performer->pro);
        }
        view()->share('performer', $this->performer);

        $this->pros = [];

        /*$this->pros =  Pro::all();
        if($this->pro && $this->pro->pro_type != 'ADMIN'){
            $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
            $accessibleProIds = [];
            foreach($accessiblePros as $accessiblePro){
                $accessibleProIds[] = $accessiblePro->id;
            }
            $accessibleProIds[] = $this->pro->id;

            $this->pros = Pro::whereIn('id', $accessibleProIds)->get();
        }*/

        view()->share('pros',$this->pros);
        // view()->share('notes', Note::all());

        // $noteTemplates = NoteTemplate::all();
        // view()->share('noteTemplates', $noteTemplates);

        $initiatives = ['LHI', 'BHI'];
        view()->share('intiatives', $initiatives);

        $rmCodes = ['RMB','RM20', 'RM40','RM60'];

        view()->share('rmCodes', $rmCodes);
    }

    public function performer(){
        $sessionKey = Cookie::get('sessionKey');
        if ($sessionKey == null){
            throw new \Exception('No session key in cookie.');
        }
        $performer = AppSession::where('session_key', $sessionKey)->first();
        return $performer;
    }

    public function getMyClientIds(){
        if($this->pro == null){
            return [];
        }
        return $this->pro->getMyClientIds();
    }

    public function pass($data = null): array
    {
        return [
            'success' => true,
            'data' => $data,
        ];
    }
    public function fail($message): array
    {
        return [
            'success' => false,
            'message' => $message
        ];
    }

    public function filterMultiQuery(Request $request, $query, $columnName, $keyName, $valueName1, $valueName2, $onlyDate = true) {
        switch($request->input($keyName)) {
            case 'EXACTLY':
                if($request->input($valueName1)) {
                    if($onlyDate){
                        $query->whereDate($columnName, $request->input($valueName1));
                    }else{
                        $query->where($columnName, $request->input($valueName1));
                    }
                    
                }
                break;
            case 'LESS_THAN':
                if($request->input($valueName1)) {
                    $query->where($columnName, '<', $request->input($valueName1));
                }
                break;
            case 'GREATER_THAN':
                if($request->input($valueName1)) {
                    $query->where($columnName, '>', $request->input($valueName1));
                }
                break;
            case 'BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->where($columnName, '>=', $request->input($valueName1))
                        ->where($columnName, '<=', $request->input($valueName2));
                }
                break;
            case 'NOT_BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->where(function ($q) use ($request, $columnName, $valueName1, $valueName2) {
                            $q->where($columnName, '<', $request->input($valueName1))
                                ->orWhere($columnName, '>', $request->input($valueName2));
                        });
                }
                break;
        }
    }
    public function filterSimpleQuery(Request $request, $query, $columnName, $valueName) {
        if($request->input($valueName)) {
            $query->where($columnName, $request->input($valueName));
        }
    }

    // TODO move to utility
    public function callJavaApi($endPoint, $data)
    {
        $sessionKey = Cookie::get('sessionKey');
        $url =  config('stag.backendUrl') . $endPoint;
        
        $response = Http::asForm()
            ->withHeaders([
                'sessionKey' => $sessionKey
            ])
            ->post($url, $data)
            ->json();
        return $response;
    }

    public function getDefaultCompanyClientFromSession($patient){
        $defaultCompanyClientUid = session()->get('DEFAULT_COMPANY_CLIENT_UID_'.$patient->id);
        return CompanyClient::where('uid', $defaultCompanyClientUid)->first();
    }

}
