<?php

namespace App\Http\Controllers;

use App\Interfaces\UserRepositoryInterface;
use App\Interfaces\OrganisationRepositoryInterface;
use App\Models\Profile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use PHPOpenSourceSaver\JWTAuth\JWTAuth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;


//Handles all functionalities for users Profile
class UserProfileController extends Controller
{

    private UserRepositoryInterface $userRepository;
    private OrganisationRepositoryInterface $organisationRepository;
    public function __construct(UserRepositoryInterface $userRepository, OrganisationRepositoryInterface $organisationRepository)
    {
        $this->middleware('jwt');
        $this->userRepository = $userRepository;
        $this->organisationRepository = $organisationRepository;

    }


    //Get Profile
    public function index(Request $request)
    {
        try {
            $user = Auth::user();
            // $profile = $this->userRepository->getUserProfile($user->id);
            // $jobProfile = $this->userRepository->getUserProfileByUser($user->id);
            $organization = $this->organisationRepository->findById($user->organisation_id);
            // $profile['email'] = $user->email;
            $data = ["organization" => $organization, 'user' => $user];
            return response()->json([
                'status' => 'success',
                'message' => 'Profile returned successfully',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    //Update Profile
    public function updateProfile(Request $request)
    {
        try {
            $id = $request->id;
            $email = $request->work_email;
            $profile = $this->userRepository->updateUserProfile($id, $request->all());

            if(!empty($email)){
                $userData = [
                    "email" => $email
                ];

                $updateData = $this->userRepository->updateUser($profile->user_id, $userData);
            }

            return response()->json([
                'status' => 'success',
                'message' => 'Profile updated successfully',
                'data' => $profile
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

        //Upload Profile Image
        public function updateProfileRequest(Request $request)
        {
            try {
                $user = Auth::user();
                $user_data = $this->userRepository->findById($user->id);
                $org = $this->organisationRepository->findById($user->organisation_id);

                if ($org) {
                    if ($request->profile_img) {
                        // Get the uploaded file
                        $file = $request->file('profile_img');
                        // Define the path where the file should be stored in S3
                        $path = 'uploads/' . uniqid() . '.' . $file->getClientOriginalExtension();
                        // Put the file content into the defined path in S3
                        Storage::disk('s3')->put($path, file_get_contents($file));
                        // Get the full URL of the stored file
                        $disk = Storage::disk('s3');
                        $url = $disk->url($path);
    
                        $data = ['profile_image' => $url];
    
                        // return $data;
                        $profileId = $user["profile"]->id;
                        $profile = $this->userRepository->updateUserProfile($profileId, $data);
                    }
                    return response()->json([
                        'status' => 'success',
                        'data' => $profile,
                        'message' => 'Profile image uploaded successfully'
                    ]);
                }
    
            } catch (\Exception $e) {
                return response()->json([
                    'status' => 'failed',
                    'message' => $e->getMessage(),
                ], 500);
            }
        }

}
