<?php

namespace Modules\Pengguna\Services;

use App\Libraries\Constants;
use App\Services\BaseService;
use Illuminate\Support\Facades\Storage;
use Modules\Pengguna\Repositories\PenggunaRepository;

class PenggunaService extends BaseService implements PenggunaServiceInterface
{
    public function __construct(PenggunaRepository $penggunaRepository)
    {
        $this->mainRepository = $penggunaRepository;
    }

    public function datatable()
    {
        return $this->mainRepository->datatable();
    }

    public function deleteUserAndRemoveRole($id)
    {
        try {
            $user = $this->mainRepository->findById($id);

            if (!$user) {
                throw new \Exception(__('messages.id_not_found'));
            }

            // REMOVE ROLE
            $user->removeRole($user->role->name);

            if (!$user->delete()) {
                throw new \Exception(__('messages.delete_failed', ['action' => __('lang.users'),]));
            }

            return $user;
        } catch (\Exception $e) {
            writeAppLog($e->getMessage(), 'alert');

            return false;
        }
    }

    public function storePhotoProfil($request)
    {
        try {
            $Pengguna = $this->findById(auth()->user()->id);

            $userPath = getUploadPath() . config('ppid.photo_profil_path');
            $validFileExtension = ['jpg', 'png', 'jpeg'];

            // FOTO PROFIL PENGGUNA
            if ($request->profil_image) {
                $PhotoProfilName = $request->profil_image->getClientOriginalName();
                $PhotoProfilExtension = $request->profil_image->getClientOriginalExtension();
                $PhotoProfilSize = convertFileSize($request->profil_image->getSize());
                $newNamePhotoProfil = Constants::FILE_PHOTO_PROFIL_NAME . time() . '.' . $PhotoProfilExtension;

                // VALIDATION EXTENSION
                if (!in_array(strtolower($PhotoProfilExtension), $validFileExtension)) {
                    throw new \Exception(__('validation.file', ['attribute' => __('lang.profil_image'),]));
                }

                // VALIDATION FILE SIZE
                if ($PhotoProfilSize > getSetting('max_file_upload')) {
                    throw new \Exception(__('validation.max.file', ['attribute' => __('lang.profil_image'), 'max' => config('ppid.max_file_upload'),]));
                }

                // DELETE FILE SEBELUMNYAs
                if ($Pengguna->foto_profil !== NULL) {
                    if (Storage::exists(getUploadPath() . $Pengguna->foto_profil)) {
                        Storage::delete(getUploadPath() . $Pengguna->foto_profil);
                    }
                }

                // UPLOAD
                Storage::putFileAs($userPath, $request->profil_image, $newNamePhotoProfil);

                return $Pengguna->update(['foto_profil' => config('ppid.photo_profil_path') . $newNamePhotoProfil]);
            }
        } catch (\Exception $e) {
            // WRITE LOG
            writeAppLog($e->getMessage() . ' (Line : ' . $e->getLine() . ')', 'alert');

            return false;
        }
    }
}
