<?php

namespace Tests\Feature;

use App\Jobs\UploadImportedCustomersToMikrotikJob;
use App\Models\Mikrotik;
use App\Models\Package;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;

class CustomerCsvImportTest extends TestCase
{
    use RefreshDatabase;

    public function test_csv_import_creates_missing_package_and_customer_without_router_load_when_disconnected(): void
    {
        Queue::fake();
        $user = User::factory()->create();
        Permission::findOrCreate('customer.create');
        $user->givePermissionTo('customer.create');
        $router = Mikrotik::create([
            'name' => 'Test Router', 'ip_address' => '192.0.2.1', 'api_port' => 8728,
            'username' => 'admin', 'password' => 'secret', 'status' => true, 'connected' => false,
        ]);
        $csv = "customer_code,name,contact_phone,email,ip_address,package,package_speed,monthly_bill,pppoe_password,status,connection_date,disconnect_date,address\nCUS1001,Imported User,01700000000,,10.0.0.10,20 Mbps,20M/20M,1200,pass123,active,2026-07-01,2026-08-01,Dhaka\n";

        $response = $this->actingAs($user)->post(route('admin.customers.import'), [
            'csv_file' => UploadedFile::fake()->createWithContent('customers.csv', $csv),
            'mikrotik_id' => $router->id,
        ]);

        $response->assertRedirect()->assertSessionHas('customer_import.created', 1);
        $this->assertDatabaseHas('packages', ['name' => '20 Mbps', 'mikrotik_profile_name' => '20 Mbps']);
        $this->assertDatabaseHas('customers', ['customer_code' => 'CUS1001', 'mikrotik_id' => $router->id]);
        Queue::assertNotPushed(UploadImportedCustomersToMikrotikJob::class);
    }

    public function test_connected_router_upload_is_queued_and_existing_package_is_reused(): void
    {
        Queue::fake();
        $user = User::factory()->create();
        Permission::findOrCreate('customer.create');
        $user->givePermissionTo('customer.create');
        $router = Mikrotik::create([
            'name' => 'Online Router', 'ip_address' => '192.0.2.2', 'api_port' => 8728,
            'username' => 'admin', 'password' => 'secret', 'status' => true, 'connected' => true,
        ]);
        $package = Package::create(['name' => '10 Mbps', 'net_speed' => '10M/10M', 'price' => 800, 'status' => true]);
        $csv = "customer_code,name,contact_phone,email,ip_address,package,package_speed,monthly_bill,pppoe_password,status,connection_date,disconnect_date,address\nCUS1002,Queued User,01800000000,,10.0.0.11,10 Mbps,10M/10M,800,pass456,active,,,,\n";

        $this->actingAs($user)->post(route('admin.customers.import'), [
            'csv_file' => UploadedFile::fake()->createWithContent('customers.csv', $csv),
            'mikrotik_id' => $router->id,
        ])->assertRedirect()->assertSessionHas('customer_import.mikrotik_queued', true);

        $this->assertSame(1, Package::count());
        $this->assertSame('10 Mbps', $package->fresh()->mikrotik_profile_name);
        Queue::assertPushed(UploadImportedCustomersToMikrotikJob::class, fn ($job) => $job->mikrotikId === $router->id && count($job->customerIds) === 1);
    }
}
