<?php

namespace Tests\Feature;

use App\Models\GtsProvider;
use App\Models\GtsService;
use App\Models\GtsWallet;
use App\Models\User;
use App\Services\GtsProviderApi;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class GtsProviderApiFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_gts_order_auto_submits_to_provider_when_service_is_mapped(): void
    {
        Http::fake([
            'provider.test/*' => Http::response(['order' => '998877'], 200),
        ]);

        $client = User::create(['name' => 'GTS Buyer', 'email' => 'gts-buyer@mgg.test', 'password' => 'password', 'role' => 'gts_client']);
        GtsWallet::create(['user_id' => $client->id, 'balance' => 5000, 'currency' => 'PKR', 'status' => 'active']);
        $provider = GtsProvider::create(['name' => 'Provider', 'api_url' => 'https://provider.test/api/v2', 'api_key' => 'secret', 'status' => 'active']);
        $service = GtsService::create([
            'gts_provider_id' => $provider->id,
            'provider_service_id' => '123',
            'category' => 'TikTok',
            'name' => 'TikTok Views',
            'min_quantity' => 1000,
            'max_quantity' => 10000,
            'rate_per_1000' => 35,
            'provider_rate_per_1000' => 25,
            'profit_percent' => 40,
            'auto_submit' => true,
            'status' => 'active',
        ]);

        $this->actingAs($client)->post('/gts-client/orders', [
            'gts_service_id' => $service->id,
            'target_link' => 'https://social.example/video',
            'quantity' => 1000,
        ])->assertRedirect('/gts-client/orders');

        $this->assertDatabaseHas('gts_orders', [
            'user_id' => $client->id,
            'gts_service_id' => $service->id,
            'provider_order_id' => '998877',
            'status' => 'processing',
        ]);
    }

    public function test_provider_service_sync_applies_selected_commission(): void
    {
        Http::fake([
            'provider.test/*' => Http::response([[
                'service' => '8219',
                'name' => 'WhatsApp Channel Members',
                'category' => 'WhatsApp',
                'rate' => '2.220000',
                'min' => '10',
                'max' => '10000',
                'type' => 'Default',
                'refill' => false,
                'dripfeed' => false,
            ]], 200),
        ]);

        $provider = GtsProvider::create(['name' => 'Provider', 'api_url' => 'https://provider.test/api/v2', 'api_key' => 'secret', 'status' => 'active']);

        app(GtsProviderApi::class)->syncServices($provider, 50);

        $this->assertDatabaseHas('gts_services', [
            'gts_provider_id' => $provider->id,
            'provider_service_id' => '8219',
            'name' => 'WhatsApp Channel Members',
            'provider_rate_per_1000' => 2.22,
            'profit_percent' => 50,
            'rate_per_1000' => 3.33,
            'auto_submit' => true,
        ]);
    }
}
