<?php

namespace App\Http\Controllers\Client;

use App\Models\Client;
use App\Models\ClientProfile;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Subscription;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class ClientProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit()
    {   $company_id = Auth::user()->company->id;
        $subscription = Subscription::where('company_id',$company_id)->firstOrFail();
        $user = Client::findOrFail(Auth::user()->id);
        $profile = ClientProfile::where('client_id', $user->id)->first();
        $period = Carbon::parse($subscription->end_date)->diffInDays(Carbon::parse($subscription->start_date));
        $spend = Carbon::parse(Carbon::now())->diffInDays($subscription->start_date);
        return view('client.profiles.edit', compact('user', 'profile','subscription','period','spend'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
        $this->validate($request, [
            'name'=>'required',
            'email'=>'required|email|unique:clients,email,'.Auth::user()->id,
            'city_name' => 'required',
            'age' => 'required',
            'gender' => 'required'
        ]);
        


        $user = Client::findOrFail(Auth::user()->id);
        $user->update($request->all());

        if (Auth::user()->email !== $request->email) {

            $user->update(['email_verified_at' => null]);
        }


        $profile = ClientProfile::where('client_id', $user->id)->first();
        $profile->update($request->all());

        if ($request->hasFile('profile_pic')) {
            $profile_pic = $request->file('profile_pic');
            $fileName = $profile_pic->getClientOriginalName();
            $uploadDir = 'uploads/profiles/Clients/' . $user->id;
            $profile_pic->move($uploadDir, $fileName);
            $profile->profile_pic = $uploadDir . '/' . $fileName;
            $profile->save();

        }

        return response()->json(['message' => 'تم تعديل بياناتك بنجاح','status' => true]);
    }

    public function password(Request $request){

        $this->validate($request, [
            'currentPassword' => 'required',
            'password' => 'required|confirmed|min:6',

        ]);

        $user = Client::findOrFail(Auth::user()->id);
        if (Hash::check($request->currentPassword, $user->password)) {

        if(Hash::check($request->password, $user->password)){

            return response()->json(['message' => 'كلمة المرور الجديدة يجب ان تكون مختلفة عن كلمة المرور القديمة','status' => false]);
       
        }else{
        $user->password = Hash::make($request->password);
        $user->save();
        return response()->json(['message' => 'تم تعديل كلمة المرور بنجاح','status' => true]);
        }
    }else{

        return response()->json(['message' => 'كلمة المرور الحالية غير صحيحة','status' => false]);

    }

}

}
