<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Department;
use App\Models\Department_Working_Time;
use App\Models\General_Setting;
use App\Models\Onboarding_Screen;
use App\Models\Page;
use App\Models\Service;
use App\Models\Social_Link;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\Validator;

class HomeController extends Controller
{
    private $folder_setting = "setting";
    private $folder_page = "page";
    private $folder_service = "service";
    private $folder_department = "department";

    public $common;
    public $page_limit;
    public function __construct()
    {
        try {
            $this->common = new Common();
            $this->page_limit = env('PAGE_LIMIT');
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }

    public function general_setting()
    {
        try {

            $list = General_Setting::get();
            foreach ($list as $key => $value) {

                if ($value['key'] == 'app_logo') {
                    $value['value'] = $this->common->getImage($this->folder_setting, $value['value']);
                }
            }

            return $this->common->API_Response(200, __('api_msg.get_record_successfully'), $list);
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
    public function get_social_links()
    {
        try {
            $data = Social_Link::get();
            if (count($data) > 0) {

                $this->common->imageNameToUrl($data, 'image', $this->folder_setting);
                return $this->common->API_Response(200, __('api_msg.get_record_successfully'), $data);
            } else {
                return $this->common->API_Response(400, __('api_msg.data_not_found'));
            }
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
    public function get_onboarding_screen()
    {
        try {
            $data = Onboarding_Screen::get();
            if (count($data) > 0) {

                $this->common->imageNameToUrl($data, 'image', $this->folder_setting);
                return $this->common->API_Response(200, __('api_msg.get_record_successfully'), $data);
            } else {
                return $this->common->API_Response(400, __('api_msg.data_not_found'));
            }
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
    public function get_department(Request $request)
    {
        try {

            $data = Department::where('status', 1)->orderBy('name', 'ASC')->get();

            if (count($data) > 0) {

                $this->common->imageNameToUrl($data, 'department_icon', $this->folder_department);
                foreach ($data as $value) {
                    $value['working_time'] = Department_Working_Time::where('department_id', $value['id'])->get();
                }
                return $this->common->API_Response(200, __('api_msg.get_record_successfully'), $data);
            } else {
                return $this->common->API_Response(400, __('api_msg.data_not_found'));
            }
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
    public function get_service(Request $request)
    {
        try {

            $department_id = isset($request->department_id) ? $request->department_id : 0;

            if($department_id != 0){

                $data = Service::where('department_id', $department_id)->orderBy('name', 'ASC')->get();
            } else {
                $data = Service::orderBy('name', 'ASC')->get();
            }

            if (count($data) > 0) {

                $this->common->imageNameToUrl($data, 'image', $this->folder_service);
                foreach ($data as $value) {

                    $value['subin_charge_name'] = $this->common->get_Subin_charge_name($department_id, $value['id']);
                }

                return $this->common->API_Response(200, __('api_msg.get_record_successfully'), $data);
            } else {
                return $this->common->API_Response(400, __('api_msg.data_not_found'));
            }
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
}
