<?php

namespace App\Repository;

use App\Packages;
use App\Categories;
use Carbon\Carbon;
use App\Settings;
use App\Excesses;
use App\Facilities;
use App\WebsiteFaqs;
use App\Widgets;

class PackagesRepository {

    CONST CACHE_KEY = 'PACKAGES';

    public function all() {

        $data = array();
        $data["categories"] = Categories::where("status", 1)->get();
        $data["excesses"] = Excesses::where("status", 1)->get();
        return view('front.packages.categories', $data);
    }

    public function getPackages($category_id) {

        $model = Packages::where("status", 1)->where("category_id", $category_id)->orderBy("position", "asc")->get();
        $data["excesses"] = Excesses::where("status", 1)->orderBy("position", "asc")->get();
//        $fa= Facilities::where("status", 1)->orderBy("position", "asc")->get();
//       $articles =DB::table('categories')
//                ->join('facilities', 'facilities.id', '=', 'categories.facility_id')
//                ->select('*')
//                ->get();
//       d($articles,1);

        $packages = array();
        $fac = array();
        $i = 0;
        foreach ($model as $m) {
            //echo $m->category->facility_id;            
            $fac = Facilities::where("status", 1)->orderBy("position", "asc")->get();
            $id = $m->id;
            $data["facilities"] = $fac;
            $packages[$id] = $this->setPackage($m);
            $i++;
        }        
        $data["packages"] = $packages;
        $data["category_id"] = $category_id;
        return view('front.packages.list', $data);
    }

    public function getPackage($code1, $code2) {

        $model = Packages::where("url", $code1 . '/' . $code2)->first();


        if (!empty($model) && $code1 == $model->category->code) {
            $data["excesses"] = Excesses::where("status", 1)->orderBy("position", "asc")->get();


//        $data["facilities"] = Facilities::where("status", 1)->orderBy("position", "asc")->get();
            $fa = array();

            foreach ($model->facilities as $f) {
                $facilities = Facilities::where('id', $f->facility_id)->where("status", 1)->orderBy("position", "asc")->first();
                $fa[$f->facility_id] = $facilities;
            }

            $data["facilities"] = $fa;

            $i = 0;
            $package = $this->setPackage($model);

            $data["sec9"] = Widgets::where('code', 'index_sec_9')->first();
            $data["sec4"] = Widgets::where('code', 'index_sec_4')->first();
            $data["sec5"] = Widgets::where('code', 'index_sec_7')->first();
            $data["package"] = $package;

            return view('front.packages.package', $data);
        } else {
            abort(404);
        }
    }

    public function setPackage(Packages $m) {

        $packages["id"] = $m->id;
        $packages["title"] = $m->title;
        $packages["tagline"] = $m->tagline;
        $packages["tagline2"] = $m->tagline2;
        $packages["shortDescription"] = $m->shortDescription;
        $packages["description"] = $m->description;
        $packages["code"] = $m->code;
        $packages["url"] = $m->url;
        $packages["metaTitle"] = $m->metaTitle;
        $packages["metaDescription"] = $m->metaDescription;
        $packages["keywords"] = $m->keywords;
        $packages["category_id"] = $m->category_id;
        $packages["category"] = $m->category->title;
        $packages["category_code"] = $m->category->code;
        $packages["categoryDescription"] = $m->category->description;
        $packages["notCoveredTitle"] = $m->notCoveredTitle;
        $packages["notCoveredText"] = $m->notCoveredText;


        $packages["faqs"] = WebsiteFaqs::where("type", "packages")->where("type_id", $packages["id"])->where("status", 1)->get();

        $packages["fileDownloadText"] = $m->fileDownloadText;
        $packages["file"] = $m->file;

        $packages["detail"] = addslashes($m->detail);
        
        $packages["image"] = $m->image;
        $packages["review"] = addslashes($m->review);
        $packages["faqTitle"] = addslashes($m->faqTitle);
        $packages["faqText"] = addslashes($m->faqText);
        $package["facilities"] = array();
        $j = 0;
        $customizable = false;
        $packages["default_facilities"]=array();
        foreach ($m->facilities as $f) {          
            if ($f->hasPrice == 0 || empty($f->price)) {
                $packages["default_facilities"][$f->facility_id] = $f;
            }           
            if ($customizable == false && $f->hasPrice == 1 && !empty($f->price)) {
                $customizable = true;
            } 
            //$packages["facilities"][$j] = $f->facility_id;
            $packages["facilities"][$f->facility_id] = $f;
            $j++;
        }
        $packages["customizable"] = $customizable;
        $j = 0;
        $price = 0;
        foreach ($m->prices as $p) {
            
            $packages["prices"][$p->type][$p->excess_id]["excess_id"] = $p->excess_id;
            $packages["prices"][$p->type][$p->excess_id]["price"] = numberformat($p->price);
            $packages["prices"][$p->type][$p->excess_id]["vat"] = $p->vat;
            $packages["prices"][$p->type][$p->excess_id]["total_price"] = numberformat($p->price + ($p->price * ($p->vat / 100)));

            if ($j == 0) {
                $packages["starting_price"] = numberformat($p->price + ($p->price * ($p->vat / 100)));
                $packages["starting_excess_id"] = $p->excess_id;
                $packages["starting_excess"] = $p->type;
                $packages["starting_vat"] = numberformat($p->vat);
            }
            $j++;
        }
        

        return $packages;
    }

    public function getCacheKey($key) {
        $key = strtoupper($key);

        return self::CACHE_KEY . $key;
    }

    public function get($code) {
        $key = "all.{$code}";
        $cacheKey = $this->getCacheKey($key);
        return cache()->remember($cacheKey, Carbon::now()->addMinutes(10), function() use($code) {
                    return Settings::where('code', $code)->first();
                });
    }

}
