<?php

namespace App\Http\Livewire\ProfileUser;

use App\Models\User;
use App\Traits\FormHelpersTrait;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\HasProfilePhoto;
use Livewire\Component;
use Livewire\WithFileUploads;

class UpdateProfilePersonalForm extends Component
{
    use WithFileUploads;
    use HasProfilePhoto;
    use FormHelpersTrait;

    public $state = [];
    public $user;
    public $photo;
    public $languages;
    public $website;

    protected $listeners = ['languagesToParent'];

    public function rules()
    {
        return [
            'photo' => timebank_config('rules.profile_user.profile_photo'),
            'state.about' => timebank_config('rules.profile_user.about', 400),
            'state.about_short' => timebank_config('rules.profile_user.about_short', 150),
            'state.motivation' => timebank_config('rules.profile_user.motivation', 300),
            'languages' => timebank_config('rules.profile_user.languages', 'required'),
            'languages.id' => timebank_config('rules.profile_user.languages_id', 'int'),
            'state.date_of_birth' => timebank_config('rules.profile_user.date_of_birth', 'nullable|date'),
            'website' => timebank_config('rules.profile_user.website', 'nullable|regex:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i'),
        ];
    }

    /**
     * Prepare the component.
     *
     * @return void
     */
    public function mount()
    {
        // --- Check roles and permissions --- //
        $activeProfile = getActiveProfile();

        // Check if active profile is a User
        if (!($activeProfile instanceof \App\Models\User)) {
            abort(403, 'Unauthorized action.');
        }

        // Check if web user has permission or if it's the user's own profile
        $webUser = Auth::guard('web')->user();
        $authorized = ($webUser && (
            $webUser->can('manage users') ||
            $webUser->id === $activeProfile->id
        ));

        if (!$authorized) {
            abort(403, 'Unauthorized action.');
        }

        $this->state = Auth::user()->withoutRelations()->toArray();
        $this->website = strtolower($this->state['website']);
        $this->user = Auth::guard('web')->user();

        $this->getLanguages();
    }


    public function getLanguages()
    {
        // Create a language options collection that combines all language and competence options
        $langOptions = DB::table('languages')->get(['id','name']);
        $compOptions = DB::table('language_competences')->get(['id','name']);
        $langOptions = collect(Arr::crossJoin($langOptions, $compOptions));
        $langOptions = $langOptions->Map(function ($language, $key) {
            return [
                'id' => $key,   // index key is needed to select values in dropdown (option-value)
                'langId' => $language[0]->id,
                'compId' => $language[1]->id,
                'name' => trans($language[0]->name) . ' - ' . trans($language[1]->name),
            ];
        });

        // Create an array of the pre-selected language options
        $languages = $this->user->languages;
        $languages = $languages->map(function ($language, $key) use ($langOptions) {
            $competence = DB::table('language_competences')->find($language->pivot->competence);
            $langSelected = collect($langOptions)->where('name', trans($language->name) . ' - ' . trans($competence->name));
            return [
                $langSelected->keys()
            ];
        });
        $languages = $languages->flatten();

        // Create a selected language collection that holds the selected languages with their selected competences
        $this->languages = collect($langOptions)->whereIn('id', $languages)->values();
    }


    public function languagesToParent($values)
    {
        $this->languages = $values;
        $this->validateOnly('languages');
    }

    /**
     * Validate a single field when updated.
     * This is the 1st validation method on this form.
     *
     * @param  mixed $field
     * @return void
     */
    public function updated($field)
    {
        if ($field == 'website') {
            // If website is not empty, add URL scheme
            if (!empty($this->website)) {
                $this->website = $this->addUrlScheme($this->website);
            } else {
                // If website is empty, remove 'https://' prefix
                $this->website = str_replace('https://', '', $this->website);
            }
            strtolower($this->website);
        }

        $this->validateOnly($field);
    }

    /**
    * Update the user's profile contact information.
    *
    * @return void
    */
    public function updateProfilePersonalForm()
    {
        $user = Auth::guard('web')->user();

        if (isset($this->photo)) {
            $user->updateProfilePhoto($this->photo);
        }

        $this->validate();  // 2nd validation, just before save method

        $user->about = $this->state['about'];
        $user->about_short = $this->state['about_short'];
        $user->motivation = $this->state['motivation'];
        $user->date_of_birth = $this->state['date_of_birth'];
        $user->website =  $this->website;

        if (isset($this->languages)) {

            $languages = collect($this->languages)->Map(function ($lang, $key) use ($user) {
                return [
                    'language_id' => $lang['langId'],
                    'competence' => $lang['compId'],
                    'languagable_type' => User::class,
                    'languagable_id' => $user->id,
                ];
            })->toArray();

            $user->languages()->detach(); // Remove all languages of this user before inserting the new ones
            DB::table('languagables')->insert($languages);
        }

       

        $user->save();
        $this->dispatch('saved');
        Session(['activeProfilePhoto' => $user->profile_photo_path ]);
        redirect()->route('profile.edit');
    }

    /**
     * Delete user's profile photo.
     *
     * @return void
     */
    public function deleteProfilePhoto()
    {
        $user = Auth::guard('web')->user();

        if (! Features::managesProfilePhotos()) {
            return;
        }

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

        $defaultPath = timebank_config('profiles.user.profile_photo_path_default');

        // Delete uploaded photos (profile-photos/) and reset to default
        if (str_starts_with($user->profile_photo_path, 'profile-photos/')) {
            Storage::disk(isset($_ENV['VAPOR_ARTIFACT_NAME']) ? 's3' : config('jetstream.profile_photo_disk', 'public'))->delete($user->profile_photo_path);

            $user->forceFill([
                'profile_photo_path' => $defaultPath,
            ])->save();

            Session(['activeProfilePhoto' => $user->profile_photo_path ]);
        }
        // If current path is app-images but not the correct default, update it
        elseif (str_starts_with($user->profile_photo_path, 'app-images/') && $user->profile_photo_path !== $defaultPath) {
            $user->forceFill([
                'profile_photo_path' => $defaultPath,
            ])->save();

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

        $this->dispatch('saved');
        Session(['activeProfilePhoto' => $user->profile_photo_path ]);
        redirect()->route('profile.edit'); // Reloads the navigation bar with the new profile photo
    }


    public function addUrlScheme($url, $scheme = 'https://')
    {
        return parse_url($url, PHP_URL_SCHEME) === null ?
        $scheme . $url : $url;
    }


    /**
     * Gets the label for the "about_short" input field, including a character counter if applicable.
     * The character counter needs to use the `characterLeftCounter` method from the `FormHelpersTrait`.
     *
     * @return string The label for the "about" field, optionally including the remaining character count.
     */
    public function getAboutLabelProperty()
    {
        $maxInput = timebank_config('rules.profile_user.about_max_input');
        $baseLabel = __('Please introduce yourself in a few sentences ');
        $counter = $this->characterLeftCounter($this->state['about'] ?? '', $maxInput);

        return $counter ? $baseLabel . ' (' . $counter . ')' : $baseLabel;
    }


    /**
     * Gets the label for the "about_short" input field, including a character counter if applicable.
     * The character counter needs to use the `characterLeftCounter` method from the `FormHelpersTrait`.
     *
     * @return string The label for the "about_short" field, optionally including the remaining character count.
     */
    public function getAboutShortLabelProperty()
    {
        $maxInput = timebank_config('rules.profile_user.about_short_max_input');
        $baseLabel = __('Introduction in one sentence');
        $counter = $this->characterLeftCounter($this->state['about_short'] ?? '', $maxInput);

        return $counter ? $baseLabel . ' (' . $counter . ')' : $baseLabel;
    }

    /**
     * Gets the label for the "motivation" input field, including a character counter if applicable.
     * The character counter needs to use the `characterLeftCounter` method from the `FormHelpersTrait`.
     *
     * @return string The label for the "about_short" field, optionally including the remaining character count.
     */
    public function getMotivationLabelProperty()
    {
        $maxInput = timebank_config('rules.profile_user.motivation_max_input');
        $baseLabel = trans_with_platform('Why are you a @PLATFORM_USER@?');
        $counter = $this->characterLeftCounter($this->state['motivation'] ?? '', $maxInput);

        return $counter ? $baseLabel . ' (' . $counter . ')' : $baseLabel;
    }

    public function render()
    {
        return view('livewire.profile-user.update-profile-personal-form');
    }
}
