<?php


namespace App\Repositories;


use App\Pricing;

class PricingRepository implements RepositoryInterface {
    public static function all() {
        return Pricing::all();
    }

    public static function create(array $data, array $box_ids = null) {
        $p = Pricing::create($data);
        if($box_ids!=null) $p->boxes()->sync($box_ids);

        return $p;
    }

    public static function update(array $data, $id, array $box_ids = null) {
        $p = Pricing::findOrFail($id);
        $p->update($data);

        if($box_ids != null) $p->boxes()->sync($box_ids);

        return $p;
    }

    public static function delete($id){
        return Pricing::destroy($id);
    }

    public static function find($id){
        return Pricing::findOrFail($id);
    }

    public static function company($id) {
        return Pricing::findOrFail($id)->company;
    }

    public static function userSubscriptions($id, $active = false) {
        $user_subscriptions = Pricing::findOrFail($id)->user_subscriptions;
        if($active) {
            $temp = $user_subscriptions;
            $user_subscriptions = [];
            foreach($temp as $s) {
                if($s->active == true) array_push($user_subscriptions, $s);
            }
        }
        return $user_subscriptions;
    }

    public static function boxes($id) {
        return Pricing::findOrFail($id)->boxes;
    }

    public static function box_ids($id) {
        $res = [];
        foreach(PricingRepository::boxes($id) as $b) {
            array_push($res, $b->id);
        }
        return $res;
    }

}
