<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\Controller;
use App\Models\NotificationSetting;
use App\Models\User;
use App\Models\UserBlock;
use App\Models\UserConnection;
use App\Models\UserReport;
use App\Models\UserRequest;
use App\Models\Chat;
use App\Models\ChatMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Kreait\Firebase\Contract\Database;

class UserConnectionRequestController extends Controller
{
    public $database;
    public function __construct(Database $database, Request $request)
    {
        $this->database = $database;
        if (isset($request->lang)) {
            \App::setLocale($request->lang);
        }
        $this->lang = \App::getLocale();
    }
    private function validateAccesToken($access_token)
    {
        $user = User::where(['user_access_token' => $access_token])->first();
        if (!$user) {
            http_response_code(401);
            echo json_encode([
                'status' => (string) 0,
                'message' => 'Invalid access token',
                'oData' => (object) [],
                'errors' => (object) [],
            ]);
            exit();
        }
        if ($user->status != 1) {
            http_response_code(401);
            echo json_encode([
                'status' => (string) 0,
                'message' => 'User is not active',
                'oData' => (object) [],
                'errors' => (object) [],
            ]);
            exit();
        }
        return $user;
    }
    public function send_connection_request(Request $request)
    {
         $status = '0';
        $message = '';
        $errors = [];
        $o_data = [];
        $validator = Validator::make($request->all(), [
            'receiver_id' => 'required|exists:users,id',
            'access_token' => 'required',
        ],[
            'access_token.required' => trans('validation.access_token_required')
        ]);
        if ($validator->fails()) {
            return response()->json([
                'status' => $status,
                'message' => 'Validation error occurred',
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }
        $user = $this->validateAccesToken($request->access_token);
        if (empty($user)) {
            $message = trans('validation.invalid_user');
            return response()->json(
                [
                    'status' => '0',
                    'message' => $message,
                    'error' => (object) [],
                ],
                401,
            );
        }
        $sender_id=$user->id;
        $receiver_id=$request->receiver_id;

        if($sender_id==$receiver_id){
               return response()->json([
            'status' => '0',
            'message' => trans('validation.invalid_connection_request'),
            'error' => (object)[],
        ]);
        }
        
        $connected = UserConnection::where('user_id', $sender_id)->orWhere('connected_user_id', $sender_id)->exists();
        
         if ($connected) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.already_have_connection'),
                'error' => (object)[],
            ]);
            }
            
        $connected = UserConnection::where('user_id', $receiver_id)->orWhere('connected_user_id', $receiver_id)->exists();
        
         if ($connected) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.user_already_have_connection'),
                'error' => (object)[],
            ]);
            }

        // Check if already connected
        $connected = UserConnection::where(function ($q) use ($sender_id, $receiver_id) {
            $q->where('user_id', $sender_id)->where('connected_user_id', $receiver_id);
        })->orWhere(function ($q) use ($sender_id, $receiver_id) {
            $q->where('user_id', $receiver_id)->where('connected_user_id', $sender_id);
        })->exists();

        if ($connected) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.already_friend'),
                'error' => (object)[],
            ]);
            }


            // Check if blocked
        $blocked = UserBlock::where(function ($q) use ($sender_id, $receiver_id) {
            $q->where('user_id', $sender_id)->where('blocked_user_id', $receiver_id)
                ->orWhere('user_id', $receiver_id)->where('blocked_user_id', $sender_id);
        })->exists();

        if ($blocked) {
        return response()->json([
                'status' => '0',
                'message' => trans('validation.user_block_request'),
                'error' => (object)[],
            ]);
        }


        
            // Check if reported
        $blocked = UserReport::where(function ($q) use ($sender_id, $receiver_id) {
            $q->where('reporter_id', $sender_id)->where('reported_user_id', $receiver_id)
                ->orWhere('reported_user_id', $receiver_id)->where('reporter_id', $sender_id);
        })->exists();

        if ($blocked) {
        return response()->json([
                'status' => '0',
                'message' => trans('validation.invalid_connection_request'),
                'error' => (object)[],
            ]);
        }

        // Check if request already exists
        $requestExists = UserRequest::where('sender_id', $sender_id)->where('status','=',0)->where('receiver_id', $receiver_id)->first();

        if ($requestExists) {
            return response()->json([
                'status' => 0,
                'message' => 'Friend request already sent.',
            ]);
        }

        if($user->active_request_count <= 0){
            return response()->json([
                'status' => 0,
                'message' => 'Your request limit has been reached. Please try upgrading your package.',
            ]);
        }

        $exU = User::find($user->id);
        $exU->active_request_count = $exU->active_request_count - 1;
        $exU->save();

        UserRequest::create([
            'sender_id' => $sender_id,
            'receiver_id' => $receiver_id,
            'status' => 0,
        ]);

        $rUser = User::where('id', $receiver_id)->first();

        $connection = DB::table('user_connections')->where('user_id', $user->id)
                    ->where('connected_user_id', $rUser->id)
                        ->first();

        $hideImage = $connection ? (int) $connection->hide_image : 0;

        send_notification_to_user(
            $this->database,
            $rUser,
            ['userId' => (string)$user->id, "imageBlur" => (string)$hideImage],
            NotificationSetting::REQUEST,
            'proposal_request',
            time(),
             trans('notification.proposal_request_title', [], 'en') . "\n" . trans('notification.proposal_request_title', [], 'ar'),
                trans('notification.proposal_request_message', [], 'en') .$user->name. "\n" . trans('notification.proposal_request_message', [], 'ar').$user->name,
            $user->profile_image_url
        );

        return response()->json(
            [
                'oData' => [],
                'status' => '1',
                'message' => trans('validation.connection_request_sent'),
            ],
            200,
        );
    }

    public function accept_connection_request(Request $request)
    {
         $status = '0';
        $message = '';
        $errors = [];
        $o_data = [];
        $validator = Validator::make($request->all(), [
            'sender_id' => 'required|exists:users,id',
            'access_token' => 'required',
            'status'=>'nullable'
        ],[
            'access_token.required' => trans('validation.access_token_required')
        ]);
        if ($validator->fails()) {
            return response()->json([
                'status' => $status,
                'message' => 'Validation error occurred',
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }
        $user = $this->validateAccesToken($request->access_token);
        if (empty($user)) {
            $message = trans('validation.invalid_user');
            return response()->json(
                [
                    'status' => '0',
                    'message' => $message,
                    'error' => (object) [],
                ],
                401,
            );
        }
        $sender_id=$request->sender_id;
        $user_id=$user->id;
           $connection = DB::table('user_connections')
    ->where(function ($query) use ($user,$sender_id) {
        $query->where('user_id', $user->id)
              ->where('connected_user_id', $sender_id);
    })
    ->orWhere(function ($query) use ($user, $sender_id) {
        $query->where('user_id', $sender_id)
              ->where('connected_user_id', $user->id);
    })
    ->first();
    
     $connected = UserConnection::where('user_id', $sender_id)->orWhere('connected_user_id', $sender_id)->exists();
        
         if ($connected) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.user_already_have_connection'),
                'error' => (object)[],
            ]);
            }

    $connected = UserConnection::where('user_id', $user_id)->orWhere('connected_user_id', $user_id)->exists();
   
        
         if ($connected) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.already_have_connection'),
                'error' => (object)[],
            ]);
            }
    if(!$connection){
        //on accept add two row for each user
        UserConnection::create([
            'user_id'=>$sender_id,
            'connected_user_id'=>$user_id
        ]);

           UserConnection::create([
            'user_id'=>$user_id,
            'connected_user_id'=>$sender_id
        ]);
    }
        //user connected
        UserRequest::where(function ($q) use ($sender_id, $user_id) {
            $q->where('sender_id', $sender_id)->where('receiver_id', $user_id)
            ->orWhere('sender_id', $user_id)->where('receiver_id', $sender_id);
        })->update(['status' => 1]);

        $sUser = User::where('id', $sender_id)->first();

        $connection = DB::table('user_connections')->where('user_id', $user->id)
                    ->where('connected_user_id', $sUser->id)
                        ->first();

        $hideImage = $connection ? (int) $connection->hide_image : 0;

        send_notification_to_user(
            $this->database,
            $sUser,
            ['userId' => (string)$user->id, "imageBlur" => (string)$hideImage],
            NotificationSetting::REQUEST,
            'request_accepted',
            time(),
             trans('notification.request_accepted_title', [], 'en') . "\n" . trans('notification.request_accepted_title', [], 'ar'),
            $user->name.trans('notification.request_accepted_message', [], 'en') . "\n" . $user->name. trans('notification.request_accepted_message', [], 'ar'),
            $user->profile_image_url
        );


        $user = User::with(['children', 'preference','nationality','emirate', 'country', 'images', 'details.sect', 'details.education', 'details.profession', 'details.maritalStatus', 'details.dressCode',  'plan', 'sports.sport', 'activities.activity', 'known_languages.language', 'notification_settings','geneticDiseases.geneticDisease','geneticDiseases.geneticDiseaseVariant.geneticDiseaseVariant'])
            ->find($user_id)
            ->toArray();
            convertNullRelationsToObjects($user);
            $o_data=convert_all_elements_to_string($o_data, true);
        return response()->json(
            [
                'oData' => $o_data,
                'status' => '1',
                'message' => trans('validation.connection_request_accepted'),
            ],
            200,
        );
    }

    public function reject_connection_request(Request $request)
    {
        $status = '0';
        $message = '';
        $errors = [];
        $o_data = [];
        $validator = Validator::make($request->all(), [
            'sender_id' => 'required|exists:users,id',
            'access_token' => 'required',
        ],[
            'access_token.required'
        ]);

        $user = $this->validateAccesToken($request->access_token);
        $senderId = $request->sender_id;

        $userRequest=UserRequest::where(function ($q) use ($user, $request) {
        $q->where('sender_id', $request->sender_id)
          ->where('receiver_id', $user->id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('sender_id', $user->id)
          ->where('receiver_id', $request->sender_id);
    })->first();

        if (empty($userRequest)) {
            return response()->json([
                'status' => $status,
                'message' => trans('validation.can_not_change_request_status'),
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }

        if($userRequest->status != 0){
            return response()->json([
                'status' => $status,
                'message' => trans('validation.can_not_change_request_status'),
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }

        if($userRequest->receiver_id !== $user->id){
            return response()->json([
                'status' => $status,
                'message' => trans('validation.can_not_change_request_status'),
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }

         $userRequest->delete();

        $sUser = User::where('id', $senderId)->first();

        $connection = DB::table('user_connections')->where('user_id', $user->id)
                    ->where('connected_user_id', $sUser->id)
                        ->first();

        $hideImage = $connection ? (int) $connection->hide_image : 0;

        send_notification_to_user(
            $this->database,
            $sUser,
            ['userId' => (string)$user->id, "imageBlur" => (string)$hideImage],
            NotificationSetting::REQUEST,
            'request_rejected',
            time(),
            trans('notification.request_rejected_title', [], 'en') . "\n" . trans('notification.request_rejected_title', [], 'ar'),
            $user->name.trans('notification.request_rejected_message', [], 'en') . "\n" . $user->name. trans('notification.request_rejected_message', [], 'ar'),
            $user->profile_image_url
        );

        $message = trans('validation.connection_request_rejected');
        $status = '1';
        return response()->json([
            'status' => $status,
            'message' => $message,
            'errors' => $errors,
            'oData' => $o_data,
        ]);


    }
public function get_connection_request_list(Request $request)
{
    $status = '0';
    $message = '';
    $o_data = [];

    $validator = Validator::make($request->all(), [
        'access_token' => 'required',
    ], [
        'access_token.required' => trans('validation.access_token_required')
    ]);

    if ($validator->fails()) {
        return response()->json([
            'status' => $status,
            'message' => 'Validation error occurred',
            'errors' => $validator->errors(),
            'oData' => $o_data,
        ]);
    }

    $user = $this->validateAccesToken($request->access_token);
    if (empty($user)) {
        return response()->json([
            'status' => '0',
            'message' => trans('validation.invalid_user'),
            'error' => (object) [],
        ], 401);
    }

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

    $userId = $user->id;

    $requestUserIds = DB::table('user_requests')
        ->selectRaw('CASE WHEN sender_id = ? THEN receiver_id ELSE sender_id END as related_user_id', [$userId])
        ->where(function ($q) use ($userId) {
            $q->where('sender_id', $userId)
              ->orWhere('receiver_id', $userId);
        });

    $users = User::with([
        'children', 'preference','nationality','emirate', 'country', 'images', 
        'details.sect', 'details.education', 'details.profession', 'details.maritalStatus', 
        'details.dressCode', 'plan', 'sports.sport', 'activities.activity', 
        'known_languages.language', 'notification_settings',
        'geneticDiseases.geneticDisease','geneticDiseases.geneticDiseaseVariant'
    ])
    ->whereIn('users.id', $requestUserIds)
    ->leftJoin('user_requests as ur_sent', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ur_sent.receiver_id')
             ->where('ur_sent.sender_id', '=', $userId);
    })
    ->leftJoin('user_requests as ur_received', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ur_received.sender_id')
             ->where('ur_received.receiver_id', '=', $userId);
    })
    ->leftJoin('user_blocks as ub_me', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ub_me.blocked_user_id')
             ->where('ub_me.user_id', '=', $userId);
    })
    ->leftJoin('user_blocks as ub_other', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ub_other.user_id')
             ->where('ub_other.blocked_user_id', '=', $userId);
    })
    ->leftJoin('user_reports as ur_me', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ur_me.reported_user_id')
             ->where('ur_me.reporter_id', '=', $userId);
    })
    ->leftJoin('user_reports as ur_other', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ur_other.reporter_id')
             ->where('ur_other.reported_user_id', '=', $userId);
    })
    ->leftJoin('user_connections as uc1', function ($join) use ($userId) {
        $join->on('users.id', '=', 'uc1.connected_user_id')
             ->where('uc1.user_id', '=', $userId);
    })
    ->leftJoin('user_connections as uc2', function ($join) use ($userId) {
        $join->on('users.id', '=', 'uc2.user_id')
             ->where('uc2.connected_user_id', '=', $userId);
    })
    ->leftJoin('profile_picture_requests as ppr', function ($join) use ($userId) {
        $join->on('users.id', '=', 'ppr.receiver_id')
             ->where('ppr.sender_id', '=', $userId);
    })
    ->whereNull('users.deleted_at')
    ->where('users.deleted',0)
    // Exclude blocked or reported users entirely
    ->whereNull('ub_me.id')
    ->whereNull('ub_other.id')
    ->whereNull('ur_me.id')
    ->whereNull('ur_other.id')

    ->select('users.*')
    ->selectRaw('
        CASE WHEN uc1.id IS NOT NULL OR uc2.id IS NOT NULL THEN 1 ELSE 0 END as is_connected,

        CASE 
            WHEN (ub_me.id IS NULL AND ub_other.id IS NULL AND ur_me.id IS NULL AND ur_other.id IS NULL) 
                 AND ur_sent.id IS NOT NULL 
            THEN 1 ELSE 0 
        END as is_sent,

        CASE 
            WHEN (ub_me.id IS NULL AND ub_other.id IS NULL AND ur_me.id IS NULL AND ur_other.id IS NULL) 
                 AND ur_received.id IS NOT NULL 
            THEN 1 ELSE 0 
        END as is_received,

        0 as is_blocked, -- always zero since blocked users excluded
        0 as am_i_blocked, -- same here

        0 as is_reported, -- reported users excluded
        0 as am_i_reported,

        CASE 
            WHEN (uc1.id IS NOT NULL OR uc2.id IS NOT NULL) 
            THEN COALESCE(uc1.hide_image, 0) ELSE 0 
        END as hide_image_by_current_user,

        CASE 
            WHEN ppr.status = "accepted" 
            THEN "0" ELSE "1"
        END as hide_image_by_target_user
    ')
    ->offset($offset)
    ->limit($limit)
    ->get();

    $o_data['users'] = $users->map(function ($u) {
        $arr = $u->toArray();
        convertNullRelationsToObjects($arr);
        return convert_all_elements_to_string($arr, true);
    });

    return response()->json([
        'oData' => $o_data,
        'status' => '1',
        'message' => trans('validation.data_fetched_message'),
    ], 200);
}


public function update_connection_status(Request $request)
    {
        $status = '0';
        $message = trans('validation.update_connection_message');
        $errors = [];
        $o_data = [];
        $isBlocked = false;
        $isReported = false;
 
        $validator = Validator::make($request->all(), [
            'access_token' => 'required',
            'is_blocked' => 'nullable|boolean',
            'is_reported' => 'nullable|boolean',
            'report_type_id'=>'nullable',
            'reason' => 'nullable|string',
            'hide_image' => 'nullable|boolean',
            'unmatch' => 'nullable|boolean',
            'connected_user_id' => 'required|exists:users,id'
        ], [
            'access_token.required' => trans('validation.access_token_required')
        ]);
 
        if ($validator->fails()) {
            return response()->json([
                'status' => $status,
                'message' => 'Validation error occurred',
                'errors' => $validator->errors(),
                'oData' => $o_data,
            ]);
        }
 
        $notificationStatus = false;
 
        $user = $this->validateAccesToken($request->access_token);
        if (empty($user)) {
            return response()->json([
                'status' => '0',
                'message' => trans('validation.invalid_user'),
                'error' => (object) [],
            ], 401);
        }
 
        // Handle hide_image (still in user_connections)
        if ($request->hide_image !== null) {
            UserConnection::where('user_id', $user->id)
                ->where('connected_user_id', $request->connected_user_id)
                ->update(['hide_image' => $request->hide_image]);
            
            $hideText = $request->hide_image == 1 ? 'hide' : 'unhide';
            
            $nDescription = $user->name." ".$hideText." you on Luqia";
            $ntype = 'user_image_toggle';
            $nTitle = ucfirst($hideText)." Notification";
            $notificationStatus = true;
            $permission = NotificationSetting::ALERT;
        }
 
        // Handle block
        if ($request->is_blocked) {
            DB::table('user_blocks')->updateOrInsert(
                ['user_id' => $user->id, 'blocked_user_id' => $request->connected_user_id],
                ['reason' => $request->reason, 'created_at' => now(), 'updated_at' => now()]
            );
             UserRequest::where(function ($q) use ($user, $request) {
        $q->where('sender_id', $request->connected_user_id)
          ->where('receiver_id', $user->id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('sender_id', $user->id)
          ->where('receiver_id', $request->connected_user_id);
    })->where('status', '!=', 1)->delete();
 
    UserConnection::where(function ($q) use ($user, $request) {
        $q->where('user_id', $user->id)
          ->where('connected_user_id', $request->connected_user_id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('user_id', $request->connected_user_id)
          ->where('connected_user_id', $user->id);
    })->delete();
            $message = trans('validation.update_connection_blocked');
            $isBlocked = true;
        }
 
        // Handle report
        if ($request->is_reported) {
DB::table('user_reports')->insert([
        'reporter_id' => $user->id,
        'reported_user_id' => $request->connected_user_id,
        'reason' => $request->reason,
        'report_type_id'=>$request->report_type_id,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
 
    DB::table('user_blocks')->updateOrInsert(
        ['user_id' => $user->id, 'blocked_user_id' => $request->connected_user_id],
        ['reason' => $request->reason, 'created_at' => now(), 'updated_at' => now()]
    );
 
    UserRequest::where(function ($q) use ($user, $request) {
        $q->where('sender_id', $request->connected_user_id)
          ->where('receiver_id', $user->id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('sender_id', $user->id)
          ->where('receiver_id', $request->connected_user_id);
    })->delete();
 
    UserConnection::where(function ($q) use ($user, $request) {
        $q->where('user_id', $user->id)
          ->where('connected_user_id', $request->connected_user_id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('user_id', $request->connected_user_id)
          ->where('connected_user_id', $user->id);
    })->delete();
 
            $message = trans('validation.report_connection_message');
            $isReported = true;
 
        }
 
        // Handle unmatch
        if ($request->unmatch) {
            UserConnection::where(function ($q) use ($user, $request) {
                $q->where('user_id', $user->id)
                ->where('connected_user_id', $request->connected_user_id);
            })->orWhere(function ($q) use ($user, $request) {
                $q->where('user_id', $request->connected_user_id)
                ->where('connected_user_id', $user->id);
            })->delete();
 
              UserRequest::where(function ($q) use ($user, $request) {
        $q->where('sender_id', $request->connected_user_id)
          ->where('receiver_id', $user->id);
    })->orWhere(function ($q) use ($user, $request) {
        $q->where('sender_id', $user->id)
          ->where('receiver_id', $request->connected_user_id);
    })->delete();
    
            Chat::where(function ($q) use ($user, $request) {
                $q->where('user_id_1', $request->connected_user_id)
                  ->where('user_id_2', $user->id);
            })->orWhere(function ($q) use ($user, $request) {
                $q->where('user_id_1', $user->id)
                  ->where('user_id_2', $request->connected_user_id);
            })->delete();
            
            ChatMessage::where(function ($q) use ($user, $request) {
                $q->where('receiver_id', $request->connected_user_id)
                  ->where('sender_id', $user->id);
            })->orWhere(function ($q) use ($user, $request) {
                $q->where('receiver_id', $user->id)
                  ->where('sender_id', $request->connected_user_id);
            })->delete();
    
            $message = trans('validation.data_fetched_message');
 
            $nDescription =$user->name.trans('notification.unmatch_notification_message', [], 'en') . "\n" . $user->name. trans('notification.unmatch_notification_message', [], 'ar');
            $ntype = 'user_unmatched';
            $nTitle = trans('notification.unmatch_notification_title', [], 'en') . "\n" . trans('notification.unmatch_notification_title', [], 'ar');
            $notificationStatus = true;
            $permission = NotificationSetting::REQUEST;
        }
        
        if($isBlocked){
            $nDescription = $user->name.trans('notification.blocked_notification_message', [], 'en') . "\n" . $user->name. trans('notification.blocked_notification_message', [], 'ar');
            $ntype = 'user_blocked';
            $nTitle = trans('notification.blocked_notification_title', [], 'en') . "\n" . trans('notification.blocked_notification_title', [], 'ar');
            $notificationStatus = true;
            $permission = NotificationSetting::ALERT;
        }
 
        if($isReported){
            $nDescription = $user->name.trans('notification.report_notification_message', [], 'en') . "\n" . $user->name. trans('notification.report_notification_message', [], 'ar');
            $ntype = 'user_reported';
            $nTitle = trans('notification.report_notification_title', [], 'en') . "\n" . trans('notification.report_notification_title', [], 'ar');
            $notificationStatus = true;
            $permission = NotificationSetting::ALERT;
        }
 
        if($notificationStatus){
            $rUser = User::where('id', $request->connected_user_id)->first();
            $connection = DB::table('user_connections')->where('user_id', $user->id)
                    ->where('connected_user_id', $rUser->id)
                        ->first();
 
            $hideImage = $connection ? (int) $connection->hide_image : 0;
 
            send_notification_to_user(
                $this->database,
                $rUser,
                ['userId' => (string)$user->id, "imageBlur" => (string)$hideImage],
                $permission,
                $ntype,
                time(),
                $nTitle,
                $nDescription,
                $user->profile_image_url
            );
 
        }
 
        // Reload the user data with relationships
        $user = User::with(['children', 'preference','nationality','emirate', 'country', 'images', 'details.sect', 'details.education', 'details.profession', 'details.maritalStatus', 'details.dressCode',  'plan', 'sports.sport', 'activities.activity', 'known_languages.language', 'notification_settings','geneticDiseases.geneticDisease','geneticDiseases.geneticDiseaseVariant.geneticDiseaseVariant'])->find($user->id)->toArray();
 
        convertNullRelationsToObjects($user);
        $o_data['user'] = convert_all_elements_to_string($user, true);
 
        return response()->json([
            'oData' => $o_data,
            'status' => '1',
            'message' => $message,
        ], 200);
    }
 


}
