<?php

namespace App\Http\Controllers;
use App\Enums\RecordStatusEnum;
use App\Http\Controllers\Helper\GlobalController;
use App\Http\Requests\TestSampleCreateRequest;
use App\Models\BaseModel;
use App\Models\Organism;
use App\Models\PatientType;
use App\Models\SampleType;
use App\Models\Test;
use App\Models\TestNormalValue;
use App\Models\TestSample;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class OrganismController extends Controller
{

    protected $model;
    protected $baseLabId;
    protected $labs;

    public function __construct(Organism $organism){
        $this->model = $organism;
        $this->baseLabId = Session::get('base_lab_id');
        $this->base = Session::get('base_lab');
    }

    public function index(Request $request){
        if(!GlobalController::user_can(Auth::user()->role_id, ['view_organism'])) return redirect(url('my-profile'));
        $organisms = $this->model::with(['lab'])->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            //->where('lab_id', $this->baseLabId)
            ->when(!empty($request->kword), function ($organisms) use($request){
                $organisms->whereRaw("replace(name_en, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT_FIELD,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('organism', ['organisms' => $organisms]);
    }

    public function save(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_organism'])) return false;
            $validator = \Validator::make($request->all(), ['name_en' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('organism.create_fail'), 'errors' => $validator->errors()->all()]);
            $new  = $this->model::query()->firstOrNew(['name_en' => $request->name_en]);
            $new->name_en = $request->name_en;
            $new->weight = $request->weight;
            $new->is_bold = $request->is_bold;
            $new->lab_id =  $this->baseLabId;
            $new->created_at = date('Y-m-d H:i:s');
            $new->created_by = Auth::id();
            $new->record_status_id = RecordStatusEnum::ACTIVE;
            $new->save();
            return response()->json(['success' => true, 'message' => __('organism.create_success'), 'data' => [$new]]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('organism.create_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function update(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['update_organism'])) return false;
            $validator = \Validator::make($request->all(), ['name_en' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('organism.update_fail'), 'errors' => $validator->errors()->all()]);
            $data = array(
                'name_en' => $request->name_en,
                'name_kh' => $request->name_kh,
                'is_bold' => $request->is_bold,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => Auth::id()
            );
            $this->model::query()->where('id', $request->uid)->update($data);
            return response()->json(['success' => true, 'message' => __('organism.update_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('organism.update_fail'), 'errors' => $e->getMessage()]);
        }
    }

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

    public function delete(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['delete_organism'])) return false;
            $this->model::query()->where('id', $request->uid)->update(array( BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::DELETE));
            return response()->json(['success' => true, 'message' => __('organism.delete_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('organism.delete_fail'), 'errors' => $e->getMessage()]);
        }
    }

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


}
