<?php

namespace Modules\User\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Modules\Country\Services\CountryService;
use Modules\User\Enums\UserGenderEnum;
use Modules\User\Requests\Web\ProfileRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;

class ProfileController extends Controller
{
    private $viewDirectory = 'web.pages.profile.';

    public function __construct(
        private UserService $userService,
        private AccountService $accountService,
        private CountryService $countryService,
    ){}



    public function profile()
    {
        $title = __('web.profile');
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $user_data = [
            $user->email,
            $user->name,
            $user->last_name,
            $user->phone,
            $user->birth_date,
            $user->gender,
            $user->country_id,
        ];
        $countFilled = count(array_filter($user_data, function($value) {
            return $value != null;
        })) ;
        $percentage = ceil( ( $countFilled / count($user_data) ) * 100);

        $genders = UserGenderEnum::values();
        $female = UserGenderEnum::FEMALE->value;
        $countries = $this->countryService->get();
        return view($this->viewDirectory.'profile',get_defined_vars());
    }
    public function orders()
    {
        $title = __('web.orders');
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        return view($this->viewDirectory.'orders',get_defined_vars());
    }

    public function editProfile(ProfileRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $this->userService->update($request->validated(),$user->id);
        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data'=> route('web.profile')
        ]);
    }

}
