<?php

namespace App\Http\Controllers\User;

use Request;
use App\User;
use Response;
use Validator;
use App\Services\Util;
use App\Models\UserShop;
use App\Models\PromotionUser;
use App\Services\UserService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Repositories\UserShopRepository;

class ShopController extends Controller
{
    protected $userShopRepo;

    public function __construct(UserShopRepository $userShopRepo)
    {
        $this->middleware('active.account');
        $this->userShopRepo = $userShopRepo;
    }

    public function index(){

        $user = Auth::user();

       // $user_shop = UserShop::where('user_id', Auth::user()->id)->first();
        if(!$user->shop){
            //create new shop
            $user = $this->createNewUserShop(Auth::user());
        }
        if($user->shop->user_id != Auth::user()->id){
            abort(404);
        }
        $data = [
            'user_shop' => $user->shop,
        ];

        return view('user.shop.detail', $data);
    }
    private function createNewUserShop($user){
        return $this->userShopRepo->create($user);
    }

    public function store()
    {
        $data = Request::all();
        $user = Auth::user();

        if(isset($data['action']) && $data['action'] === 'save-user-shop'){
            $rules = array(
                'name' => 'required',
                'user_shop_url' => ' required|alpha_dash|'.'unique:user_shops,url,'.$user->shop->id.',id'.'|min:4|max:20|full_word_check', 
            );
            Validator::extend('full_word_check', function ($attribute, $value, $parameters, $validator) {
                $profanity = \App\Models\Setting::getContentBySlug('promotion_user_url_profanity');
                $profanity = array_map('trim', explode(',', $profanity));
                if(in_array($value, $profanity)){
                    return false;
                }
                return true;
            });
            $validator = Validator::make(Request::all(), $rules);
            if ($validator->fails()) {
                return redirect(route('user_shop'))->withErrors($validator)->withInput(Request::all());
            }
            $model = $this->userShopRepo->update($user->shop->id, Request::all());
        }
        \Session()->flash('alert-save', true);
        return redirect(route('user_shop'));
    }

    public function load(){
        $data = Request::all();

        if(Request::ajax()) {
            if(isset($data['action']) && $data['action'] === 'validate_url'){
                $unique = 'unique:user_shops,url';
                if(isset($data['usid'])){
                    $unique .= ','.$data['usid'].',id';
                }
                $rules = array(
                    'user_shop_url' => ' required|alpha_dash|'.$unique.'|min:4|max:20|full_word_check', 
                );
                Validator::extend('full_word_check', function ($attribute, $value, $parameters, $validator) {
                    $profanity = \App\Models\Setting::getContentBySlug('promotion_user_url_profanity');
                    $profanity = array_map('trim', explode(',', $profanity));
                    if(in_array($value, $profanity)){
                        return false;
                    }
                    return true;
                });
                $validator = Validator::make(Request::all(), $rules);

                if ($validator->fails()) {
                    //$messages = $validator->messages();
                    return Response::json(array(
                        'success' => false,
                        'errors' => $validator->getMessageBag()->toArray()

                    ));
                }
                //$slug = SlugService::createSlug(UserShop::class, 'slug', Request::get('user_promotion_url'));
                $name = Util::sanitize(Request::get('user_shop_url'), true, false, true, true);
            
                return Response::json(array(
                    'success' => true,
                    'preview_user_shop_url' => config('app.shop_url')."/".$name,
                ));
            }
        }   
     }

    
}