<?php

namespace Tests\Feature\Integration;

use App\Domains\Client\Models\Client;
use App\Domains\Integration\Models\ClientIntegration;
use App\Domains\Integration\Models\Integration;
use App\Domains\Integration\Services\ClientIntegrationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class ClientIntegrationControllerTest 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',
            ],
        ]);
        $client = Client::query()->create(['code' => 'test-client', 'name' => 'Test Client']);

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

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