<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\Web\ProfileCompanyRequest;
use App\Http\Requests\Web\ProfileRequest;
use App\Http\Requests\Web\ProfileUserRequest;
use App\Http\Requests\Web\VerificationCompanyRequest;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Models\City;
use App\Models\Country;
use App\Models\Notification;
use App\Models\User;
use App\Models\UserDescription;
use App\Models\UserLink;
use App\Models\UserPhoto;
use App\Services\Response\ResponseService;
use App\Services\Upload\ImageService;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    private $view = 'web.pages.profile.';
    private $viewAjax = 'web.pages.profile.components.';

    public function profile(Request $request): View
    {
        $title = __('web.profile');

        $user = Auth::user();
        if($user->role == 'company')
        {
            $user = User::withDescription()->find(Auth::user()->id);
            $action = route('edit_profile.company');
            $verification_action = route('verification.company');
            $photos = UserPhoto::where('user_id',$user->id)->cursor();
            $links = UserLink::where('user_id',$user->id)->cursor();
        }
        else
        {
            $action = route('edit_profile.user');
        }
        $countries = Country::withDescription()->cursor();
        $cities = $user->country_id ? City::withDescription()->where('cities.country_id',$user->country_id)->cursor() : [];

        return view($this->view.'profile', get_defined_vars());
    }
    public function verification(VerificationCompanyRequest $request)
    {
        if($request->hasFile('verification_file'))
        {
            $pdf = ImageService::uploadImage($request->verification_file);
        }
        else
        {
            $pdf = ImageService::uploadImage($request->verification_file_2);
        }
        $user = Auth::user();
        $user->update(['verification_pdf'=>$pdf]);
        $response =
            [
                'message' => __('web.wait_management_file'),
                'data' =>url(getCurrentLocale().'/profile'),
                'status' => 200,
            ];
        return ResponseService::response($response);
    }
    public function editProfile(ProfileUserRequest $request)
    {
        $user = Auth::user();
        
        if($request->password) {
            $request->merge(['password'=>bcrypt($request->password)]);
        } else {
            unset($request['password']);
        }

        $user->update($request->all());
        if($request->hasFile('photo'))
        {
            $image = ImageService::uploadImage($request->photo);
            $user->update(['photo'=>$image]);
        }
        $response =
            [
                'message' => __('web.success'),
                'data' =>url(getCurrentLocale().'/profile'),
                'status' => 200,
            ];
        return ResponseService::response($response);
    }

    public function editProfileCompany(ProfileCompanyRequest $request)
    {
        $user = Auth::user();
        if($request->hasFile('photo'))
        {
            $image = ImageService::uploadImage($request->photo);
            $request->merge(['photo'=>$image]);
        }
        if($request->hasFile('certificate_commercial'))
        {
            $image = ImageService::uploadImage($request->certificate_commercial);
            $request->merge(['certificate_commercial'=>$image]);
        }
        if($request->hasFile('tax_certificate'))
        {
            $image = ImageService::uploadImage($request->tax_certificate);
            $request->merge(['tax_certificate'=>$image]);
        }
        if($request->hasFile('company_exhibition'))
        {
            $image = ImageService::uploadImage($request->company_exhibition);
            $request->merge(['company_exhibition'=>$image]);
        }
        if($request->hasFile('company_showroom'))
        {
            $image = ImageService::uploadImage($request->company_showroom);
            $request->merge(['company_showroom'=>$image]);
        }
        if($request->password) {
            $request->merge(['password'=>bcrypt($request->password)]);
        } else {
            unset($request['password']);
        }

        $user->update($request->all());
        UserDescription::where('user_id',$user->id)->delete();
        UserLink::where('user_id',$user->id)->delete();
//        UserPhoto::where('user_id',$user->id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'user_id'=> $user->id,
                'language_id'=> $lang->id,
                'name'=> $request['name_'.$lang->local],
                'purchasing'=> $request['purchasing_'.$lang->local],
            ];
            UserDescription::create($data);
        }
        if(!is_null($request['links']))
        {
            foreach ($request['links'] as $link)
            {
                if($link)
                {
                    UserLink::create([
                        'user_id'=>$user->id,
                        'link'=>$link
                    ]);
                }
            }
        }
        if(!is_null($request['photos']))
        {
            foreach ($request['photos'] as $photo)
            {
                $image = ImageService::uploadImage($photo);
                UserPhoto::create([
                    'user_id'=>$user->id,
                    'photo'=>$image
                ]);
            }
        }
        $response =
            [
                'message' => __('web.success'),
                'data' =>url(getCurrentLocale().'/profile'),
                'status' => 200,
            ];
        return ResponseService::response($response);
    }


    public function orders()
    {
        $count = Order::where('user_id',Auth::user()->id)->count();
        return view($this->viewAjax.'orders', get_defined_vars());
    }
    public function notifications()
    {
        $user = Auth::user();
        $notifications = Notification::withDescription()->where('notifications.user_id',$user->id)->cursor();

        foreach($notifications as $notification)
        {
            $notification->update(['read_at'=>date('Y-m-d H:i:s')]);
        }
        return view($this->viewAjax.'notifications', get_defined_vars());
    }

    public function showOrder(Request $request)
    {
        $user = Auth::user();
        $query = Order::query()
            ->where('user_status','user')
            ->where('user_id',$user->id);
        if($request->status != 'all')
        {
            $query->where('status',$request->status);
        }
        $orders = $query->orderByDESC('id')->cursor();
        return view($this->viewAjax.'orders_show', get_defined_vars());
    }
}
