<?php

namespace App\Http\Controllers\Base;

class ProfileBaseController extends UserBaseController
{
    public $profileUser;

    public function __construct()
    {
        parent::__construct();
        
        // Skip profile resolution for CLI commands (like route:list, artisan commands)
        if (app()->runningInConsole() || php_sapi_name() === 'cli') {
            $this->profileUser = null;
            return;
        }
        
        $this->userid = \Request::segment(1);

        $this->profileUser = $this->userRepository()->getProfileUser($this->userid);
        $this->theme->share('profileUser', $this->profileUser);

        if (\Auth::check()) {
            $this->theme->share('viewerUser', \Auth::user());

            $cmId = getVisitingCommunityId();
            $cmntData = app('App\\Repositories\\CommunityRepository')->getById($cmId);

            if ($cmntData and $cmntData->gift_app_community == 1 || $this->selected_pricing_plan_type=='giftapp') {
                redirect($cmntData->present()->url())->send();
            }
        } else {

            if ($this->profileUser) {

                if (config('community-domain.domain-url') != \Request::getHost()) {
                    $community = app('App\\Repositories\\CommunityRepository')->getByDomain(\Request::getHost());
                    if ($community) {

						$selectedPlanDetails=$this->pricingPlansRepository()->getPlanByPlanId($community->pricing_plan);	
						if($selectedPlanDetails){
							$pricing_plan_type=$selectedPlanDetails->plan_type;
						}	
					
                        if ($community->race_community == 1 || $pricing_plan_type=='racing') {
                            $urltype = 'returnurl';
                            $urltypeid = '';
                            $currentUrl = \URL::to('/').$_SERVER['REQUEST_URI'];

                            $racingCommunityUrl = $community->present()->authUrl('login', ['urltype' => $urltype, 'urltypeid' => $urltypeid, 'returnUrl' => $currentUrl]);

                            redirect($racingCommunityUrl)->send();
                        }
                    }
                }

                $socialProfilePageURL = $this->profileUser->present()->socialProfilePageURL();

                if ($this->profileUser->present()->url() != $socialProfilePageURL and config('community-domains-own-url') == 'yes') {
                    redirect($socialProfilePageURL)->send();
                }

                $primaryCommunityId = '';

                if ($this->profileUser->community_signup != 0) {
                    $primaryCommunityId = $this->profileUser->community_signup;
                } elseif ($this->profileUser->primary_community != 0) {
                    $primaryCommunityId = $this->profileUser->primary_community;
                }

                if ($primaryCommunityId and config('community-domains-own-url') == 'yes') {
                    $community = app('App\\Repositories\\CommunityRepository')->getBySlug($primaryCommunityId);
                    if ($community) {
                        $this->communityOutsideTheme($community);
                    }

                    if ($community and $community->gift_app_community == 1) {
                        redirect($community->present()->url())->send();
                    }
                }
            }
        }

        $this->setTitle();
    }

    public function render($path, $param = [], $setting = [])
    {
        $predefinedSettings = [
            'title' => '',
        ];

        if ($this->exists()) {

            $predefinedSettings = array_merge($predefinedSettings, ['design' => $this->profileUser->present()->readDesign()]);
        }

        $settings = array_merge($predefinedSettings, $setting);

        if (! $this->exists()) {
            return parent::render($path, $param, $settings);
        } elseif (! $this->profileUser->present()->canViewMe()) {
            return parent::render('profile.layout', ['content' => $this->theme->section('profile.not-viewable'), 'error' => true], $settings);
        } else {
            return parent::render('profile.layout', ['content' => $this->theme->section($path, $param)], $settings);
        }

    }

    public function profileNotFound()
    {
        if (config('disable-guest-access-profile') and ! \Auth::check()) {
            return redirect('');
        }

        return $this->theme->section('error-page');
    }

    public function exists()
    {
        if (config('disable-guest-access-profile') and ! \Auth::check()) {
            return false;
        }

        return $this->profileUser;
    }

    public function setTitle($title = null)
    {
        if (! $this->exists()) {
            return parent::setTitle($title);
        }
        $title = (! empty($title)) ? '-'.$title : null;
        $title = $this->profileUser->fullname.$title;

        return parent::setTitle($title);
    }
}
