<?php

namespace common\logic\Distribution\PointSaleDistribution\Service;

use common\logic\AbstractBuilder;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\PointSaleDistribution\Model\PointSaleDistribution;
use common\logic\Distribution\PointSaleDistribution\Repository\PointSaleDistributionRepository;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\PointSale\PointSale\Repository\PointSaleRepository;

class PointSaleDistributionBuilder extends AbstractBuilder
{
    protected PointSaleDistributionRepository $pointSaleDistributionRepository;
    protected PointSaleRepository $pointSaleRepository;
    protected DistributionRepository $distributionRepository;

    public function loadDependencies(): void
    {
        $this->pointSaleDistributionRepository = $this->loadService(PointSaleDistributionRepository::class);
        $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
        $this->distributionRepository = $this->loadService(DistributionRepository::class);
    }

    /**
     * Instancie un PointSaleDistribution.
     */
    public function instanciatePointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
    {
        $pointSaleDistribution = new PointSaleDistribution();
        $pointSaleDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
        $pointSaleDistribution->populateFieldObject('id_point_sale', 'pointSale', $pointSale);

        return $pointSaleDistribution;
    }

    /**
     * Crée un PointSaleDistribution.
     */
    public function createPointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
    {
        $pointSaleDistribution = $this->instanciatePointSaleDistribution($distribution, $pointSale);

        $this->saveCreate($pointSaleDistribution);

        return $pointSaleDistribution;
    }

    public function createPointSaleDistributionIfNotExist(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
    {
        return $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distribution, $pointSale)
            ?? $this->createPointSaleDistribution($distribution, $pointSale);
    }

    /**
     * Crée les PointSaleDistribution pour une distribution donnée.
     */
    // setAll
    public function createAllPointSaleDistributions(Distribution $distribution, bool $delivery): void
    {
        $pointSaleArray = $this->pointSaleRepository->findPointSalesByDistribution($distribution);
        foreach ($pointSaleArray as $pointSale) {
            if (!$pointSale->pointSaleDistribution) {
                $this->createPointSaleDistribution($distribution, $pointSale);
            }
        }

        $pointSaleDistributionArray = $this->pointSaleDistributionRepository->findPointSaleDistributionsByDistribution($distribution);
        foreach ($pointSaleDistributionArray as $pointSaleDistribution) {
            $this->updateDelivery($pointSaleDistribution, $delivery);
        }
    }

    public function updateDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery): void
    {
        $day = date('N', strtotime($pointSaleDistribution->distribution->date));
        $pointSale = $pointSaleDistribution->pointSale;

        if ($delivery &&
            (($day == 1 && $pointSale->delivery_monday) ||
                ($day == 2 && $pointSale->delivery_tuesday) ||
                ($day == 3 && $pointSale->delivery_wednesday) ||
                ($day == 4 && $pointSale->delivery_thursday) ||
                ($day == 5 && $pointSale->delivery_friday) ||
                ($day == 6 && $pointSale->delivery_saturday) ||
                ($day == 7 && $pointSale->delivery_sunday)
            )) {
            $pointSaleDistribution->delivery = 1;
        } else {
            $pointSaleDistribution->delivery = 0;
        }

        $this->saveUpdate($pointSaleDistribution);
    }
}