<?php

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

class SampleSourceController extends Controller
{

    protected $sampleSourceModel;
    protected $baseLabId;
    protected $base;
    public function __construct(SampleSource $sampleSourceModel){
        $this->sampleSourceModel = $sampleSourceModel;
        $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_sample_source'])) return redirect(url('my-profile'));
        $sampleSources = $this->sampleSourceModel::with(['lab'])->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->where('lab_id', $this->baseLabId)
            ->when(!empty($request->kword), function ($sampleSources) use($request){
                $sampleSources->whereRaw("replace(name_en, ' ','') like '%".str_replace(" ","",$request->kword)."%'");
            })->orderBy(BaseModel::CREATED_AT,'desc')->paginate(config('labis.pagination.perpage', 10));
        return view('sample_source', ['sampleSources' => $sampleSources, 'labs' => parent::getAccessAbleLabs()]);
    }

    public function save(Request $request){
        try {
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_sample_source'])) return false;
            $validator = \Validator::make($request->all(), [
                'name_en' => 'required',
                'name_kh' => 'nullable'
            ]);
            if ($validator->fails()) return response()->json(['success' => false, 'message' => __('sample_source.create_fail'), 'errors' => $validator->errors()->all()]);

            $new = $this->sampleSourceModel::query()->firstOrNew(['name_en' => $request->name_en, 'lab_id' => $this->baseLabId]);
            $new->name_en = $request->name_en;
            $new->is_default = $request->is_default;
            $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' => __('sample_source.create_success')]);
        } catch (\Exception $e){
            return response()->json(['success' => false, 'message' => __('sample_source.create_fail'), 'errors' => $e->getMessage()]);
        }
    }

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

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

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

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


}
