/**
 * Descomplicar® Crescimento Digital
 * https://descomplicar.pt
 */

<?php

defined('BASEPATH') or exit('No direct script access allowed');

use PHPUnit\Framework\TestCase;

class CustomerMapperTest extends TestCase
{
    private $mapper;

    protected function setUp(): void
    {
        parent::setUp();
        // Mock CI instance for the mapper
        $CI = new stdClass();
        $CI->custom_fields_model = $this->createMock(stdClass::class);
        $CI->custom_fields_model->method('get')->willReturn([]);

        if (!function_exists('get_instance')) {
            function get_instance() {
                global $CI_INSTANCE_MOCK;
                return $CI_INSTANCE_MOCK;
            }
        }
        global $CI_INSTANCE_MOCK;
        $CI_INSTANCE_MOCK = $CI;

        $this->mapper = new CustomerMapper();
    }

    public function testPerfexToMoloniMapping()
    {
        $perfex_client = [
            'userid' => 999,
            'company' => 'Test Company Ltd',
            'vat' => 'PT123456789',
            'email' => 'test@testcompany.com',
            'phonenumber' => '+351234567890',
            'website' => 'https://testcompany.com',
            'billing_street' => 'Test Street, 123',
            'billing_city' => 'Lisbon',
            'billing_zip' => '1000-001',
            'billing_country' => 'PT',
            'admin_notes' => 'Test client for integration testing'
        ];

        $moloni_data = $this->mapper->toMoloni($perfex_client);

        $this->assertEquals('Test Company Ltd', $moloni_data['name']);
        $this->assertEquals('PT123456789', $moloni_data['vat']);
        $this->assertEquals('test@testcompany.com', $moloni_data['email']);
        $this->assertEquals('+351234567890', $moloni_data['phone']);
        $this->assertEquals('Test Street, 123', $moloni_data['address']);
        $this->assertEquals('Lisbon', $moloni_data['city']);
        $this->assertEquals('1000-001', $moloni_data['zip_code']);
    }

    public function testMoloniToPerfexMapping()
    {
        $moloni_data = [
            'customer_id' => 888,
            'name' => 'Test Company Ltd',
            'vat' => 'PT123456789',
            'email' => 'test@testcompany.com',
            'phone' => '+351234567890',
            'website' => 'https://testcompany.com',
            'address' => 'Test Street, 123',
            'city' => 'Lisbon',
            'state' => 'Lisboa',
            'zip_code' => '1000-001',
            'country_id' => 1,
            'notes' => 'Test client for integration testing'
        ];

        $perfex_data = $this->mapper->toPerfex($moloni_data);

        $this->assertEquals('Test Company Ltd', $perfex_data['company']);
        $this->assertEquals('PT123456789', $perfex_data['vat']);
        $this->assertEquals('test@testcompany.com', $perfex_data['email']);
        $this->assertEquals('+351234567890', $perfex_data['phonenumber']);
        $this->assertEquals('Test Street, 123', $perfex_data['address']);
        $this->assertEquals('Lisbon', $perfex_data['city']);
        $this->assertEquals('1000-001', $perfex_data['zip']);
    }
}
