<?php

/**
 * This file is part of the bitrix24-php-sdk package.
 *
 * © Maksim Mesilov <mesilov.maxim@gmail.com>
 *
 * For the full copyright and license information, please view the MIT-LICENSE.txt
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Bitrix24\SDK\Tests\Integration\Services\CRM\Deal\Service;

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Exceptions\TransportException;
use Bitrix24\SDK\Services\CRM\Contact\Service\Contact;
use Bitrix24\SDK\Services\CRM\Deal\Service\Deal;
use Bitrix24\SDK\Services\CRM\Deal\Service\DealContact;
use Bitrix24\SDK\Tests\Integration\Fabric;
use PHPUnit\Framework\TestCase;

/**
 * Class DealsTest
 *
 * @package Bitrix24\SDK\Tests\Integration\Services\CRM\Deals\Service
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\Bitrix24\SDK\Services\CRM\Deal\Service\DealContact::class)]
class DealContactTest extends TestCase
{
    protected Deal $dealService;

    protected Contact $contactService;

    private DealContact $dealContactService;

    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testAddWithPrimary(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add(
                $dealId,
                $this->contactService->add(['NAME' => 'test contact'], [])->getId(),
                true
            )->getId()
        );
    }

    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testAddWithSecondary(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();

        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add($dealId, $this->contactService->add(['NAME' => 'test contact 1'], [])->getId(), true)->getId()
        );
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add($dealId, $this->contactService->add(['NAME' => 'test contact 2'], [])->getId(), false)->getId()
        );
    }

    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testItemsGet(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add(
                $dealId,
                $this->contactService->add(['NAME' => 'test contact 1'], [])->getId(),
                true
            )->getId()
        );
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add(
                $dealId,
                $this->contactService->add(['NAME' => 'test contact 2'], [])->getId(),
                false
            )->getId()
        );

        $this::assertCount(2, $this->dealContactService->itemsGet($dealId)->getDealContacts());
    }


    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testItemsDelete(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add(
                $dealId,
                $this->contactService->add(['NAME' => 'test contact 1'], [])->getId(),
                true
            )->getId()
        );
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add(
                $dealId,
                $this->contactService->add(['NAME' => 'test contact 2'], [])->getId(),
                false
            )->getId()
        );
        $this::assertCount(2, $this->dealContactService->itemsGet($dealId)->getDealContacts());
        $this::assertTrue($this->dealContactService->itemsDelete($dealId)->isSuccess());
        $this::assertCount(0, $this->dealContactService->itemsGet($dealId)->getDealContacts());
    }

    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testItemsSet(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
        $contact1Id = $this->contactService->add(['NAME' => 'test contact 1'], [])->getId();
        $contact2Id = $this->contactService->add(['NAME' => 'test contact 2'], [])->getId();

        $sort = 400;

        $this::assertTrue(
            $this->dealContactService->itemsSet(
                $dealId,
                [
                    [
                        'CONTACT_ID' => $contact2Id,
                        'SORT'       => $sort,
                        'IS_PRIMARY' => 'Y',
                    ],
                    [
                        'CONTACT_ID' => $contact1Id,
                        'SORT'       => '100',
                        'IS_PRIMARY' => 'N',
                    ],
                ]

            )->isSuccess()
        );

        $this::assertCount(2, $this->dealContactService->itemsGet($dealId)->getDealContacts());
    }

    /**
     * @throws BaseException
     * @throws TransportException
     */
    public function testDelete(): void
    {
        $dealId = $this->dealService->add(['TITLE' => 'test deal'])->getId();
        $contact1Id = $this->contactService->add(['NAME' => 'test contact 1'], [])->getId();
        $contact2Id = $this->contactService->add(['NAME' => 'test contact 2'], [])->getId();

        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add($dealId, $contact1Id, true)->getId()
        );
        $this::assertGreaterThanOrEqual(
            1,
            $this->dealContactService->add($dealId, $contact2Id, false)->getId()
        );

        $this::assertCount(2, $this->dealContactService->itemsGet($dealId)->getDealContacts());

        $this::assertTrue($this->dealContactService->delete($dealId, $contact2Id)->isSuccess());

        $this::assertCount(1, $this->dealContactService->itemsGet($dealId)->getDealContacts());
    }

    protected function setUp(): void
    {
        $this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal();
        $this->dealContactService = Fabric::getServiceBuilder()->getCRMScope()->dealContact();
        $this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact();
    }
}