<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Segments\Services\ConvertsConstantDetailsToResource;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\CurrencyResource;
use App\Models\SegmentConstant;
use App\Models\Currency;
use App\Models\Country;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;

#[Group('unit')]
#[Group('segments')]
#[Group('services')]
#[Group('ok_to_run')]
class ConvertsConstantDetailsToResourceTest extends TestCase
{
    /**
     * Test it returns detail directly if not SUPPLIER_CURRENCIES.
     *
     * @return void
     */
    public function test_it_returns_detail_directly_if_not_supplier_currencies()
    {
        // A R R A N G E
        $mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
        $service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);

        $constant = new SegmentConstant();
        $constant->reference = 'test_it_returns_detail_directly_if_not_supplier_currencies';
        $constant->detail = (object) ['foo' => 'bar'];

        // A C T
        $result = $service->execute($constant);

        // A S S E R T
        $this->assertEquals($constant->detail, $result);
    }

    /**
     * Test it returns CurrencyResource if reference is SUPPLIER_CURRENCIES.
     *
     * @return void
     */
    public function test_it_returns_currency_resource_if_reference_is_supplier_currencies()
    {
        // A R R A N G E
        $currencyId = 123;
        $mockCurrency = new Currency();
        $mockCurrency->id = $currencyId;
        $mockCurrency->name = 'test_it_returns_currency_resource_if_reference_is_supplier_currencies';

        $mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
        $mockFetchesCurrency->shouldReceive('execute')
            ->with(['id' => $currencyId])
            ->once()
            ->andReturn($mockCurrency);

        $service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);

        $constant = new SegmentConstant();
        $constant->reference = SegmentConstants::SUPPLIER_CURRENCIES;
        $constant->detail = (object) ['id' => $currencyId];

        // A C T
        $result = $service->execute($constant);

        // A S S E R T
        $this->assertInstanceOf(CurrencyResource::class, $result);
        $this->assertEquals($currencyId, $result->id);
    }

    /**
     * Test it returns empty string if SUPPLIER_CURRENCIES but no id in detail.
     *
     * @return void
     */
    public function test_it_returns_empty_if_no_id_in_supplier_currencies()
    {
        // A R R A N G E
        $mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
        $service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);

        $constant = new SegmentConstant();
        $constant->reference = SegmentConstants::SUPPLIER_CURRENCIES;
        $constant->detail = (object) ['some' => 'other'];

        // A C T
        $result = $service->execute($constant);

        // A S S E R T
        $this->assertEquals('', $result);
    }

    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }
}
