<?php

namespace Laravel\Jetstream;

use App\Models\Organization;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Jetstream\Features;

trait HasProfilePhoto
{
    /**
     * Update the user's profile photo.
     *
     * @param  \Illuminate\Http\UploadedFile  $photo
     * @return void
     */
    public function updateProfilePhoto(UploadedFile $photo)
    {
        tap($this->profile_photo_path, function ($previous) use ($photo) {
            $this->forceFill([
                'profile_photo_path' => $photo->storePublicly(
                    'profile-photos',
                    ['disk' => $this->profilePhotoDisk()]
                ),
            ])->save();

            // Only delete a previous profile-photo, and not a previous default-photo in 'app-images/'
            if (str_starts_with($previous, 'profile-photos/')) {
                Storage::disk($this->profilePhotoDisk())->delete($previous);
            }
        });
    }

    /**
     * Delete the user's profile photo.
     *
     * @return void
     */
    public function deleteProfilePhoto()
    {
        if (! Features::managesProfilePhotos()) {
            return;
        }

        if (is_null($this->profile_photo_path)) {
            return;
        }


        // Only delete a profile-photo, and not a default-photo in 'app-images/'
        if (str_starts_with($this->profile_photo_path, 'profile-photos/')) {
            Storage::disk($this->profilePhotoDisk())->delete($this->profile_photo_path);

            $this->forceFill([
                'profile_photo_path' =>  timebank_config('profiles.user.profile_photo_path_default'),
            ])->save();

            Session(['activeProfilePhoto'=> $this->profile_photo_path ]);
        }
    }

    /**
     * Get the URL to the user's profile photo.
     *
     * @return string
     */
    public function getProfilePhotoUrlAttribute()
    {
        return $this->profile_photo_path && Storage::disk($this->profilePhotoDisk())->exists($this->profile_photo_path)
                    ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
                    : $this->defaultProfilePhotoUrl();
    }

    /**
     * Get the default profile photo URL if no profile photo has been uploaded.
     *
     * @return string
     */
    protected function defaultProfilePhotoUrl()
    {
        if (session('activeProfileType') == Organization::class) {
            return timebank_config('profiles.organization.profile_photo_path_default');
        } else {
            return timebank_config('profiles.user.profile_photo_path_default');
        }
    }

    /**
     * Get the disk that profile photos should be stored on.
     *
     * @return string
     */
    protected function profilePhotoDisk()
    {
        return isset($_ENV['VAPOR_ARTIFACT_NAME']) ? 's3' : config('jetstream.profile_photo_disk', 'public');
    }
}
