<?php

namespace App\Http\Controllers;

use App\Enums\RecordStatusEnum;
use App\Models\BaseModel;
use App\Models\Commune;
use App\Models\District;
use App\Models\Laboratory;
use App\Models\Province;
use App\Models\UserLabCover;
use App\Models\Village;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

abstract class Controller
{
    function getAccessAbleLabs(){
        $recordStatusCondition = Auth::id()==1 ? [RecordStatusEnum::DELETE, RecordStatusEnum::ACTIVE] : [RecordStatusEnum::ACTIVE];
        $labs = Laboratory::query()->whereIn(BaseModel::RECORD_STATUS_FIELD, $recordStatusCondition)->get();
        if(Auth::id()!=1){
            $labIds = UserLabCover::query()->where('user_id', Auth::id())->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)->pluck('lab_id')->toArray();
            $labs = $labs->whereIn('id', $labIds);
        }
        return $labs;
    }

    function province(){
        return Province::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)->get(['id','name_en','name_kh']);
    }

    function district($province_id = null){
        return District::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->when(! is_null($province_id), function ($district) use($province_id){
                $district->where('province_id', $province_id);
            })->orderBy('name_en','asc')->get(['id','name_en','name_kh']);
    }

    function commune($district_id = null){
        return Commune::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->when(!is_null($district_id), function ($commune) use($district_id){
                $commune->where('district_id', $district_id);
            })->orderBy('name_en','asc')->get(['id','name_en','name_kh']);
    }

    function village($commune_id = null){
        return Village::query()->where(BaseModel::RECORD_STATUS_FIELD, RecordStatusEnum::ACTIVE)
            ->when(!is_null($commune_id), function ($village) use($commune_id){
                $village->where('commune_id', $commune_id);
            })->orderBy('name_en','asc')->get(['id','name_en','name_kh']);
    }

    public function location(){
        // discount_type = [1 => '%', 2 => 'USD']
        return Laboratory::query()->find(Session::get('base_lab_id'))->only([
            'name_en',
            'name_kh',
            'short_name',
            'sample_number',
            'patient_code',
            'verify_label',
            'technician_label',
            'discount_type',
            'logo',
            'address_kh',
            'address_en',
            'phone_number',
            'email',
            'shareable_lab_result',
            'result_header_id',
            'show_result_footer',
            'abnormal_result_font_weight',
            'abnormal_text_color',
            'exchange_rate',
            'lab_category_id',
            'currency',
            'is_hide_inv_discount',
            'is_hide_comission_percentage',
            'invoice_template_id',
            'alternative_logo'
        ]);
    }

//    public function switchLanguage($locale)
//    {
//        if (array_key_exists($locale, config('app.available_locales'))) {
//            session(['locale' => $locale]);
//        }
//        return redirect()->back();
//    }

}
