<?php

namespace common\logic\Product\ProductPrice\Service;

use common\logic\AbstractBuilder;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\ProductPrice\Model\ProductPrice;
use common\logic\User\User\Model\User;
use common\logic\User\UserGroup\Model\UserGroup;

class ProductPriceBuilder extends AbstractBuilder
{
    public function instanciateProductPrice(
        Product $product,
        float $price,
        User $user = null,
        UserGroup $userGroup = null,
        PointSale $pointSale = null,
        float $quantityFrom = null): ProductPrice
    {
        $productPrice = new ProductPrice();

        $productPrice->populateProduct($product);
        $productPrice->price = $price;

        if($user) {
            $productPrice->populateUser($user);
        }

        if($userGroup) {
            $productPrice->populateUserGroup($userGroup);
        }

        if($pointSale) {
            $productPrice->populatePointSale($pointSale);
        }

        if($quantityFrom) {
            $productPrice->quantity_from = $quantityFrom;
        }

        return $productPrice;
    }

    public function createProductPrice(): ProductPrice
    {
        $productPrice = $this->instanciateProductPrice();
        $this->saveCreate($productPrice);

        return $productPrice;
    }
}