<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Plan extends Model {
    protected $fillable = ["name", "description", "stripe_id", "n_athletes", "n_coaches", "n_admins", "n_boxes",
        "is_highlighted", "active", "tax_prcnt", "total"];

    protected $appends = ["subtotal", "tax_amount"];

    protected $casts = [
        "is_highlighted" => "boolean",
        "active" => "boolean",
    ];

    /**
     * ATRIBUTS FICTICIS
     */
    public function getSubtotalAttribute() {
        return ( $this->total * 100 ) / ( 100 + $this->tax_prcnt);
    }

    public function getTaxAmountAttribute() {
        return $this->subtotal * ($this->tax_prcnt/100);
    }


    /**
     * RELACIONS
     */
    public function subscriptions() {
        return $this->hasMany("App\Subcription");
    }

    public function companies() {
        return $this->hasManyThrough("App\Company", "App\Subscription", "company_id", "id", "id", "plan_id");
    }

    /**
     * FUNCIONS
     */
    public function getCapacity() {
        return [
            "athlete" => $this->n_athletes,
            "coach" => $this->n_coaches,
            "admin" => $this->n_admins,
            "boxes" => $this->n_boxes
        ];
    }
}

