<?php

namespace domain\PointSale\SharedPointSale;

use common\components\ActiveRecordCommon;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;
use domain\User\User\User;

class SharedPointSale extends ActiveRecordCommon
{
    public ?Producer $producerCurrent;

    public const SCENARIO_CREATE = 'create';
    public const SCENARIO_CONFIRM = 'confirm';

    public static function tableName()
    {
        return 'shared_point_sale';
    }

    public function rules()
    {
        return [
            [['id_point_sale', 'id_producer_with_sharing', 'created_at', 'created_by'], 'required'],
            ['id_point_sale_with_sharing', 'required', 'on' => self::SCENARIO_CONFIRM],
            [['id_point_sale', 'id_producer_with_sharing', 'id_point_sale_with_sharing', 'status',
                'created_by', 'confirmed_by', 'declined_by'], 'integer'],
            [['created_at', 'confirmed_at', 'declined_at'], 'safe'],
            ['id_point_sale', 'verifySharedPointSale', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_CONFIRM]],
            ['id_point_sale', 'exist', 'targetClass' => PointSale::class, 'targetAttribute' => 'id'],
            ['id_point_sale_with_sharing', 'exist', 'targetClass' => PointSale::class, 'targetAttribute' => 'id'],
            ['id_point_sale_with_sharing', 'verifyConfirm'],
            ['id_producer_with_sharing', 'exist', 'targetClass' => Producer::class, 'targetAttribute' => 'id'],
            [['created_by', 'confirmed_by', 'declined_by'], 'exist', 'targetClass' => User::class, 'targetAttribute' => 'id'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id_point_sale' => 'Point de vente',
            'id_producer_with_sharing' => 'Producteur',
            'id_point_sale_with_sharing' => 'Point de vente partagé',
        ];
    }

    public function verifySharedPointSale($attribute, $params)
    {
        $sharedPointSaleRepository = SharedPointSaleRepository::getInstance();

        if($this->scenario == self::SCENARIO_CREATE || $this->scenario == self::SCENARIO_CONFIRM) {
            $sharedPointSaleConfirmed = $sharedPointSaleRepository->findSharedPointsSaleConfirmedByPointSale(
                $this->getPointSale(),
                $this->getProducerWithSharing()
            );
            if($sharedPointSaleConfirmed && count($sharedPointSaleConfirmed)) {
                $this->addError($attribute, "Ce point de vente est déjà partagé avec ce producteur.");
            }
        }

        if($this->scenario == self::SCENARIO_CREATE) {
            $sharedPointSaleRequest = $sharedPointSaleRepository->findSharedPointSaleRequestByPointSale(
                $this->getPointSale(),
                $this->getProducerWithSharing()
            );
            if($sharedPointSaleRequest && count($sharedPointSaleRequest)) {
                $this->addError($attribute, "Ce point de vente a déjà une demande de partage en attente avec ce producteur.");
            }
        }
    }

    public function verifyConfirm($attribute, $params)
    {
        if($this->scenario == self::SCENARIO_CONFIRM) {
            if($this->producerCurrent->id != $this->getProducerWithSharing()->id) {
                $this->addError($attribute, "Vous ne pouvez pas confirmer cette demande de partage.");
            }
        }
    }

    /* Getters / Setters */

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getPointSale(): PointSale
    {
        return $this->pointSaleRelation;
    }

    public function setPointSale(PointSale $pointSale): self
    {
        $this->populateFieldObject('id_point_sale', 'pointSaleRelation', $pointSale);
        return $this;
    }

    public function getProducerWithSharing(): Producer
    {
        return $this->producerWithSharingRelation;
    }

    public function setProducerWithSharing(Producer $producerWithSharing): self
    {
        $this->populateFieldObject('id_producer_with_sharing', 'producerWithSharingRelation', $producerWithSharing);

        return $this;
    }

    public function getPointSaleWithSharing(): ?PointSale
    {
        return $this->pointSaleWithSharingRelation;
    }

    public function setPointSaleWithSharing(PointSale $pointSaleWithSharing = null): self
    {
        if($pointSaleWithSharing) {
            $this->populateFieldObject('id_point_sale_with_sharing', 'pointSaleWithSharingRelation', $pointSaleWithSharing);
        }

        return $this;
    }

    public function getStatus(): int
    {
        return $this->status;
    }

    public function setStatus(int $status): self
    {
        $this->status = $status;
        return $this;
    }

    public function getCreatedAt(): \DateTime
    {
        return new \DateTime($this->created_at);
    }

    public function setCreatedAt(\DateTime $createdAt): self
    {
        $this->created_at = $createdAt->format('Y-m-d H:i:s');
        return $this;
    }

    public function getConfirmedAt(): \DateTime
    {
        return new \DateTime($this->confirmed_at);
    }

    public function setConfirmedAt(\DateTime $confirmedAt): self
    {
        $this->confirmed_at = $confirmedAt->format('Y-m-d H:i:s');
        return $this;
    }

    public function getDeclinedAt(): \DateTime
    {
        return new \DateTime($this->declined_at);
    }

    public function setDeclinedAt(\DateTime $declinedAt): self
    {
        $this->declined_at = $declinedAt->format('Y-m-d H:i:s');
        return $this;
    }

    public function getCreatedBy(): User
    {
        return $this->createdByRelation;
    }

    public function setCreatedBy(User $createdBy): SharedPointSale
    {
        $this->populateFieldObject('created_by', 'createdByRelation', $createdBy);
        return $this;
    }

    public function getConfirmedBy(): User
    {
        return $this->confirmedByRelation;
    }

    public function setConfirmedBy(User $confirmedBy): SharedPointSale
    {
        $this->populateFieldObject('confirmed_by', 'confirmedByRelation', $confirmedBy);
        return $this;
    }

    public function getDeclinedBy(): User
    {
        return $this->declinedByRelation;
    }

    public function setDeclinedBy(User $declinedBy): SharedPointSale
    {
        $this->populateFieldObject('declined_by', 'declinedByRelation', $declinedBy);
        return $this;
    }

    /* Relations */

    public function getPointSaleRelation()
    {
        return $this->hasOne(PointSale::class, ['id' => 'id_point_sale']);
    }

    public function getProducerWithSharingRelation()
    {
        return $this->hasOne(Producer::class, ['id' => 'id_producer_with_sharing']);
    }

    public function getPointSaleWithSharingRelation()
    {
        return $this->hasOne(PointSale::class, ['id' => 'id_point_sale_with_sharing']);
    }

    public function getCreatedByRelation()
    {
        return $this->hasOne(User::class, ['id' => 'created_by']);
    }

    public function getConfirmedByRelation()
    {
        return $this->hasOne(User::class, ['id' => 'confirmed_by']);
    }

    public function getDeclinedByRelation()
    {
        return $this->hasOne(User::class, ['id' => 'declined_by']);
    }
}