<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class WebsiteManagementTest extends TestCase
{
    use RefreshDatabase;

    public function test_super_admin_can_create_a_website(): void
    {
        Http::fake([
            'https://atlas.example.com/agent/health' => Http::response([
                'status' => 'online',
                'health' => 'healthy',
                'version' => 'v1.0.0',
                'database_count' => 2,
                'table_count' => 16,
                'capabilities' => ['health', 'tables', 'backups'],
            ], 200),
            'https://atlas.example.com/agent/tables' => Http::response([
                'tables' => [
                    [
                        'table_name' => 'users',
                        'row_count' => 8,
                        'size_bytes' => 4096,
                        'last_updated_at' => now()->toIso8601String(),
                        'structure' => ['columns' => [['name' => 'id', 'type' => 'bigint']]],
                    ],
                    [
                        'table_name' => 'orders',
                        'row_count' => 18,
                        'size_bytes' => 8192,
                        'last_updated_at' => now()->toIso8601String(),
                        'structure' => ['columns' => [['name' => 'id', 'type' => 'bigint']]],
                    ],
                ],
            ], 200),
        ]);

        $user = User::factory()->superAdmin()->create();

        $response = $this->actingAs($user)->post(route('websites.store'), [
            'name' => 'Atlas Retail',
            'domain' => 'atlas.example.com',
            'connection_type' => 'api_agent',
            'api_endpoint' => 'https://atlas.example.com/agent',
            'api_key' => 'cc_test_key_1234567890',
            'signing_secret' => 'atlas-signing-secret',
            'description' => 'Primary commerce property.',
        ]);

        $website = Website::query()->first();

        $response->assertRedirect(route('websites.show', $website));
        $this->assertDatabaseHas('managed_sites', [
            'name' => 'Atlas Retail',
            'domain' => 'atlas.example.com',
            'connection_type' => 'api_agent',
            'created_by' => $user->id,
        ]);
        $this->assertDatabaseCount('site_health_logs', 1);
        $this->assertDatabaseCount('api_agent_logs', 2);
        $this->assertDatabaseCount('table_snapshots', 2);
        $this->assertNotNull($website->agent);
        $this->assertSame('verified', $website->agent->authentication_status);
    }

    public function test_super_admin_can_view_websites_index(): void
    {
        $user = User::factory()->superAdmin()->create();
        Website::factory()->count(2)->for($user, 'createdBy')->create();

        $this->actingAs($user)
            ->get(route('websites.index'))
            ->assertOk()
            ->assertSee('Manage remote Laravel websites');
    }
}
