<?php

namespace App\Services\APIs\CustomerAPIs;

use App\Models\IamPrincipal;
use Exception;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class CustomerApiServices
{
    public function getUserProfileDetailService($customerIamId)
    {
        try {
            $user = IamPrincipal::findOrFail($customerIamId);

            $data = IamPrincipal::select(
                'id',
                'first_name',
                'last_name',
                'email_address',
                'phone_number',
                'date_of_birth',
                'state_xid',
                'profile_photo',
                'referral_code'
            )->find($user->id);


            if ($data->profile_photo) {
                $data->profile_photo = ListingImageUrl('profile_image', $data->profile_photo);;
            } else {
                $data->profile_photo = asset('public/assets/img/blankProfile.png');
            }
            // $data['voucher_redeemed'] = MyPassportVoucher::where([['iam_principal_xid', $customerIamId], ['is_redeem', 1]])->count();
            return $data;
        } catch (Exception $ex) {
            Log::error('Customer Get data service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }


    public function updateUserProfileDetailService($customerIamId, $request)
    {
        try {
            DB::beginTransaction();
            $data = IamPrincipal::findOrFail($customerIamId);

            if (!$data) {
                DB::rollBack();
                return jsonResponseWithErrorMessage(__('error_message.user_details_not_found'), 404);
            }

            if ($request->has('image')) {
                $image = $request->image;
                $tnormalImage = saveSingleImageWithoutCrop($image, 'profile_image', null);
                $data->profile_photo = $tnormalImage;
                $data->save();
                DB::commit();

            }

            if ($request->has('date_of_birth')) {
                $data->date_of_birth = $request->date_of_birth;
                $data->save();
                DB::commit();

            }
            if ($request->has('first_name')) {
                $data->first_name = $request->first_name;
                $data->save();
                DB::commit();

            }
            if ($request->has('last_name')) {
                $data->last_name = $request->last_name;
                $data->save();
                DB::commit();

            }

            if ($request->has('phone_number')) {
                $data->phone_number = $request->phone_number;
                $data->save();
                DB::commit();

            }

            if ($request->has('email_address')) {
                $email = $request->input('email_address');
                if ($email !== $data->email_address) {
                    $existingUser = IamPrincipal::where('email_address', $email)
                        ->where('id', '!=', $customerIamId)
                        ->whereNull('deleted_at')
                        ->exists();

                    if ($existingUser) {
                        return jsonResponseWithErrorMessage(__('auth.email_already_exist'), 400);
                    }

                    $data->email_address = $email;
                }
            }

            $data->save();

            DB::commit();

            return response()->json(['status' => 'success', 'message' => 'User profile updated successfully']);
        } catch (Exception $ex) {
            DB::rollBack();

            Log::error('Customer update profile service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function resetUserPassword($customerIamId, $request)
    {
        try {
            DB::beginTransaction();
            $user = IamPrincipal::findOrFail($customerIamId);
            if (!Hash::check($request->current_password, $user->password)) {
                DB::rollBack();
                return jsonResponseWithErrorMessageApi(__('auth.invalid_current_passsword'), 404);
            } else {
                $user->update([
                    'password' => Hash::make($request->new_password)
                ]);
                DB::commit();
                return jsonResponseWithSuccessMessageApi(__('auth.password_updated_successfully'));
            }
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('Update password service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }




}



