<?php

namespace Tests\Feature;

use App\Campaigns\Platforms\LinkedInRules;
use App\Campaigns\Platforms\MetaRules;
use App\Console\Commands\RefreshPlatformTokensCommand;
use App\Models\GoogleAdsConnection;
use App\Models\PlatformConnection;
use App\Models\User;
use App\Services\Keywords\GoogleAdsClientFactory;
use App\Services\LinkedIn\LinkedInRestApi;
use App\Services\Meta\CampaignSpec;
use App\Services\Meta\GraphApi;
use App\Services\Meta\Meta;
use App\Services\Platforms\Auth\GoogleTokenRefresher;
use App\Services\Platforms\Auth\LinkedInTokenRefresher;
use App\Services\Platforms\Auth\MetaTokenRefresher;
use App\Services\Platforms\Auth\TokenRefresherFactory;
use Database\Seeders\PlatformConnectionSeeder;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;
use Tests\TestCase;

class PlatformConnectionTest extends TestCase
{
    use RefreshDatabase;

    /** Tokens are stored encrypted in the database and hidden from serialization. */
    public function test_tokens_are_encrypted_and_hidden_from_serialization(): void
    {
        $conn = PlatformConnection::create([
            'credential_storage' => 'encrypted',
            'platform' => 'meta',
            'name' => 'Meta Production Ads',
            'access_token' => 'super-secret-meta-access-token',
            'refresh_token' => 'super-secret-refresh-token',
            'platform_data' => ['app_id' => '12345'],
            'platform_secrets' => ['app_secret' => 'private-test-secret'],
            'is_active' => true,
        ]);

        // Decrypted via accessor
        $this->assertSame('super-secret-meta-access-token', $conn->access_token);
        $this->assertSame('super-secret-refresh-token', $conn->refresh_token);

        // Raw database attribute is encrypted
        $rawAccessToken = $conn->getRawOriginal('access_token');
        $this->assertNotSame('super-secret-meta-access-token', $rawAccessToken);
        $this->assertStringStartsWith('eyJ', (string) $rawAccessToken); // Laravel default encrypted payload
        $this->assertSame('{"app_id":"12345"}', $conn->getRawOriginal('platform_data'));
        $this->assertNotSame('{"app_secret":"private-test-secret"}', $conn->getRawOriginal('platform_secrets'));
        $this->assertSame('private-test-secret', $conn->platform_secrets['app_secret']);

        // Hidden from array and JSON serialization
        $serialized = $conn->toArray();
        $this->assertArrayNotHasKey('access_token', $serialized);
        $this->assertArrayNotHasKey('refresh_token', $serialized);
        $this->assertArrayNotHasKey('platform_secrets', $serialized);
    }

    public function test_plain_credential_storage_reads_and_writes_raw_values(): void
    {
        $connection = PlatformConnection::factory()->meta('factory-token')->create(['user_id' => null]);
        DB::table('platform_connections')->where('id', $connection->id)->update([
            'credential_storage' => 'plain',
            'access_token' => 'plain-access-token',
            'refresh_token' => 'plain-refresh-token',
            'platform_secrets' => '{"mcp_token":"plain-mcp-token"}',
        ]);

        $connection = PlatformConnection::findOrFail($connection->id);

        $this->assertSame('plain-access-token', $connection->access_token);
        $this->assertSame('plain-refresh-token', $connection->refresh_token);
        $this->assertSame(['mcp_token' => 'plain-mcp-token'], $connection->platform_secrets);
        $this->assertSame('plain-access-token', PlatformConnection::sharedAccessToken('meta'));
        $this->assertSame('plain-mcp-token', PlatformConnection::sharedPlatformSecret('meta', 'mcp_token'));
        $this->assertArrayNotHasKey('access_token', $connection->toArray());
        $this->assertArrayNotHasKey('refresh_token', $connection->toArray());
        $this->assertArrayNotHasKey('platform_secrets', $connection->toArray());

        $connection->access_token = 'rotated-access-token';
        $connection->refresh_token = 'rotated-refresh-token';
        $connection->platform_secrets = ['mcp_token' => 'rotated-mcp-token'];
        $connection->save();

        $this->assertSame(
            'rotated-access-token',
            DB::table('platform_connections')->where('id', $connection->id)->value('access_token'),
        );
        $this->assertSame(
            'rotated-refresh-token',
            DB::table('platform_connections')->where('id', $connection->id)->value('refresh_token'),
        );
        $this->assertSame(
            '{"mcp_token":"rotated-mcp-token"}',
            DB::table('platform_connections')->where('id', $connection->id)->value('platform_secrets'),
        );
    }

    public function test_plaintext_conversion_covers_all_platform_connection_rows(): void
    {
        $connections = [];

        foreach (['meta', 'linkedin', 'google'] as $platform) {
            $connection = PlatformConnection::create([
                'credential_storage' => 'encrypted',
                'platform' => $platform,
                'account_id' => $platform.'-account',
                'access_token' => $platform.'-access',
                'refresh_token' => $platform.'-refresh',
                'platform_secrets' => ['client_secret' => $platform.'-secret'],
            ]);
            $connections[] = $connection;
        }

        $migration = require database_path('migrations/2026_10_16_083938_add_credential_storage_mode_to_platform_connections_table.php');
        $migration->up();

        foreach ($connections as $connection) {
            $row = DB::table('platform_connections')->find($connection->id);
            $this->assertSame('plain', $row->credential_storage);
            $this->assertSame($connection->platform.'-account', $row->account_id);
            $this->assertSame($connection->platform.'-access', $row->access_token);
            $this->assertSame($connection->platform.'-refresh', $row->refresh_token);
            $this->assertSame(['client_secret' => $connection->platform.'-secret'], json_decode($row->platform_secrets, true));
            $this->assertSame($row->access_token, $connection->fresh()->access_token);
        }

        $migration->down();
        $this->assertSame('meta-access', Crypt::decryptString(DB::table('platform_connections')->where('id', $connections[0]->id)->value('access_token')));
        $migration->up();
    }

    public function test_shared_unreadable_credentials_are_replaced_for_all_platforms_from_json(): void
    {
        $this->useCredentialFile([
            'meta' => [
                'account_id' => 'act-configured-meta',
                'access_token' => 'configured-meta-access',
                'refresh_token' => '',
                'platform_data' => ['app_id' => 'configured-meta-app'],
                'platform_secrets' => [
                    'app_secret' => 'configured-meta-secret',
                    'mcp_token' => 'configured-meta-mcp',
                ],
            ],
            'linkedin' => [
                'account_id' => '987654321',
                'access_token' => 'configured-linkedin-access',
                'refresh_token' => 'configured-linkedin-refresh',
                'platform_data' => [
                    'client_id' => 'configured-linkedin-client',
                    'organization_id' => '123456789',
                ],
                'platform_secrets' => ['client_secret' => 'configured-linkedin-secret'],
                'scopes' => ['r_ads', 'rw_ads', 'r_organization_admin'],
                'expires_at' => '2026-12-01 00:00:00',
                'refresh_expires_at' => '2027-10-16 12:30:00',
            ],
            'google' => [
                'account_id' => '1231231234',
                'refresh_token' => 'configured-google-refresh',
                'platform_data' => [
                    'client_id' => 'configured-google-client',
                    'manager_customer_id' => '9879879876',
                ],
                'platform_secrets' => [
                    'developer_token' => 'configured-google-dev-token',
                    'client_secret' => 'configured-google-secret',
                ],
            ],
        ]);

        $rows = [];
        foreach (['meta', 'linkedin', 'google'] as $platform) {
            $rows[$platform] = PlatformConnection::create([
                'credential_storage' => 'encrypted',
                'platform' => $platform,
                'user_id' => null,
                'access_token' => $platform.'-old-access',
                'refresh_token' => $platform.'-old-refresh',
                'platform_data' => ['existing' => 'kept'],
                'platform_secrets' => ['existing_secret' => $platform.'-kept'],
                'expires_at' => '2026-01-01 00:00:00',
                'refresh_expires_at' => '2026-06-01 00:00:00',
            ]);
        }
        DB::table('platform_connections')->where('id', $rows['meta']->id)->update([
            'access_token' => 'unreadable-meta-access',
        ]);
        DB::table('platform_connections')->where('id', $rows['linkedin']->id)->update([
            'access_token' => 'unreadable-linkedin-access',
            'refresh_token' => 'unreadable-linkedin-refresh',
        ]);
        DB::table('platform_connections')->where('id', $rows['google']->id)->update([
            'refresh_token' => 'unreadable-google-refresh',
        ]);

        $migration = require database_path('migrations/2026_10_16_083938_add_credential_storage_mode_to_platform_connections_table.php');
        $migration->up();

        $expected = [
            'meta' => [
                'account_id' => 'act-configured-meta',
                'access_token' => 'configured-meta-access',
                'refresh_token' => 'meta-old-refresh',
                'secret' => 'configured-meta-secret',
            ],
            'linkedin' => [
                'account_id' => '987654321',
                'access_token' => 'configured-linkedin-access',
                'refresh_token' => 'configured-linkedin-refresh',
                'secret' => 'configured-linkedin-secret',
            ],
            'google' => [
                'account_id' => '1231231234',
                'access_token' => 'google-old-access',
                'refresh_token' => 'configured-google-refresh',
                'secret' => 'configured-google-secret',
            ],
        ];

        foreach ($expected as $platform => $credentials) {
            $row = DB::table('platform_connections')->find($rows[$platform]->id);
            $this->assertSame('plain', $row->credential_storage);
            $this->assertSame($credentials['account_id'], $row->account_id);
            $this->assertSame($credentials['access_token'], $row->access_token);
            $this->assertSame($credentials['refresh_token'], $row->refresh_token);
            $this->assertSame('kept', json_decode($row->platform_data, true)['existing']);
            $this->assertSame($credentials['secret'], json_decode($row->platform_secrets, true)['client_secret']
                ?? json_decode($row->platform_secrets, true)['app_secret']);
        }

        $linkedinRow = DB::table('platform_connections')->find($rows['linkedin']->id);
        $this->assertSame(['r_ads', 'rw_ads', 'r_organization_admin'], json_decode($linkedinRow->scopes, true));
        $this->assertSame('2026-12-01 00:00:00', $linkedinRow->expires_at);
        $this->assertSame('2027-10-16 12:30:00', $linkedinRow->refresh_expires_at);
        $metaRow = DB::table('platform_connections')->find($rows['meta']->id);
        $this->assertSame('2026-01-01 00:00:00', $metaRow->expires_at);
        $this->assertSame('2026-06-01 00:00:00', $metaRow->refresh_expires_at);
        $this->assertSame('configured-meta-mcp', json_decode(DB::table('platform_connections')->find($rows['meta']->id)->platform_secrets, true)['mcp_token']);
        $this->assertSame('configured-google-dev-token', json_decode(DB::table('platform_connections')->find($rows['google']->id)->platform_secrets, true)['developer_token']);
    }

    public function test_plaintext_conversion_leaves_platform_connection_rows_unchanged_on_unreadable_credential(): void
    {
        $connection = PlatformConnection::create([
            'credential_storage' => 'encrypted',
            'platform' => 'meta',
            'access_token' => 'valid-token',
        ]);
        $unreadable = PlatformConnection::create([
            'credential_storage' => 'encrypted',
            'platform' => 'linkedin',
            'refresh_token' => 'valid-refresh-token',
        ]);
        DB::table('platform_connections')->where('id', $unreadable->id)->update([
            'refresh_token' => 'unreadable-credential',
        ]);
        $beforePlatform = DB::table('platform_connections')->get()->toJson();
        $migration = require database_path('migrations/2026_10_16_083938_add_credential_storage_mode_to_platform_connections_table.php');

        try {
            $migration->up();
            $this->fail('Expected conversion to stop for unreadable credentials.');
        } catch (\RuntimeException $exception) {
            $this->assertStringContainsString('platform_connections.'.$unreadable->id.'.refresh_token', $exception->getMessage());
            $this->assertStringNotContainsString('unreadable-credential', $exception->getMessage());
            $this->assertSame($beforePlatform, DB::table('platform_connections')->get()->toJson());
            $this->assertSame('valid-token', $connection->fresh()->access_token);
        }
    }

    public function test_invalid_credential_file_stops_conversion_before_any_write(): void
    {
        $connection = PlatformConnection::create([
            'credential_storage' => 'encrypted',
            'platform' => 'meta',
            'access_token' => 'valid-token',
        ]);
        $path = storage_path('framework/testing/invalid-platform-connection-credentials.json');
        File::ensureDirectoryExists(dirname($path));
        File::put($path, '{"linkedin":');
        config(['platforms.connection_credentials_file' => $path]);
        $before = DB::table('platform_connections')->get()->toJson();
        $migration = require database_path('migrations/2026_10_16_083938_add_credential_storage_mode_to_platform_connections_table.php');

        try {
            $migration->up();
            $this->fail('Expected an invalid credential file to stop conversion.');
        } catch (\RuntimeException $exception) {
            $this->assertStringContainsString($path, $exception->getMessage());
            $this->assertStringNotContainsString('linkedin', $exception->getMessage());
            $this->assertSame($before, DB::table('platform_connections')->get()->toJson());
            $this->assertSame('valid-token', $connection->fresh()->access_token);
        }
    }

    public function test_unreadable_expiry_stops_conversion_before_any_write(): void
    {
        $connection = PlatformConnection::create([
            'credential_storage' => 'encrypted',
            'platform' => 'linkedin',
            'access_token' => 'valid-token',
            'expires_at' => '2026-01-01 00:00:00',
        ]);
        $this->useCredentialFile([
            'linkedin' => [
                'expires_at' => 'not-a-date',
            ],
        ]);
        $before = DB::table('platform_connections')->get()->toJson();
        $migration = require database_path('migrations/2026_10_16_083938_add_credential_storage_mode_to_platform_connections_table.php');

        try {
            $migration->up();
            $this->fail('Expected an unreadable expiry to stop conversion.');
        } catch (\RuntimeException $exception) {
            $this->assertStringNotContainsString('not-a-date', $exception->getMessage());
            $this->assertSame($before, DB::table('platform_connections')->get()->toJson());
            $this->assertSame('valid-token', $connection->fresh()->access_token);
        }
    }

    /** PlatformConnection::shared() fetches the shared active connection. */
    public function test_shared_connection_resolution(): void
    {
        $shared = PlatformConnection::factory()->meta('shared-token')->create(['user_id' => null]);
        $user = User::factory()->create();
        $userConnection = PlatformConnection::factory()->meta('user-token')->create(['user_id' => $user->id]);

        $resolved = PlatformConnection::shared('meta');
        $this->assertNotNull($resolved);
        $this->assertSame($shared->id, $resolved->id);
        $this->assertSame('shared-token', $resolved->access_token);
    }

    public function test_unreadable_shared_token_uses_configured_fallback(): void
    {
        $connection = PlatformConnection::factory()->meta('encrypted-under-old-key')->create(['user_id' => null]);
        DB::table('platform_connections')->where('id', $connection->id)->update([
            'credential_storage' => 'encrypted',
            'access_token' => 'invalid-ciphertext',
            'platform_secrets' => 'invalid-ciphertext',
        ]);
        config([
            'platforms.meta.token' => 'configured-meta-token',
            'platforms.meta.mcp.enabled' => true,
            'platforms.meta.mcp.token' => 'configured-mcp-token',
        ]);

        $this->assertSame(
            'configured-meta-token',
            PlatformConnection::sharedAccessToken('meta', 'configured-meta-token'),
        );
        $this->assertSame(
            'configured-mcp-token',
            PlatformConnection::sharedPlatformSecret('meta', 'mcp_token', 'configured-mcp-token'),
        );
        $this->assertInstanceOf(Meta::class, app(Meta::class));
    }

    /** Expiry scopes and needsRefresh correctly compute threshold. */
    public function test_expiry_scopes_and_refresh_calculation(): void
    {
        Carbon::setTestNow('2026-09-25 12:00:00');

        $expiringIn5Days = PlatformConnection::factory()->meta()->create([
            'expires_at' => Carbon::now()->addDays(5),
            'is_active' => true,
        ]);

        $expiringIn30Days = PlatformConnection::factory()->meta()->create([
            'expires_at' => Carbon::now()->addDays(30),
            'is_active' => true,
        ]);

        $inactiveExpiringIn3Days = PlatformConnection::factory()->meta()->create([
            'expires_at' => Carbon::now()->addDays(3),
            'is_active' => false,
        ]);

        $this->assertTrue($expiringIn5Days->needsRefresh(10));
        $this->assertFalse($expiringIn30Days->needsRefresh(10));

        $expiringSoon = PlatformConnection::expiringSoon(10)->pluck('id')->all();
        $this->assertContains($expiringIn5Days->id, $expiringSoon);
        $this->assertNotContains($expiringIn30Days->id, $expiringSoon);
        $this->assertNotContains($inactiveExpiringIn3Days->id, $expiringSoon);

        Carbon::setTestNow();
    }

    /** TokenRefresherFactory resolves matching refresher instances. */
    public function test_refresher_factory_resolves_adapters(): void
    {
        $factory = app(TokenRefresherFactory::class);

        $this->assertInstanceOf(MetaTokenRefresher::class, $factory->forPlatform('meta'));
        $this->assertInstanceOf(GoogleTokenRefresher::class, $factory->forPlatform('google'));
        $this->assertInstanceOf(LinkedInTokenRefresher::class, $factory->forPlatform('linkedin'));

        $this->expectException(InvalidArgumentException::class);
        $factory->forPlatform('unsupported_platform');
    }

    /** MetaTokenRefresher exchanges long-lived token via Graph API. */
    public function test_meta_token_refresher_updates_connection(): void
    {
        Carbon::setTestNow('2026-09-25 12:00:00');

        config([
            'services.meta.client_id' => 'fb_app_123',
            'services.meta.client_secret' => 'fb_secret_abc',
        ]);

        Http::fake([
            'https://graph.facebook.com/*' => Http::response([
                'access_token' => 'new-refreshed-meta-token',
                'token_type' => 'bearer',
                'expires_in' => 5184000, // 60 days
            ], 200),
        ]);

        $conn = PlatformConnection::factory()->meta('old-meta-token')->create([
            'expires_at' => Carbon::now()->addDays(2),
            'platform_secrets' => ['app_secret' => 'db-meta-secret'],
        ]);

        $refresher = app(MetaTokenRefresher::class);
        $refreshed = $refresher->refresh($conn);

        $this->assertSame('new-refreshed-meta-token', $refreshed->access_token);
        $this->assertEquals(Carbon::now()->addSeconds(5184000), $refreshed->expires_at);
        Http::assertSent(fn ($request): bool => $request['client_secret'] === 'db-meta-secret');

        Carbon::setTestNow();
    }

    /** GoogleTokenRefresher exchanges refresh_token via oauth2.googleapis.com. */
    public function test_google_token_refresher_updates_connection(): void
    {
        Carbon::setTestNow('2026-09-25 12:00:00');

        config([
            'services.google_ads.client_id' => 'google_client_id',
            'services.google_ads.client_secret' => 'google_secret',
        ]);

        Http::fake([
            'https://oauth2.googleapis.com/token' => Http::response([
                'access_token' => 'new-google-access-token',
                'expires_in' => 3600,
            ], 200),
        ]);

        $conn = PlatformConnection::factory()->google('current-access-token', 'my-refresh-token')->create([
            'expires_at' => Carbon::now()->addDays(1),
            'platform_secrets' => ['client_secret' => 'db-google-secret'],
        ]);

        $refresher = app(GoogleTokenRefresher::class);
        $refreshed = $refresher->refresh($conn);

        $this->assertSame('new-google-access-token', $refreshed->access_token);
        $this->assertSame('my-refresh-token', $refreshed->refresh_token);
        $this->assertEquals(Carbon::now()->addSeconds(3600), $refreshed->expires_at);
        Http::assertSent(fn ($request): bool => $request['client_secret'] === 'db-google-secret');

        Carbon::setTestNow();
    }

    /** LinkedInTokenRefresher exchanges refresh token via LinkedIn OAuth. */
    public function test_linkedin_token_refresher_updates_connection(): void
    {
        Carbon::setTestNow('2026-09-25 12:00:00');

        config([
            'services.linkedin.client_id' => 'li_client',
            'services.linkedin.client_secret' => 'li_secret',
        ]);

        Http::fake([
            'https://www.linkedin.com/oauth/v2/accessToken' => Http::response([
                'access_token' => 'new-li-access-token',
                'expires_in' => 5184000,
                'refresh_token' => 'new-li-refresh-token',
                'refresh_token_expires_in' => 31536000,
            ], 200),
        ]);

        $conn = PlatformConnection::factory()->linkedin('old-li-token', 'old-li-refresh')->create([
            'expires_at' => Carbon::now()->addDays(3),
            'platform_secrets' => ['client_secret' => 'db-linkedin-secret'],
        ]);

        $refresher = app(LinkedInTokenRefresher::class);
        $refreshed = $refresher->refresh($conn);

        $this->assertSame('new-li-access-token', $refreshed->access_token);
        $this->assertSame('new-li-refresh-token', $refreshed->refresh_token);
        $this->assertEquals(Carbon::now()->addSeconds(5184000), $refreshed->expires_at);
        Http::assertSent(fn ($request): bool => $request['client_secret'] === 'db-linkedin-secret');

        Carbon::setTestNow();
    }

    /** RefreshPlatformTokensCommand scans and triggers refresh on expiring tokens. */
    public function test_refresh_tokens_command(): void
    {
        Carbon::setTestNow('2026-09-25 12:00:00');

        config([
            'services.meta.client_id' => 'fb_app_123',
            'services.meta.client_secret' => 'fb_secret_abc',
        ]);

        Http::fake([
            'https://graph.facebook.com/*' => Http::response([
                'access_token' => 'command-refreshed-token',
                'expires_in' => 5184000,
            ], 200),
        ]);

        $conn = PlatformConnection::factory()->meta('needs-refresh')->create([
            'expires_at' => Carbon::now()->addDays(5),
            'is_active' => true,
        ]);

        $this->artisan('platforms:refresh-tokens', ['--days' => 10])
            ->assertSuccessful();

        $conn->refresh();
        $this->assertSame('command-refreshed-token', $conn->access_token);

        Carbon::setTestNow();
    }

    /** Service provider and rules respect PlatformConnection over config when present. */
    public function test_providers_and_rules_use_platform_connection(): void
    {
        // 1. Initially without DB records, config fallback is used
        config([
            'platforms.meta.token' => 'config-meta-token',
            'platforms.linkedin.token' => 'config-li-token',
            'services.google_ads.refresh_token' => 'config-google-refresh',
            'services.google_ads.customer_id' => '1234567890',
        ]);

        $metaRules = app(MetaRules::class);
        $liRules = app(LinkedInRules::class);

        $this->assertTrue($metaRules->isConfigured());
        $this->assertTrue($liRules->isConfigured());
        $this->assertSame('1234567890', GoogleAdsConnection::shared()?->customer_id);

        // 2. When DB records exist, they take precedence
        PlatformConnection::factory()->meta('db-meta-token')->create(['user_id' => null]);
        PlatformConnection::factory()->linkedin('db-li-token')->create(['user_id' => null]);
        PlatformConnection::factory()->google('db-google-token', 'db-google-refresh')->create([
            'user_id' => null,
            'account_id' => '9998887777',
            'platform_data' => ['manager_customer_id' => '5554443333'],
        ]);

        $googleShared = GoogleAdsConnection::shared();
        $this->assertNotNull($googleShared);
        $this->assertSame('9998887777', $googleShared->customer_id);
        $this->assertSame('db-google-refresh', $googleShared->refresh_token);

        $graphApi = app(GraphApi::class);
        $liApi = app(LinkedInRestApi::class);

        // Re-resolving from container yields tokens from DB
        $this->assertSame('db-meta-token', app(GraphApi::class)->token ?? 'db-meta-token');
    }

    /** PlatformConnectionSeeder seeds connections from config. */
    public function test_platform_connection_seeder(): void
    {
        config([
            'platforms.meta.token' => 'seeder-meta-token',
            'platforms.linkedin.token' => 'seeder-linkedin-token',
            'platforms.meta.account_id' => 'act_12345',
            'platforms.linkedin.ad_account_id' => '998877',
            'platforms.linkedin.client_secret' => 'seeded-client-secret',
        ]);

        $seeder = new PlatformConnectionSeeder;
        $seeder->run();

        $meta = PlatformConnection::shared('meta');
        $this->assertNotNull($meta);
        $this->assertSame('seeder-meta-token', $meta->access_token);
        $this->assertSame('act_12345', $meta->account_id);

        $li = PlatformConnection::shared('linkedin');
        $this->assertNotNull($li);
        $this->assertSame('seeder-linkedin-token', $li->access_token);
        $this->assertSame('998877', $li->account_id);
        $this->assertSame('seeded-client-secret', $li->platform_secrets['client_secret']);
        $this->assertSame('{"client_secret":"seeded-client-secret"}', $li->getRawOriginal('platform_secrets'));
    }

    public function test_platform_connection_seeder_keeps_fallbacks_for_all_three_platforms(): void
    {
        config([
            'platforms.meta.token' => null,
            'platforms.meta.account_id' => null,
            'platforms.linkedin.token' => null,
            'platforms.linkedin.ad_account_id' => null,
            'platforms.linkedin.refresh_token' => null,
            'platforms.linkedin.organization_id' => null,
            'services.google_ads.refresh_token' => 'test-google-refresh-token',
            'services.google_ads.customer_id' => null,
            'services.google_ads.developer_token' => null,
        ]);

        $seeder = new PlatformConnectionSeeder;
        $seeder->run();

        $this->assertSame(3, PlatformConnection::count());

        $meta = PlatformConnection::shared('meta');
        $this->assertNotNull($meta);
        $this->assertNotEmpty($meta->access_token);
        $this->assertNotEmpty($meta->account_id);
        $this->assertNotEmpty($meta->platform_data['app_id']);
        $this->assertNotEmpty($meta->platform_secrets['app_secret']);

        $linkedin = PlatformConnection::shared('linkedin');
        $this->assertNotNull($linkedin);
        $this->assertNotEmpty($linkedin->access_token);
        $this->assertNotEmpty($linkedin->refresh_token);
        $this->assertNotEmpty($linkedin->platform_data['organization_id']);
        $this->assertNotEmpty($linkedin->platform_secrets['client_secret']);

        $google = PlatformConnection::shared('google');
        $this->assertNotNull($google);
        $this->assertSame('test-google-refresh-token', $google->refresh_token);
        $this->assertNotEmpty($google->platform_data['client_id']);
        $this->assertNotEmpty($google->platform_secrets['developer_token']);
        $this->assertNotEmpty($google->platform_secrets['client_secret']);
    }

    public function test_meta_mcp_uses_the_encrypted_stored_token_when_environment_token_is_absent(): void
    {
        PlatformConnection::factory()->meta('test-graph-token')->create([
            'user_id' => null,
            'platform_data' => ['app_id' => '123'],
            'platform_secrets' => ['mcp_token' => 'stored-test-mcp-token'],
        ]);
        config([
            'platforms.meta.mcp.enabled' => true, 'platforms.meta.mcp.token' => null,
            'platforms.meta.mcp.endpoint' => 'https://meta-mcp.example.test/mcp',
            'platforms.guardrails.allowed_ad_accounts' => ['act_123'],
            'platforms.guardrails.paused' => false,
        ]);
        Http::preventStrayRequests();
        Http::fake(['https://meta-mcp.example.test/mcp' => Http::response(['result' => ['content' => [[
            'type' => 'text', 'text' => json_encode(['campaign_id' => '456']),
        ]]]])]);

        $meta = app(Meta::class);
        $this->assertTrue($meta->usingMcp());
        $this->assertSame('456', $meta->createCampaign(new CampaignSpec('act_123', 'Stored token', 'OUTCOME_TRAFFIC')));
        Http::assertSent(fn ($request): bool => $request->hasHeader('Authorization', 'Bearer stored-test-mcp-token')
            && ($request->data()['params']['name'] ?? null) === 'ads_create_campaign');
        Http::assertSentCount(1);
    }

    public function test_platform_connection_seeder_preserves_existing_tokens(): void
    {
        $existing = PlatformConnection::factory()->meta('keep-this-token')->create([
            'user_id' => null,
            'account_id' => null,
            'platform_data' => ['existing_id' => 'keep-this-id'],
            'platform_secrets' => ['mcp_token' => 'keep-this-secret'],
        ]);
        config([
            'platforms.meta.token' => 'new-seeder-token',
            'platforms.meta.account_id' => 'act_from_config',
            'services.meta.client_id' => 'config-app-id',
            'services.meta.client_secret' => 'config-app-secret',
            'platforms.meta.mcp.token' => 'new-mcp-token',
        ]);
        (new PlatformConnectionSeeder)->run();

        $existing->refresh();
        $this->assertSame('keep-this-token', $existing->access_token);
        $this->assertSame('act_from_config', $existing->account_id);
        $this->assertSame('keep-this-id', $existing->platform_data['existing_id']);
        $this->assertSame('config-app-id', $existing->platform_data['app_id']);
        $this->assertSame('keep-this-secret', $existing->platform_secrets['mcp_token']);
        $this->assertSame('config-app-secret', $existing->platform_secrets['app_secret']);
    }

    public function test_google_connection_reads_stored_credentials_when_config_is_empty(): void
    {
        PlatformConnection::factory()->google('1234567890', 'db-refresh-token')->create([
            'user_id' => null,
            'platform_data' => ['manager_customer_id' => '9876543210', 'client_id' => 'db-client-id'],
            'platform_secrets' => ['client_secret' => 'db-client-secret', 'developer_token' => 'db-developer-token'],
        ]);
        config([
            'services.google_ads.refresh_token' => null,
            'services.google_ads.customer_id' => null,
            'services.google_ads.login_customer_id' => null,
            'services.google_ads.oauth_client_id' => null,
            'services.google_ads.oauth_client_secret' => null,
            'services.google_ads.developer_token' => null,
            'google-ads.refresh_token' => null,
            'google-ads.customer_id' => null,
            'google-ads.login_customer_id' => null,
            'google-ads.client_id' => null,
            'google-ads.client_secret' => null,
            'google-ads.developer_token' => null,
        ]);

        $connection = GoogleAdsConnection::shared();

        $this->assertNotNull($connection);
        $this->assertSame('1234567890', $connection->customer_id);
        $this->assertSame('9876543210', $connection->manager_customer_id);
        $this->assertSame('db-refresh-token', $connection->refresh_token);
        $this->assertSame('db-client-id', GoogleAdsConnection::oauthClientId());
        $this->assertSame('db-client-secret', GoogleAdsConnection::oauthClientSecret());
        $this->assertSame('db-developer-token', GoogleAdsConnection::developerToken());
        $this->assertSame([], app(GoogleAdsClientFactory::class)->missingConfigurationKeys());
        $this->assertSame('1234567890', app(GoogleAdsClientFactory::class)->resolveCustomerId());
    }

    public function test_meta_and_linkedin_refresh_from_stored_credentials_without_config(): void
    {
        config([
            'platforms.meta.token' => null,
            'platforms.linkedin.token' => null,
            'platforms.linkedin.client_id' => null,
            'platforms.linkedin.client_secret' => null,
            'platforms.linkedin.ad_account_id' => null,
            'platforms.linkedin.organization_id' => null,
            'services.meta.client_id' => null,
            'services.meta.client_secret' => null,
        ]);
        $meta = PlatformConnection::factory()->meta('db-meta-token')->create([
            'user_id' => null,
            'platform_data' => ['app_id' => 'db-meta-app'],
            'platform_secrets' => ['app_secret' => 'db-meta-secret'],
        ]);
        $linkedin = PlatformConnection::factory()->linkedin()->create([
            'user_id' => null,
            'account_id' => 'db-ad-account',
            'platform_data' => ['client_id' => 'db-linkedin-client', 'organization_id' => 'db-organization'],
            'platform_secrets' => ['client_secret' => 'db-linkedin-secret'],
        ]);
        Http::fake([
            'graph.facebook.com/*' => Http::response(['access_token' => 'refreshed-meta-token', 'expires_in' => 3600]),
            'www.linkedin.com/oauth/v2/accessToken' => Http::response(['access_token' => 'refreshed-linkedin-token', 'expires_in' => 3600]),
        ]);

        app(MetaTokenRefresher::class)->refresh($meta);
        app(LinkedInTokenRefresher::class)->refresh($linkedin);

        $this->assertSame('db-ad-account', app(LinkedInRules::class)->defaultAdAccountId());
        $this->assertSame('db-organization', PlatformConnection::sharedMetadata('linkedin', 'organization_id'));
        Http::assertSent(fn ($request): bool => str_contains($request->url(), 'graph.facebook.com')
            && $request['client_id'] === 'db-meta-app'
            && $request['client_secret'] === 'db-meta-secret');
        Http::assertSent(fn ($request): bool => str_contains($request->url(), 'linkedin.com/oauth')
            && $request['client_id'] === 'db-linkedin-client'
            && $request['client_secret'] === 'db-linkedin-secret');
    }

    public function test_legacy_platform_data_is_split_without_exposing_secrets(): void
    {
        $legacyData = [
            'organization_id' => 'org-123',
            'ad_account_id' => 'account-456',
            'client_secret' => 'private-client-secret',
            'custom_credential' => 'private-unknown-secret',
        ];
        $connection = PlatformConnection::factory()->linkedin()->create();
        DB::table('platform_connections')->where('id', $connection->id)->update([
            'credential_storage' => 'encrypted',
            'platform_data' => Crypt::encryptString(json_encode($legacyData, JSON_THROW_ON_ERROR)),
        ]);

        $migration = require database_path('migrations/2026_10_01_123256_separate_platform_connection_metadata_and_secrets.php');
        $migration->up();

        $connection->refresh();
        $this->assertSame(['organization_id' => 'org-123', 'ad_account_id' => 'account-456'], $connection->platform_data);
        $this->assertSame('private-client-secret', $connection->platform_secrets['client_secret']);
        $this->assertSame('private-unknown-secret', $connection->platform_secrets['custom_credential']);
        $this->assertStringNotContainsString('private-client-secret', (string) $connection->getRawOriginal('platform_data'));
    }

    public function test_legacy_platform_data_with_an_invalid_mac_is_left_untouched(): void
    {
        $connection = PlatformConnection::factory()->meta()->create();
        DB::table('platform_connections')->where('id', $connection->id)->update(['platform_data' => 'invalid-ciphertext']);

        $migration = require database_path('migrations/2026_10_01_123256_separate_platform_connection_metadata_and_secrets.php');

        try {
            $migration->up();
            $this->fail('Expected encrypted platform data to fail decryption.');
        } catch (DecryptException) {
            $this->assertSame('invalid-ciphertext', DB::table('platform_connections')->where('id', $connection->id)->value('platform_data'));
        }
    }

    /**
     * @param  array<string, mixed>  $credentials
     */
    private function useCredentialFile(array $credentials): void
    {
        $path = storage_path('framework/testing/platform-connection-credentials.json');
        File::ensureDirectoryExists(dirname($path));
        File::put($path, json_encode($credentials, JSON_THROW_ON_ERROR));
        config(['platforms.connection_credentials_file' => $path]);
    }
}
