<?php

namespace App\Http\Controllers;
use App\Enums\RecordStatusEnum;
use App\Http\Controllers\Helper\GlobalController;
use App\Http\Requests\TestSampleCreateRequest;
use App\Http\Resources\AntibioticResource;
use App\Models\Antibiotic;
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 AntibioticController extends Controller
{

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

    public function __construct(Antibiotic $antibioticModel){
        $this->model = $antibioticModel;
        $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_antibiotic'])) return redirect(url('my-profile'));
        $antibiotics = $this->model::with(['lab'])->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->when(!empty($request->kword), function ($antibiotics) use($request){
                $antibiotics->whereRaw("replace(name_en, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT_FIELD,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('antibiotic', ['antibiotics' => AntibioticResource::collection($antibiotics)]);
    }

    public function save(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_antibiotic'])) return false;
            $validator = \Validator::make($request->all(), ['name_en' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('antibiotic.create_fail'), 'errors' => $validator->errors()->all()]);
            $antibiotic  = $this->model::query()->create([ 'name_en' => $request->name_en, 'weight' => $request->weight]);
            return response()->json(['success' => true, 'message' => __('antibiotic.create_success'), 'data' => [$antibiotic]]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('antibiotic.create_fail'), 'errors' => $e->getMessage()]);
        }
    }

    public function update(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['update_antibiotic'])) return false;
            $validator = \Validator::make($request->all(), ['name_en' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('antibiotic.update_fail'), 'errors' => $e->getMessage()]);
            $data = array(
                'name_en' => $request->name_en,
                'weight' => $request->weight
            );
            $this->model::query()->where('id', $request->uid)->update($data);
            return response()->json(['success' => true, 'message' => __('antibiotic.update_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('antibiotic.update_fail'), 'errors' => $e->getMessage()]);
        }
    }

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

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

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


}
