<?php

namespace MGModule\DNSManager2\models\custom\package\item;

use \Exception;
use \MGModule\DNSManager2 as main;

/**
 * Log class
 * 
 * @Table(name=packageitem)
 */
class PackageItem extends main\mgLibs\models\orm
{
    /**
     * ID field
     * 
     * @Column(id)
     * @var int 
     */
    public $id;
    
    /**
     *
     * @Column(int=11, refrence=models\custom\package\Package::id,uniqueKey=1,isKey=1)
     * @var int 
     */
    public $packageid;
    
    /**
     *
     * @Column(int,uniqueKey=1,isKey=1)
     * @var string 
     */
    public $type;
    
    /**
     *
     * @Column(varchar,uniqueKey=1,isKey=1)
     * @var string 
     */
    public $relid;
    
    /**
     *
     * @Column(int)
     * @var string 
     */
    public $limit;
    
    public function setType($type) {
        if(!PackageItemTypeEnum::isValidValue($type))
            throw new Exception ("Wrong type provided");
        $this->type = $type;
    }
    
    public function save($data = array()) {
        if(!PackageItemTypeEnum::isValidValue($this->type)) {
            throw new Exception('Invalid Package Item Type ('. $this->type .')');
        }
        parent::save($data);
    }
    
    public function getPackage() {
        return new main\models\custom\package\Package($this->packageid);
    }
    
    public function getRelatedItem() {
        switch ($this->type) {
            case PackageItemTypeEnum::DOMAIN:
                return main\models\whmcs\domains\pricing\repository::factory()->byExtension($this->relid)->one();
                
            case PackageItemTypeEnum::PRODUCT:
                return main\models\whmcs\product\product::factory($this->relid);
                
            case PackageItemTypeEnum::ADDON:
                return main\models\whmcs\addon\addon::factory($this->relid);
                
            case PackageItemTypeEnum::OTHER:
                return new main\mgLibs\custom\relateditem\Other();
        }
    }
  
}
