<?php

namespace App\Actions\Fortify;

use App\Http\Helpers\FileUploader;
use App\Http\Helpers\UploadedFiles;
use App\Models\Engineer;
use App\Models\Firm;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;

class UpdateUserProfileInformation
{

    public static function updateUser($user, array $input)
    {
        $sanitised = Validator::make($input, [
            'name' => ['nullable', 'string', 'max:255'],
            'email' => ['nullable', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
            'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
            'cover_photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:2056'],
            'tag_line' => ['nullable', 'string', 'max:255'],
            'slug' => ['nullable', 'string', 'max:255', 'regex:/^[a-zA-Z0-9-_.]+$/', Rule::unique('users')->ignore($user->id)],
            'socials' => ['nullable', 'array'],
            ])->validateWithBag('updateProfileInformation');
        if (array_key_exists('email',$sanitised) && $user->email===preg_replace('/\s+/', '', $sanitised['email'])){
            unset($sanitised['email']);
        }
        else if(array_key_exists('email',$sanitised) && $sanitised['email']!=null){

            if(Engineer::where('email',$input['email'])->where('user_id','!=',$user->id)->first()!=null || Firm::where('email',$input['email'])->where('user_id','!=',$user->id)->first()!=null){
                return back(303)->with(['error' => "Can not update email, Email already in use"]);
            }else{
                $user->update([
                    'email' => $input['email'],
                    'email_verified_at' => null,
                ]);
                $user->sendEmailVerificationNotification();
                if ($user->firm!=null){
                    $user->firm->email = $input['email'];
                    $user->firm->save();
                }
                if($user->engineer!=null){
                    $user->engineer->email = $input['email'];
                    $user->engineer->save();
                }
                unset($sanitised['email']);
            }
        }
        if(array_key_exists('tag_line',$sanitised) &&$sanitised['tag_line']!=null){

                if ($user->firm!=null){
                    $user->firm->tag_line = $input['tag_line'];
                    $user->firm->save();
                }
                if($user->engineer!=null){
                    $user->engineer->tag_line = $input['tag_line'];
                    $user->engineer->save();
                }

        }
        if(array_key_exists('name',$sanitised) &&$sanitised['name']!=null){

            if ($user->firm!=null){
                $user->firm->name = $input['name'];
                $user->firm->save();
            }
            if($user->engineer!=null){
                $user->engineer->name = $input['name'];
                $user->engineer->save();
            }

        }
        if (isset($sanitised['slug'])) {
            $sanitised['slug'] = strtolower($sanitised['slug']);
        }

        if (isset($sanitised['photo'])) {
            $user->updateProfilePhoto($sanitised['photo']);
            unset($sanitised['photo']);
        }

        if (isset($sanitised['cover_photo'])) {
            $user->updateCoverPhoto($sanitised['cover_photo']);
            unset($sanitised['cover_photo']);
        }

        if(isset($sanitised['socials'])) {
            $user->addSocials($sanitised['socials']);
            unset($sanitised['socials']);
        }

        $user->update([
            ...$sanitised
        ]);
    }


    /**
     * Validate and update the given user's profile information.
     *
     * @param mixed $user
     * @param array $input
     * @return void
     */
    public function update($user, array $input)
    {
        $sanitised = Validator::make($input, [
            'name' => ['nullable', 'string', 'max:255'],
            'email' => ['nullable', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
            'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
            'tag_line' => ['nullable', 'string', 'max:255'],
            'slug' => ['nullable', 'string', 'max:255', 'regex:/^[a-zA-Z0-9-_.]+$/', Rule::unique('users')->ignore($user->id)],
        ])->validateWithBag('updateProfileInformation');

        if (isset($sanitised['slug'])) {
            $sanitised['slug'] = strtolower($sanitised['slug']);
        }
        if (isset($sanitised['photo'])) {
            $user->updateProfilePhoto($sanitised['photo']);
            unset($sanitised['photo']);
        }

        $user->forceFill([
            ...$sanitised
        ])->save();
    }


    /**
     * Update the given verified user's profile information.
     *
     * @param mixed $user
     * @param array $input
     * @return void
     */
    protected function updateVerifiedUser($user, array $input)
    {
        $user->forceFill([
            'name' => $input['name'],
            'email' => $input['email'],
            'email_verified_at' => null,
        ])->save();

        $user->sendEmailVerificationNotification();
    }
}
