<?php

namespace Tests\unit\workflow\engine\src\ProcessMaker\Model;

use EmailServer;
use ProcessMaker\Model\Application;
use ProcessMaker\Model\AbeConfiguration;
use ProcessMaker\Model\AbeRequest;
use ProcessMaker\Model\Delegation;
use ProcessMaker\Model\Dynaform;
use ProcessMaker\Model\EmailServerModel;
use ProcessMaker\Model\Process;
use ProcessMaker\Model\Task;
use Tests\TestCase;

/**
 * Class AbeConfigurationTest
 *
 * @coversDefaultClass \ProcessMaker\Model\AbeConfiguration
 */
class AbeConfigurationTest extends TestCase
{
    /**
     * Call the setUp parent method
     */
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
    }

    /**
     * Call the tearDown parent method
     */
    public function tearDown(): void
    {
        parent::tearDown(); // TODO: Change the autogenerated stub
    }

    /**
     * Test the getAbeRequest method
     *
     * @covers \ProcessMaker\Model\AbeConfiguration::getAbeRequest()
     * @test
     */
    public function it_should_test_the_get_abe_request_method()
    {
        //Create the Task factory
        Task::factory()->create();
        //Create the Process factory
        Process::factory()->create();
        //Create the Dynaform factory
        Dynaform::factory()->create();
        //Create the EmailServerModel factory
        EmailServerModel::factory()->create();
        //Create the Application factory
        Application::factory()->create();
        //Create the Delegation factory
        $delegation = Delegation::factory()->create();
        //Create the AbeConfiguration factory
        $abeConfiguration = AbeConfiguration::factory()->create();
        //Create the AbeConfiguration factory
        AbeRequest::factory()->create([
            'ABE_UID' => $abeConfiguration->ABE_UID,
            'APP_UID' => $delegation->APP_UID,
            'DEL_INDEX' => $delegation->DEL_INDEX,
            'ABE_REQ_UID' => $abeConfiguration->ABE_UID
        ]);

        //Call the getAbeRequest method
        $res = AbeConfiguration::getAbeRequest($abeConfiguration->ABE_UID);

        //Assert the result is not empty
        $this->assertNotEmpty($res);
        //Assert the result is the one looked for
        $this->assertEquals($res['ABE_UID'], $abeConfiguration->ABE_UID);
    }

    /**
     * Tests the getAbeRequest method  when the result is empty
     *
     * @covers \ProcessMaker\Model\AbeConfiguration::getAbeRequest()
     * @test
     */
    public function it_should_test_the_get_abe_request_method_when_the_result_is_empty()
    {
        //Creates the Task factory
        Task::factory()->create();
        //Creates the Process factory
        Process::factory()->create();
        //Creates the Dynaform factory
        Dynaform::factory()->create();
        //Creates the EmailServer factory
        EmailServerModel::factory()->create();
        //Creates the Application factory
        Application::factory()->create();
        //Creates the Delegation factory
        $delegation = Delegation::factory()->create();
        //Creates the AbeConfiguration factory
        $abeConfiguration = AbeConfiguration::factory()->create();
        //Creates the AbeConfiguration factory
        AbeRequest::factory()->create([
            'ABE_UID' => $abeConfiguration->ABE_UID,
            'APP_UID' => $delegation->APP_UID,
            'DEL_INDEX' => $delegation->DEL_INDEX,
            'ABE_REQ_UID' => $abeConfiguration->ABE_UID
        ]);

        //Call the getAbeRequest method
        $res = AbeConfiguration::getAbeRequest('');

        //Asserts the result has one record
        $this->assertEmpty($res);
    }

    /**
     * It should test the updateReceiverUidToEmpty method
     * 
     * @covers \ProcessMaker\Model\AbeConfiguration::updateReceiverUidToEmpty()
     * @test
     */
    public function it_should_test_the_update_abe_configuration_receiver_uid_method()
    {
        $emailServer = EmailServerModel::factory()->create();

        $abeConfigurationFactory = AbeConfiguration::factory()->create([
            'ABE_EMAIL_SERVER_UID' => $emailServer['MESS_UID']
        ]);

        $abeConfiguration = new AbeConfiguration();
        $abeConfiguration->updateReceiverUidToEmpty($emailServer['MESS_UID']);

        $query = AbeConfiguration::query()->select();
        $query->where('ABE_UID', $abeConfigurationFactory['ABE_UID']);
        $updatedAbe = $query->get()->values()->toArray();

        $this->assertEquals($updatedAbe[0]['ABE_EMAIL_SERVER_RECEIVER_UID'], '');
    }

    /**
     * It should test the updateEmailServerUidToDefaultOrEmpty method when there is not a default server
     * 
     * @covers \ProcessMaker\Model\AbeConfiguration::updateEmailServerUidToDefaultOrEmpty()
     * @test
     */
    public function it_should_test_the_update_abe_configuration_email_server_uid_method_when_there_is_not_a_default_server()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create();

        $abeConfigurationFactory = AbeConfiguration::factory()->create([
            'ABE_EMAIL_SERVER_UID' => $emailServer['MESS_UID']
        ]);

        $abeConfiguration = new AbeConfiguration();
        $abeConfiguration->updateEmailServerUidToDefaultOrEmpty($emailServer['MESS_UID']);

        $query = AbeConfiguration::query()->select();
        $query->where('ABE_UID', $abeConfigurationFactory['ABE_UID']);
        $updatedAbe = $query->get()->values()->toArray();

        $this->assertEquals($updatedAbe[0]['ABE_EMAIL_SERVER_UID'], '');
    }

    /**
     * It should test the updateEmailServerUidToDefaultOrEmpty method when there is a default server
     * 
     * @covers \ProcessMaker\Model\AbeConfiguration::updateEmailServerUidToDefaultOrEmpty()
     * @test
     */
    public function it_should_test_the_update_abe_configuration_email_server_uid_method_when_there_is_a_default_server()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create();
        
        $defaultServer = EmailServerModel::factory()->create([
            'MESS_DEFAULT' => 1
        ]);

        $abeConfigurationFactory = AbeConfiguration::factory()->create([
            'ABE_EMAIL_SERVER_UID' => $emailServer['MESS_UID']
        ]);

        $abeConfiguration = new AbeConfiguration();
        $abeConfiguration->updateEmailServerUidToDefaultOrEmpty($emailServer['MESS_UID']);

        $query = AbeConfiguration::query()->select();
        $query->where('ABE_UID', $abeConfigurationFactory['ABE_UID']);
        $updatedAbe = $query->get()->values()->toArray();
        
        $this->assertEquals($updatedAbe[0]['ABE_EMAIL_SERVER_UID'], $defaultServer['MESS_UID']);
    }
}