<?php

namespace App;

use App\Extensions\CustomCashierUserSubscription;
use App\Repositories\BoxOrderLineRepository;
use App\Repositories\PaymentRepository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class UserSubscription extends CustomCashierUserSubscription {
    protected $fillable = [
        'pricing_id','user_id', 'role_id', 'start_date', 'end_date', 'price', 'wod_counter', 'open_counter',
        'total_counter', 'renew', "stripe_id"
    ];

    protected $casts = [
        "start_date" => "date",
        "end_date" => "date",
        "active" => "boolean",
        'renew' => 'boolean',
        "ends_at" => "date"
    ];

    protected $appends = ["active", "box_ids"];

    protected $with = ["role", "pricing"];

    public function getActiveAttribute() {

        $now = Carbon::now();
        if($this->end_date != null && $this->start_date != null) {
            $extra = 0;
            foreach($this->boxes() as $b) {
                $extra = $b->user_subscription_expires_after_days > $extra ? $b->user_subscription_expires_after_days : $extra;
            }
            $real_expiration_date = Carbon::createFromFormat("Y-m-d H:i:s", $this->end_date)->addDays($extra);
            return
                    ($this->start_date <= $now  &&  $real_expiration_date >= $now)  //te en compte els dies
                    ? 1 : 0;
        }
        else {
            return ($this->start_date && $this->start_date <= $now )? 1 : 0;
        }
    }

    public function getBoxIdsAttribute() {
        $res = [];

        $boxes = [];
        if($this->pricing_id != null) {
            $res = [];
            if($this->role && $this->role->slug != "super_admin" && $this->role->slug != "company_admin") {
                $boxes = $this->pricing->boxes; //a partir de la taula pricing_boxes
            }
            else {
                //es company admin
                $boxes = $this->company->boxes; //tots
            }
        }

        foreach($boxes as $b) {
            array_push($res, $b->id);
        }
        return $res;
    }

    public function user(){
        return $this->belongsTo("App\User");
    }

    //no tocar!! utilitza laravel cashier
    public function owner(){
        return StripeUser::where([["company_id", $this->pricing->company_id], ["user_id", $this->user_id]])->first();
    }

    public function pricing(){
        return $this->belongsTo("App\Pricing")->without("company");
    }

    public function role(){
        return $this->belongsTo("App\Role", "role_id");
    }

    public function company() { //NO TOCAR!!!!!!!!
        return $this->hasOneThrough("App\Company",
                                    "App\Pricing",
                                    "id",
                                    "id",
                                    "pricing_id",
                                    "company_id");
        //return $this->pricing_id ? $this->pricing->company : null;
    }

    public function box_order_lines() {
        return $this->hasMany("App\BoxOrderLine");
    }

    public function box_orders() { //PROVAT!
        return $this->hasManyThrough("App\BoxOrder", "App\BoxOrderLine", "user_subscription_id", "id", "id", "box_order_id")
                    ->orderByDesc("created_at");
    }

    public function boxes() {
        $boxes = array();

        if($this->pricing_id != null) {
            $boxes = Box::whereHas("pricing", function($query) {
                $query->where("pricings.id", $this->pricing_id);
            })->get();
        }

        return $boxes;
    }
}
