<?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\Comment;
use App\Models\Department;
use App\Models\Organism;
use App\Models\PatientType;
use App\Models\Quantity;
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 CommentController extends Controller
{

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

    public function __construct(Comment $comment, Department $department){
        $this->model = $comment;
        $this->department = $department;
        $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_comment'])) return redirect(url('my-profile'));
        $departments = $this->department::query()->where(['lab_id' => Session::get('base_lab_id'), BaseModel::RECORD_STATUS_FIELD => RecordStatusEnum::ACTIVE])->orderBy('name_en','asc')->get();
        $recordStatusCondition = Auth::id()==1 ? [RecordStatusEnum::DELETE, RecordStatusEnum::ACTIVE] : [RecordStatusEnum::ACTIVE];
        $comments = $this->model::with(['sample_type','sample_type.department'])->whereIn(BaseModel::RECORD_STATUS_FIELD, $recordStatusCondition)
            ->where('lab_id', $this->baseLabId)
            ->when(!empty($request->kword), function ($comments) use($request){
                $comments->whereRaw("replace(comment_desc, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT_FIELD,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('comment', ['comments' => $comments, 'departments' => $departments]);
    }

    public function save(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_comment'])) return false;
            $validator = \Validator::make($request->all(), ['comment_desc' => 'required', 'sample_type_id' => 'required']);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('comment.create_fail'), 'errors' => $validator->errors()->all()]);
            foreach ($request->sample_type_id as $sample_type) {
                $comment = $this->model::query()->create([
                    'comment_desc' => $request->comment_desc,
                    'sample_type_id' => $sample_type,
                    'lab_id' => $this->baseLabId
                ]);
            }
            return response()->json(['success' => true, 'message' => __('comment.create_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('comment.create_fail'), 'errors' => $e->getMessage()]);
        }
    }

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

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

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


}
