<?php

namespace Tests\Feature\Integration;

use App\Domains\Integration\Models\Integration;
use App\Domains\Integration\Models\TenantIntegration;
use App\Domains\Integration\Services\TenantIntegrationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class TenantIntegrationControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_store_returns_success_message_without_integration_data(): void
    {
        $integration = Integration::create([
            'integration_code' => 'test_integration',
            'name' => 'Test Integration',
            'integration_data_schema' => [
                'api_key' => 'required|string',
            ],
        ]);

        $this->mock(TenantIntegrationService::class, function ($mock) use ($integration) {
            $mock->shouldReceive('updateOrCreateIntegration')
                ->once()
                ->with(
                    'test-tenant',
                    Mockery::on(fn (Integration $argument) => $argument->is($integration)),
                    ['api_key' => 'secret']
                )
                ->andReturn(new TenantIntegration);
        });

        $this->postJson('/api/test-tenant/integrations/test_integration', [
            'integration_data' => [
                'api_key' => 'secret',
            ],
        ])->assertOk()
            ->assertExactJson([
                'code' => 'integration.configured',
                'message' => 'Integración configurada correctamente.',
            ]);
    }
}
