<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */
declare(strict_types=1);

namespace PrestaShop\Module\AgileMultisellerPro\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * SellerinfoRating
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="PrestaShop\Module\AgileMultisellerPro\Repository\SellerinfoRatingRepository")
 * @ORM\HasLifecycleCallbacks
 */
class SellerinfoRating
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id_sellerinfo_rating", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $idSellerinfoRating;

    /**
     * @var Sellerinfo
     *
     * @ORM\ManyToOne(targetEntity="PrestaShop\Module\AgileMultisellerPro\Entity\Sellerinfo", inversedBy="sellerRating")
     * @ORM\JoinColumn(name="id_sellerinfo", referencedColumnName="id_sellerinfo", nullable=false, onDelete="CASCADE")
     */
    private $sellerInfo;

    /**
     * @var int
     *
     * @ORM\Column(name="id_order", type="integer", nullable=false)
     */
    private $idOrder;

    /**
     * @var int
     *
     * @ORM\Column(name="id_lang", type="integer", nullable=false)
     */
    private $idLang;

    /**
     * @var int
     *
     * @ORM\Column(name="id_customer", type="integer", nullable=false)
     */
    private $idCustomer;

    /**
     * @var string
     *
     * @ORM\Column(name="customer", type="string", length=255, nullable=false)
     */
    private $customer;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text", length=65535, nullable=false)
     */
    private $content;

    /**
     * @var float
     *
     * @ORM\Column(name="grade", type="float", precision=10, scale=0, nullable=false)
     */
    private $grade;

    /**
     * @var string|null
     *
     * @ORM\Column(name="ip_address", type="string", length=64, nullable=true)
     */
    private $ipAddress;

    /**
     * @var string
     *
     * @ORM\Column(name="response", type="text", length=65535, nullable=true)
     */
    private $response;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_add", type="datetime", nullable=false)
     */
    private $dateAdd;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_upd", type="datetime", nullable=false)
     */
    private $dateUpd;

    /**
     * @ORM\OneToMany(targetEntity="PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoRatingGrade", cascade={"persist", "remove"}, mappedBy="sellerinfoRating")
     */
    private $sellerinfoRatingGrades;

    public function __construct()
    {
        $this->sellerinfoRatingGrades = new ArrayCollection();
    }

    public function getIdSellerinfoRating(): ?int
    {
        if (!empty($this->idSellerinfoRating)) {
            return (int) $this->idSellerinfoRating;
        } else {
            return $this->idSellerinfoRating;
        }
    }

    public function getSellerinfo(): Sellerinfo
    {
        return $this->sellerInfo;
    }

    public function setSellerinfo(Sellerinfo $sellerInfo): self
    {
        $this->sellerInfo = $sellerInfo;

        return $this;
    }

    public function getIdOrder(): int
    {
        return (int) $this->idOrder;
    }

    public function setIdOrder(int $idOrder): self
    {
        $this->idOrder = $idOrder;

        return $this;
    }

    public function getIdLang(): int
    {
        return (int) $this->idLang;
    }

    public function setIdLang(int $idLang): self
    {
        $this->idLang = $idLang;

        return $this;
    }

    public function getIdCustomer(): int
    {
        return (int) $this->idCustomer;
    }

    public function setIdCustomer(int $idCustomer): self
    {
        $this->idCustomer = $idCustomer;

        return $this;
    }

    public function getCustomer(): string
    {
        return $this->customer;
    }

    public function setCustomer(string $customer): self
    {
        $this->customer = $customer;

        return $this;
    }

    public function getContent(): string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getGrade(): float
    {
        return $this->grade;
    }

    public function setGrade(): self
    {
        $sumGrades = 0;
        $countGrades = $this->getSellerinfoRatingGrades()->count();

        foreach ($this->getSellerinfoRatingGrades() as $sellerinfoRatingGrade) {
            $sumGrades += $sellerinfoRatingGrade->getGrade();
        }

        if ($countGrades > 0) {
            $this->grade = $sumGrades / $countGrades;
        } else {
            $this->grade = 0;
        }

        return $this;
    }

    public function getIpAddress(): ?string
    {
        return $this->ipAddress;
    }

    public function setIpAddress(?string $ipAddress): self
    {
        $this->ipAddress = $ipAddress;

        return $this;
    }

    public function getResponse(): ?string
    {
        return $this->response;
    }

    public function setResponse(?string $response): self
    {
        $this->response = $response;

        return $this;
    }

    public function getDateAdd(): ?\DateTimeInterface
    {
        return $this->dateAdd;
    }

    public function setDateAdd(\DateTimeInterface $dateAdd): self
    {
        $this->dateAdd = $dateAdd;

        return $this;
    }

    public function getDateUpd(): ?\DateTimeInterface
    {
        return $this->dateUpd;
    }

    public function setDateUpd(\DateTimeInterface $dateUpd): self
    {
        $this->dateUpd = $dateUpd;

        return $this;
    }

    /**
     * @return ArrayCollection
     */
    public function getSellerinfoRatingGrades()
    {
        return $this->sellerinfoRatingGrades;
    }

    /**
     * @param SellerinfoRatingCriterion $sellerinfoRatingCriterion
     *
     * @return SellerinfoRatingGrade|null
     */
    public function getRatingGradeByRatingCriterion(SellerinfoRatingCriterion $sellerinfoRatingCriterion): ?SellerinfoRatingGrade
    {
        foreach ($this->sellerinfoRatingGrades as $sellerinfoRatingGrade) {
            if ($sellerinfoRatingCriterion === $sellerinfoRatingGrade->getSellerinfoRatingCriterion()) {
                return $sellerinfoRatingGrade;
            }
        }

        return null;
    }

    /**
     * @param SellerinfoRatingGrade $sellerinfoRatingGrade
     *
     * @return $this
     */
    public function addSellerinfoRatingGrade(SellerinfoRatingGrade $sellerinfoRatingGrade): self
    {
        $sellerinfoRatingGrade->setSellerinfoRating($this);
        $this->sellerinfoRatingGrades->add($sellerinfoRatingGrade);

        return $this;
    }

    /**
     * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
        if ($this->getDateAdd() == null) {
            $this->setDateAdd(new \DateTime());
        }

        $this->setDateUpd(new \DateTime());
        $this->setGrade();
    }
}
