<?php
/**
 * Copyright 2022 Adobe
 * All Rights Reserved.
 */
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Model\Product\Price;

use PHPUnit\Framework\Attributes\DataProvider;
use Magento\Catalog\Api\Data\TierPriceInterfaceFactory;
use Magento\Catalog\Model\Product\Price\TierPrice;
use Magento\Catalog\Model\Product\Price\TierPriceFactory;
use Magento\Catalog\Model\Product\Price\TierPricePersistence;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class TierPriceFactoryTest extends TestCase
{
    /**
     * @var TierPriceInterfaceFactory|MockObject
     */
    private $tierPriceFactory;

    /**
     * @var TierPricePersistence|MockObject
     */
    private $tierPricePersistence;

    /**
     * @var GroupRepositoryInterface|MockObject
     */
    private $customerGroupRepository;

    /**
     * @var SearchCriteriaBuilder|MockObject
     */
    private $searchCriteriaBuilder;

    /**
     * @var FilterBuilder|MockObject
     */
    private $filterBuilder;

    /**
     * @var TierPriceFactory
     */
    private $model;

    /**
     * @inheritdoc
     */
    protected function setUp(): void
    {
        parent::setUp();

        $this->tierPriceFactory = $this->createMock(TierPriceInterfaceFactory::class);
        $this->tierPricePersistence = $this->createMock(TierPricePersistence::class);
        $this->customerGroupRepository = $this->createMock(GroupRepositoryInterface::class);
        $this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
        $this->filterBuilder = $this->createMock(FilterBuilder::class);

        $this->model = new TierPriceFactory(
            $this->tierPriceFactory,
            $this->tierPricePersistence,
            $this->customerGroupRepository,
            $this->searchCriteriaBuilder,
            $this->filterBuilder
        );
    }

    /**
     * @param array $rawData
     * @param array $expected
     * @return void
     */
    #[DataProvider('createDataProvider')]
    public function testCreate(array $rawData, array $expected): void
    {
        $rawData = array_merge(
            [
                'value_id' => 1,
                'entity_id' => 1,
                'all_groups' => 1,
                'customer_group_id' => 0,
                'qty' => 2.0000,
                'value' => 2.0000,
                'percentage_value' => null,
                'website_id' => 0
            ],
            $rawData
        );
        $expected = array_merge(
            [
                'sku' => 'simple',
                'price' => 2.0,
                'price_type' => TierPrice::PRICE_TYPE_FIXED,
                'website_id' => 0,
                'quantity' => 2.000,
                'customer_group' => 'all groups'
            ],
            $expected
        );
        $customerGroupMock = $this->createMock(GroupInterface::class);
        $customerGroupMock->method('getCode')
            ->willReturn('NOT LOGGED IN');

        $isCustomerGroupResolved = isset($rawData['customer_group_code']) || $rawData['all_groups'];
        $this->customerGroupRepository->expects($isCustomerGroupResolved ? $this->never() : $this->once())
            ->method('getById')
            ->willReturn($customerGroupMock);
        $expectedTierPrice = $this->createMock(TierPrice::class);
        $expectedTierPrice->setData($expected);
        $tierPriceMock = $this->createMock(TierPrice::class);
        $this->tierPriceFactory->method('create')
            ->willReturn($tierPriceMock);
        $tierPrice = $this->model->create($rawData, 'simple');
        $this->assertEquals($expectedTierPrice->getSku(), $tierPrice->getSku());
        $this->assertEquals($expectedTierPrice->getPrice(), $tierPrice->getPrice());
        $this->assertEquals($expectedTierPrice->getPriceType(), $tierPrice->getPriceType());
        $this->assertEquals($expectedTierPrice->getWebsiteId(), $tierPrice->getWebsiteId());
        $this->assertEquals($expectedTierPrice->getCustomerGroup(), $tierPrice->getCustomerGroup());
        $this->assertEquals($expectedTierPrice->getQuantity(), $tierPrice->getQuantity());
    }

    /**
     * @return array
     */
    public static function createDataProvider(): array
    {
        return [
            [
                [],
                []
            ],
            [
                [
                    'all_groups' => 0,
                    'customer_group_id' => 1,
                ],
                [
                    'customer_group' => 'NOT LOGGED IN'
                ]
            ],
            [
                [
                    'all_groups' => 0,
                    'customer_group_id' => 2,
                    'customer_group_code' => 'custom',
                ],
                [
                    'customer_group' => 'custom'
                ]
            ]
        ];
    }
}
