<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use Mtc\Crm\Facades\Enquiries;
use Mtc\Crm\Models\Enquiry;
use Mtc\Crm\Models\EnquiryAction;
use Tests\TenantTestCase;

class StormMSDynamicsTest extends TenantTestCase
{
    public function testEnabled()
    {
        Settings::make([
            'tab' => 'CRM',
            'section' => 'Integrations',
            'group' => 'Storm.ie MS Dynamics connector',
            'name' => 'Enabled',
            'config_key' => 'storm-ms-dynamics-crm-enabled',
            'type' => 'boolean',
            'value' => true,
        ]);

        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $this->assertTrue($action->enabled());
    }

    public function testDisabled()
    {
        Settings::make([
            'tab' => 'CRM',
            'section' => 'Integrations',
            'group' => 'Storm.ie MS Dynamics connector',
            'name' => 'Enabled',
            'config_key' => 'storm-ms-dynamics-crm-enabled',
            'type' => 'boolean',
            'value' => false,
        ]);

        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $this->assertFalse($action->enabled());
    }

    public function testFormQuestionDataFields()
    {
        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $fields = $action->formQuestionDataFields();

        $this->assertArrayHasKey('ms-dynamics-field', $fields);
        $this->assertArrayHasKey('choices', $fields['ms-dynamics-field']);
        $this->assertArrayHasKey('first_name', $fields['ms-dynamics-field']['choices']);
        $this->assertArrayHasKey('last_name', $fields['ms-dynamics-field']['choices']);
        $this->assertArrayHasKey('email', $fields['ms-dynamics-field']['choices']);
        $this->assertArrayHasKey('phone', $fields['ms-dynamics-field']['choices']);
        $this->assertArrayHasKey('message', $fields['ms-dynamics-field']['choices']);
    }

    public function testFormAdditionalDataFields()
    {
        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $this->assertIsArray($action->formAdditionalDataFields());
        $this->assertEmpty($action->formAdditionalDataFields());
    }

    public function testDealershipAdditionalDataFields()
    {
        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $fields = $action->dealershipAdditionalDataFields();

        $this->assertIsArray($fields);
        $this->assertArrayHasKey('ms-dynamics-dealership', $fields);
    }

    public function testCustomFields()
    {
        $fields = \App\Crm\StormMSDynamics::customFields();

        $this->assertIsArray($fields);
        $this->assertArrayHasKey('occupation_title', $fields);
        $this->assertArrayHasKey('employer_name', $fields);
        $this->assertArrayHasKey('eircode', $fields);
        $this->assertArrayHasKey('date_of_birth', $fields);
    }

    public function testHandleReturnsTrue()
    {
        $enquiry = Enquiry::factory()->create();
        $action = EnquiryAction::factory()->create([
            'action_name' => 'storm-ie-ms-dynamics',
            'data' => [],
            'processed_at' => null,
            'failed_at' => null,
        ]);

        $enquiryAction = Enquiries::makeAction('storm-ie-ms-dynamics');
        $result = $enquiryAction->handle($action, $enquiry);

        $this->assertTrue($result);
    }

    public function testSuccessDetails()
    {
        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $this->assertEquals('Available on API endpoint', $action->successDetails());
    }

    public function testFailureReason()
    {
        $action = Enquiries::makeAction('storm-ie-ms-dynamics');
        $this->assertEquals('', $action->failureReason());
    }
}
