<?php

namespace App\Http\Controllers;
use App\Enums\RecordStatusEnum;
use App\Http\Controllers\Helper\GlobalController;
use App\Models\BaseModel;
use App\Models\Concept;
use App\Models\Pathogen;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Session;


class PathogenController extends Controller
{
    protected $model;
    protected $baseOrganizationId;
    protected $conceptModel;

    public function __construct(){
        $this->baseOrganizationId = Session::get('base_organization_id');
        $this->base = Session::get('base_organization');
        $this->model = new Pathogen();
        $this->conceptModel = new Concept();
    }

    public function index(Request $request){
        $concepts = $this->conceptModel::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)->get();
        $pathogens = $this->model::with(['riskGroup', 'bsl', 'pathogenType'])->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->when(!empty($request->kword), function ($query) use($request){
                $query->whereRaw("replace(scientific_name, ' ','') like '%".str_replace(" ","",$request->kword)."%'")
                    ->orWhereRaw("replace(common_name, ' ','') like '%".str_replace(" ","",$request->kword)."%'")
                    ->orWhereRaw("replace(strain, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT,'desc')->paginate(config('nrml.pagination.perpage', 10));
        return view('pathogen', ['pathogens' => $pathogens, 'concepts' => $concepts]);
    }

    public function save(Request $request){
        try {

            $validator = \Validator::make($request->all(), [
                'pathogen_category_concept_id' => 'required',
                'scientific_name' => 'required',
                'common_name' => 'required',
                'risk_group_concept_id' => 'required',
                'biosafety_level_concept_id' => 'required',
            ]);
            if ($validator->fails()) {
                return response()->json(['success' => false, 'message' => __('building.create_fail'), 'errors' => $validator->errors()->all()]);
            }
            $data = array(
                'pathogen_category_concept_id' => $request->pathogen_category_concept_id,
                'scientific_name' => $request->scientific_name,
                'common_name' => $request->common_name,
                'risk_group_concept_id' => $request->risk_group_concept_id,
                'biosafety_level_concept_id' => $request->biosafety_level_concept_id,
                'infectious_dose' => $request->infectious_dose,
                'transmission_mode' => $request->transmission_mode,
                'storage_requirement' => $request->storage_requirement,
                'min_temp' => $request->min_temp,
                'max_temp' => $request->max_temp,
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => Auth::id()
            );
            $this->model::query()->insert($data);
            return response()->json(['success' => true, 'message' => __('laboratory.create_success')]);
        }
        catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('laboratory.create_fail'), 'errors' => $e->getMessage()]);
        }
    }


    public function update(Request $request){
        try {
            $validator = \Validator::make($request->all(), [
                'pathogen_category_concept_id' => 'required',
                'scientific_name' => 'required',
                'common_name' => 'required',
                'risk_group_concept_id' => 'required',
                'biosafety_level_concept_id' => 'required',
            ]);
            if ($validator->fails()) return response()->json(['errors' => $validator->errors()->all()]);
            $data = array(
                'pathogen_category_concept_id' => $request->pathogen_category_concept_id,
                'scientific_name' => $request->scientific_name,
                'common_name' => $request->common_name,
                'risk_group_concept_id' => $request->risk_group_concept_id,
                'biosafety_level_concept_id' => $request->biosafety_level_concept_id,
                'infectious_dose' => $request->infectious_dose,
                'transmission_mode' => $request->transmission_mode,
                'storage_requirement' => $request->storage_requirement,
                'min_temp' => $request->min_temp,
                'max_temp' => $request->max_temp,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => Auth::id()
            );
            $this->model::query()->where('id', $request->pathogen_id)->update($data);
            return response()->json(['success' => true, 'message' => __('laboratory.update_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('laboratory.update_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function get(Request $request){
        try{
            $lab = $this->model::query()->where('id', $request->pathogen_id)->get()->first();
            return response()->json(['success' => true, 'message' => __('laboratory.get_success'), 'data' => $lab]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('laboratory.get_fail'), 'errors' => $e->getMessage()]);
        }

    }

    public function delete(Request $request){
        try{
            $this->model::query()->where('id', $request->pathogen_id)->update(array(
                BaseModel::DELETED_AT_FIELD => date('Y-m-d H:i:s'),
                BaseModel::DELETED_BY_FIELD => Auth::id(),
                BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::DELETE
            ));
            return response()->json(['success' => true, 'message' => __('laboratory.delete_success')]);
        }
        catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('laboratory.delete_fail'), 'errors' => $e->getMessage()]);
        }

    }

    public function restore(Request $request){
        try{
            $this->model::query()->where('id', $request->pathogen_id)->update(array(BaseModel::RECORD_STATUS_FIELD=> RecordStatusEnum::ACTIVE));
            return response()->json(['success' => true, 'message' => __('laboratory.restore_success')]);
        }
        catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('laboratory.restore_fail'), 'errors' => $e->getMessage()]);
        }
    }


}
