<?php

namespace App\Http\Controllers\Base;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\LazyLoadsRepositories;

class PageBusinessBaseController extends Controller
{
    use LazyLoadsRepositories;
    
    public $page;

    public function __construct()
    {
        parent::__construct();
        
        // Skip page resolution for CLI commands
        if (app()->runningInConsole() || php_sapi_name() === 'cli') {
            $this->page = null;
            return;
        }
        
        $slug = \Request::segment(2);
       // $this->page = $this->pageRepository()->get($slug);
		$this->page = $this->pageRepository()->getBySlug($slug);
		
        if ($this->page) {
            $this->theme->share('page', $this->page);
            $this->theme->share('feviconImage', $this->page->present()->getFeviconAvatar());
            
            // Only proceed with community logic if page exists
            $community = app('App\\Repositories\\CommunityRepository')->getById($this->page->community_id);
            if (! $community) {
                $community = app('App\\Repositories\\CommunityRepository')->publicBusinessCommunity();
            }
        } else {
            // Safe redirect that doesn't execute during CLI
            if (!app()->runningInConsole() && php_sapi_name() !== 'cli') {
                redirect(config('app.url'))->send();
            }
            return;
        }
        if (! $community) {
            $community = app('App\\Repositories\\CommunityRepository')->publicBusinessCommunity();
        }

        /* if(!\Auth::check() and $community){ */
        if ($community) {
            $urlHostname = getUrlHostname($community->present()->getCommunityDomain());
            if ($urlHostname != \Request::getHost()) {
                // Safe redirect that doesn't execute during CLI
                if (!app()->runningInConsole() && php_sapi_name() !== 'cli') {
                    redirect($community->present()->getCommunityDomain('store/'.$this->page->slug))->send();
                }
            }
        }
    }

    public function exists()
    {
        if (isset($this->page) and $this->page->community_id != 0) {
            return $this->page;
        } else {
            return false;
        }
    }

    public function render($path, $param = [], $setting = [])
    {
        $predefinedSettings = [
            'title' => ($this->page->seo_meta_title) ? $this->page->seo_meta_title : $this->setTitle(),
        ];

        if ($this->exists()) {

            $predefinedSettings = array_merge($predefinedSettings, ['design' => $this->page->present()->readDesign()]);
        }

        $settings = array_merge($predefinedSettings, $setting);

        if (config('page-design') and isset($settings['design'])) {
            extract($settings['design']);

            /*if(strpos($bg_image, 'themes/frontend/default/assets/images/background.jpg') !== false){
                $defaultImage="themes/frontend/default/assets/images/design/bg/red-flower.jpg";
                $bgImage ='background-image:url('.\Image::url($defaultImage).');';
            }else{*/
            $bgImage = (! empty($bg_image)) ? 'background-image:url('.\Image::url($bg_image).') !important;' : null;
            // }

            $this->theme->asset()->afterStyleContent('
                body{
                    '.$bgImage.'
                    background-position: '.$bg_position.';
                    background-color: '.$bg_color.';
                    background-repeat: '.$bg_repeat.';
                    background-attachment : '.$bg_attachment.';
					background-size : cover;
                }

                a {
                    color : '.$link_color.';
                }

                .page-content{
                    background-color: '.$content_bg_color.';
                }
            ');
        }

        return parent::render('page.business.layout', ['content' => $this->theme->section($path, $param)], $settings);

    }

    public function notFound()
    {
        // if ($this->page and !$this->community->present()->canView()) return \Redirect::route('communities');
        return $this->theme->section('error-page');
    }

    public function setTitle($title = null)
    {
        if (! $this->exists()) {
            return parent::setTitle($title);
        }
        $title = $this->page->title.((! empty($title)) ? ' - '.$title : null);

        return parent::setTitle($title);
    }
}

