<?php

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

class TestGroupController extends Controller
{

    protected $model;
    protected $testGroupDetailModel;

    protected $baseLabId;
    protected $labs;
    protected $base;

    public function __construct(TestGroup $model,  TestGroupDetail $testGroupDetailModel)
    {
        $this->model = $model;
        $this->testGroupDetailModel = $testGroupDetailModel;
        $this->baseLabId = Session::get('base_lab_id');
        $this->base = Session::get('base_lab');
    }

    public function index(Request $request){
        return null;
    }

    public function save(Request $request){
        DB::beginTransaction();
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['create_test_group'])) return false;
            $validator = \Validator::make($request->all(), ['name_en' => 'required','test_sample_ids' => 'required']);
            if ($validator->fails()){ return response()->json(['errors'=>$validator->errors()->all()]);}
            $testGroup  = $this->model::query()->create(['group_name' => $request->name_en, 'lab_id' => $this->baseLabId]);
            foreach ($request->test_sample_ids as $v){
                $this->testGroupDetailModel::query()->create([
                    'test_group_id' => $testGroup->id,
                    'test_sample_id' => $v,
                    'lab_id' => $this->baseLabId
                ]);
            }
            DB::commit();
            return response()->json(['success'=> true , 'data' => [$testGroup]]);
        } catch (\Exception $e)
        {
            DB::rollBack();
            return response()->json(['success'=> false , 'errors' => [$e->getMessage()]]);
        }
    }

    public function get($id){
        try{
            $testGroupItems = $this->testGroupDetailModel::query()->where(['test_group_id' => $id, 'lab_id' => $this->baseLabId, BaseModel::RECORD_STATUS_FIELD=>RecordStatusEnum::ACTIVE])->get()->pluck('test_sample_id')->toArray();
            return response()->json(['success' => true, 'data' => $testGroupItems]);
        } catch (\Exception $e)
        {
            return response()->json(['success'=> false , 'errors' => [$e->getMessage()]]);
        }
    }

    public function delete(Request $request){
        try{
            if(!GlobalController::user_can(Auth::user()->role_id, ['delete_test_group'])) return false;
            $this->model::query()->where(['id' => $request->id, 'lab_id' => $this->baseLabId])->update([BaseModel::RECORD_STATUS_FIELD=>RecordStatusEnum::DELETE]);
            return response()->json(['success' => true, 'message' => 'Test group successfully deleted']);
        } catch (\Exception $e)
        {
            return response()->json(['success'=> false , 'message' => 'Failed while deleting test group. Please try again.', 'errors' => [$e->getMessage()]]);
        }
    }


}
