<?php
namespace App\Http\Controllers\api\v1;
use App\Http\Controllers\Controller;
use App\Models\Activity;
use App\Models\Country;
use App\Models\DressCode;
use App\Models\Education;
use App\Models\Emirate;
use App\Models\GeneticDisease;
use App\Models\ChronicDisease;
use App\Models\GeneticDiseaseVariant;
use App\Models\ChronicDiseaseVariant;
use App\Models\Language;
use App\Models\MaritalStatus;
use App\Models\Nationality;
use App\Models\Page;
use App\Models\Profession;
use App\Models\ReportType;
use App\Models\Sect;
use App\Models\Sport;
use App\Models\Employment;
use App\Models\ChatCategory;
use App\Models\Question;
use App\Models\Answer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GeneralController extends Controller
{

    public function __construct(Request $request)
    {
        if (isset($request->lang)) {
            \App::setLocale($request->lang);
        }
        $this->lang = \App::getLocale();
    }
    public function step1(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];
        $sects = Sect::where('status', 1)->orderBy('id', 'asc')->get();
        $educations = Education::where('status', 1)->orderBy('id', 'asc')->get();
        $professions = Profession::where('status', 1)->orderBy('id', 'asc')->get();
        $o_data['sects'] = convert_all_elements_to_string($sects);
        $o_data['educations'] = convert_all_elements_to_string($educations);
        $o_data['professions'] = convert_all_elements_to_string($professions);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }
    public function step3(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];
        $languages = Language::where('status', 1)->orderBy('id', 'asc')->get();
        $marital_status = MaritalStatus::where('status', 1)->orderBy('id', 'asc')->get();
        $o_data['languages'] = convert_all_elements_to_string($languages);
        $o_data['marital_status'] = convert_all_elements_to_string($marital_status);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }
    public function step5(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];
        $dress_codes = DressCode::where('status', 1)->orderBy('id', 'asc')->get();
        $o_data['dress_codes'] = convert_all_elements_to_string($dress_codes);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }
    public function step6(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];
        $diseases = GeneticDisease::where('status', 1)->orderBy('id', 'asc')->get();
        $o_data['diseases'] = convert_all_elements_to_string($diseases);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function step8(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];
        $activities = Activity::where('status', 1)->orderBy('id', 'asc')->get();
        $sports = Sport::where('status', 1)->orderBy('id', 'asc')->get();
        $o_data['activities'] = convert_all_elements_to_string($activities);
        $o_data['sports'] = convert_all_elements_to_string($sports);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getGeneralList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        if ($request->nationality == 1) {
            $nationalities = Nationality::where('status', 1)->orderBy('id', 'asc')->get();
            $nationalities = $this->addAllOptionIfPreference($nationalities, $request);
            $o_data['nationalities'] = convert_all_elements_to_string($nationalities);
        }

        if ($request->country == 1) {
            $countries = Country::where('status', 1)->orderBy('id', 'asc')->get();
            $countries = $this->addAllOptionIfPreference($countries, $request);
            $o_data['countries'] = convert_all_elements_to_string($countries);
        }

        if ($request->activity == 1) {
            $activities = Activity::where('status', 1)->orderBy('id', 'asc')->get();
            $activities = $this->addAllOptionIfPreference($activities, $request);
            $o_data['activities'] = convert_all_elements_to_string($activities);
        }

        if ($request->marital_status == 1) {
            $marital_statuses = MaritalStatus::where('status', 1)->orderBy('id', 'asc')->get();
            $marital_statuses = $this->addAllOptionIfPreference($marital_statuses, $request);
            $o_data['marital_status'] = convert_all_elements_to_string($marital_statuses);
        }

        if ($request->sport == 1) {
            $sports = Sport::where('status', 1)->orderBy('id', 'asc')->get();
            $sports = $this->addAllOptionIfPreference($sports, $request);
            $o_data['sports'] = convert_all_elements_to_string($sports);
        }

        if ($request->language == 1) {
            $languages = Language::where('status', 1)->orderBy('id', 'asc')->get();
            $languages = $this->addAllOptionIfPreference($languages, $request);
            $o_data['languages'] = convert_all_elements_to_string($languages);
        }

        if ($request->education == 1) {
            $educations = Education::where('status', 1)->orderBy('id', 'asc')->get();
            $educations = $this->addAllOptionIfPreference($educations, $request);
            $o_data['educations'] = convert_all_elements_to_string($educations);
        }

        if ($request->profession == 1) {
            $professions = Profession::where('status', 1)->orderBy('id', 'asc')->get();
            $professions = $this->addAllOptionIfPreference($professions, $request);
            $o_data['professions'] = convert_all_elements_to_string($professions);
        }

        if ($request->dress_code == 1) {
            $dress_codes = DressCode::where('status', 1)->orderBy('id', 'asc')->get();
            $dress_codes = $this->addAllOptionIfPreference($dress_codes, $request);
            $o_data['dress_codes'] = convert_all_elements_to_string($dress_codes);
        }

        if ($request->emirates == 1) {
            $emirates = Emirate::where('status', 1)->orderBy('id', 'asc')->get();
            $emirates = $this->addAllOptionIfPreference($emirates, $request);
            $o_data['emirates'] = convert_all_elements_to_string($emirates);
        }

        if ($request->sect == 1) {
            $sects = Sect::where('status', 1)->orderBy('id', 'asc')->get();
            $sects = $this->addAllOptionIfPreference($sects, $request);
            $o_data['sects'] = convert_all_elements_to_string($sects);
        }

        if ($request->disease == 1) {
            $diseases = GeneticDisease::where('status', 1)->orderBy('id', 'asc')->get();
            $diseases = $this->addAllOptionIfPreference($diseases, $request);
            $o_data['diseases'] = convert_all_elements_to_string($diseases);
        }

        if ($request->chronic_disease == 1) {
            $diseases = ChronicDisease::where('status', 1)->orderBy('id', 'asc')->get();
            $diseases = $this->addAllOptionIfPreference($diseases, $request);
            $o_data['diseases'] = convert_all_elements_to_string($diseases);
        }

        return response()->json([
            'status' => $status,
            'error' => $errors,
            'message' => $message,
            'oData' => $o_data
        ], 200);
    }

    public function getNationalitiesList(Request $request)
    {
        $status = "1";
        $message = "Nationalities fetched successfully";
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $nationalities = Nationality::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($nationalities, $request);

        $o_data['nationalities'] = convert_all_elements_to_string($nationalities);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getCountriesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $countries = Country::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($countries, $request);

        $o_data['countries'] = convert_all_elements_to_string($countries);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getActivitiesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $activities = Activity::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($activities, $request);

        $o_data['activities'] = convert_all_elements_to_string($activities);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getMaritalStatusList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $marital_status = MaritalStatus::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($marital_status, $request);

        $o_data['marital_status'] = convert_all_elements_to_string($marital_status);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getSportsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $sports = Sport::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($sports, $request);

        $o_data['sports'] = convert_all_elements_to_string($sports);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getEmploymentsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $employments = Employment::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($employments, $request);

        $o_data['employments'] = convert_all_elements_to_string($employments);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getLanguagesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $languages = Language::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($languages, $request);

        $o_data['languages'] = convert_all_elements_to_string($languages);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getEducationsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $educations = Education::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($educations, $request);

        $o_data['educations'] = convert_all_elements_to_string($educations);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getProfessionsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $professions = Profession::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($professions, $request);
        $o_data['professions'] = convert_all_elements_to_string($professions);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getDressCodesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $dress_codes = DressCode::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($dress_codes, $request);
        $o_data['dress_codes'] = convert_all_elements_to_string($dress_codes);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getEmiratesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $emirates = Emirate::where('status', 1);
        if ($request->has('country_id') && $request->country_id != null) {
            $emirates->where('country_id', $request->country_id);
        }
        $emirates = $emirates->when($limit != 0, function ($query) use ($limit, $offset) {
            $query->limit($limit)->offset($offset);
        })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($emirates, $request);
        $o_data['emirates'] = convert_all_elements_to_string($emirates);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getSectsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $sects = Sect::where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($sects, $request);
        $o_data['sects'] = convert_all_elements_to_string($sects);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getDiseasesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $diseases = GeneticDisease::with('variants')->where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($diseases, $request);
        $o_data['diseases'] = convert_all_elements_to_string($diseases);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getCronicDiseasesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $diseases = ChronicDisease::with('variants')->where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($diseases, $request);
        $o_data['diseases'] = convert_all_elements_to_string($diseases);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    protected function addAllOptionIfPreference($collection, $request)
    {
        if ($request->is_preference && $request->is_preference == 1) {
            $ap = ['id' => 'all', 'name' => 'all', 'name_ar' => 'جميع'];
            if ($request->lang == 'ar') {
                $ap = ['id' => 'all', 'name' => 'جميع', 'name_ar' => 'جميع'];
            }
            $collection->prepend($ap);
        }
        return $collection;
    }



    public function getPageContent(Request $request)
    {
        $status = "1";
        $message = "CMS page fetched successfully";
        $o_data = [];
        $errors = [];

        $validator = Validator::make($request->all(), [
            'id' => 'integer|exists:cms_pages,id',
        ]);

        if ($validator->fails()) {
            http_response_code(200);
            echo json_encode([
                'status' => (string) 0,
                'message' => $validator->errors()->first(),
                'oData' => (object) [],
                'errors' => (object) $validator->messages(),
            ]);
            exit;
        }

        $title = ucfirst(str_replace('-', ' ', $request->title));
        // Get page by title or create a new record if not found
        $page = Page::where('id', $request->id)->first();
        if (!$page) {
            $message = trans('validation.invalid_page');
            return response()->json(
                [
                    'status' => '0',
                    'message' => $message,
                    'error' => (object) [],
                ],
                404,
            );
        }

        $o_data['page'] = convert_all_elements_to_string($page);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getDiseaseVariantsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $emirates = GeneticDiseaseVariant::query();
        if ($request->has('genetic_disease_id') && $request->genetic_disease_id != null) {
            $emirates->where('genetic_disease_id', $request->genetic_disease_id);
        }
        $emirates = $emirates->when($limit != 0, function ($query) use ($limit, $offset) {
            $query->limit($limit)->offset($offset);
        })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($emirates, $request);
        $o_data['genetic_disease_variants'] = convert_all_elements_to_string($emirates);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getChronicDiseasesList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $diseases = ChronicDisease::with('variants')->where('status', 1)
            ->when($limit != 0, function ($query) use ($limit, $offset) {
                $query->limit($limit)->offset($offset);
            })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($diseases, $request);
        $o_data['chronic_diseases'] = convert_all_elements_to_string($diseases);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getChronicDiseaseVariantsList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $limit = $request->limit ?? 10;
        $offset = isset($request->page) ? ($request->page - 1) * $limit : 0;

        $variants = ChronicDiseaseVariant::query();
        if ($request->has('chronic_disease_id') && $request->chronic_disease_id != null) {
            $variants->where('chronic_disease_id', $request->chronic_disease_id);
        }
        $variants = $variants->when($limit != 0, function ($query) use ($limit, $offset) {
            $query->limit($limit)->offset($offset);
        })->orderBy('id', 'asc')->get();
        $this->addAllOptionIfPreference($variants, $request);
        $o_data['chronic_disease_variants'] = convert_all_elements_to_string($variants);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getReportTypeList(Request $request)
    {
        $status = "1";
        $message = trans('validation.data_fetched_message');
        $o_data = [];
        $errors = [];

        $report_types = ReportType::where('status', 1)->orderBy('name', 'asc');
        if (isset($request->limit) && $request->limit == 0) {
            $report_types = $report_types->get();
        } else {
            $limit = isset($request->limit) ? $request->limit : 10;
            $offset = isset($request->page) ? ($request->page - 1) * $request->limit : 0;
            $report_types = $report_types->limit($limit)->offset($offset)->get();
        }
        $o_data['report_types'] = convert_all_elements_to_string($report_types);

        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getChatCategoryList(REQUEST $request)
    {
        $status = "1";
        $message = "";
        $o_data = [];
        $errors = [];

        $page = $request->page ?? 1;
        $limit = $rquest->limit ?? 10;
        $offset = ($page - 1) * $limit;
        $list = ChatCategory::where('status', 1)->whereHas('questions')->orderBy('id', 'asc');
        $total_records = $list->count();
        $list = $list->limit($limit)->offset($offset)->get();
        $o_data['list'] = convert_all_elements_to_string($list);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getChatQuestionList(REQUEST $request)
    {
        $status = "1";
        $message = "";
        $o_data = [];
        $errors = [];

        $page = $request->page ?? 1;
        $limit = $rquest->limit ?? 10;
        $category_id = $request->category_id;
        $offset = ($page - 1) * $limit;
        $list = Question::where(['chat_category_id' => $category_id])->whereHas('answers')->orderBy('id', 'asc');
        $total_records = $list->count();
        $list = $list->limit($limit)->offset($offset)->get();
        $o_data['list'] = convert_all_elements_to_string($list);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

    public function getAnswerList(REQUEST $request)
    {
        $status = "1";
        $message = "";
        $o_data = [];
        $errors = [];

        $page = $request->page ?? 1;
        $limit = $rquest->limit ?? 10;
        $question_id = $request->question_id;
        $offset = ($page - 1) * $limit;
        $list = Answer::where(['question_id' => $question_id])->orderBy('id', 'asc');
        $total_records = $list->count();
        $list = $list->limit($limit)->offset($offset)->get();
        $o_data['list'] = convert_all_elements_to_string($list);
        return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => $o_data], 200);
    }

}