<?php

use App\Models\Service;
use App\Models\User;
use App\Notifications\ServiceCredentialsNotification;

beforeEach(function () {
    $this->user = User::factory()->create(['name' => 'John Doe']);
    $this->service = Service::factory()->create([
        'user_id' => $this->user->id,
        'service_type' => 'vps',
        'hostname' => 'vps1.ezscale.cloud',
        'ipv4_address' => '192.168.1.100',
    ]);
});

test('notification renders with all fields including port and panel url', function () {
    $credentials = [
        'username' => 'root',
        'password' => 's3cur3P@ss',
        'hostname' => 'vps1.ezscale.cloud',
        'ip_address' => '192.168.1.100',
        'port' => 22,
        'panel_url' => 'https://panel.ezscale.cloud/server/123',
    ];

    $notification = new ServiceCredentialsNotification($this->service, $credentials);
    $mail = $notification->toMail($this->user);

    expect($mail->greeting)->toBe('Hello John Doe!')
        ->and($mail->introLines)->toContain('Here are your access credentials:')
        ->and($mail->introLines)->toContain('**Hostname:** vps1.ezscale.cloud')
        ->and($mail->introLines)->toContain('**IP Address:** 192.168.1.100')
        ->and($mail->introLines)->toContain('**Username:** root')
        ->and($mail->introLines)->toContain('**Password:** s3cur3P@ss')
        ->and($mail->introLines)->toContain('**Port:** 22')
        ->and($mail->actionText)->toBe('Access Control Panel')
        ->and($mail->actionUrl)->toBe('https://panel.ezscale.cloud/server/123')
        ->and($mail->outroLines)->toContain('For security, we recommend changing your password after first login.')
        ->and($mail->outroLines)->toContain('If you need help, our support team is available 24/7.');
});

test('notification renders without optional fields', function () {
    $credentials = [
        'username' => 'root',
        'password' => 's3cur3P@ss',
        'hostname' => 'dedicated1.ezscale.cloud',
        'ip_address' => '10.0.0.50',
        'port' => null,
        'panel_url' => null,
    ];

    $service = Service::factory()->create([
        'user_id' => $this->user->id,
        'service_type' => 'dedicated',
    ]);

    $notification = new ServiceCredentialsNotification($service, $credentials);
    $mail = $notification->toMail($this->user);

    // Port line should not be present
    $allLines = array_merge($mail->introLines, $mail->outroLines);
    $portLines = array_filter($allLines, fn (string $line): bool => str_contains($line, '**Port:**'));
    expect($portLines)->toBeEmpty();

    // No action button
    expect($mail->actionText)->toBeNull()
        ->and($mail->actionUrl)->toBeNull();
});

test('notification uses mail channel only', function () {
    $credentials = [
        'username' => 'root',
        'password' => 'test123',
        'hostname' => 'server.ezscale.cloud',
        'ip_address' => '10.0.0.1',
        'port' => null,
        'panel_url' => null,
    ];

    $notification = new ServiceCredentialsNotification($this->service, $credentials);

    expect($notification->via($this->user))->toBe(['mail']);
});

test('notification subject includes service type', function () {
    $credentials = [
        'username' => 'root',
        'password' => 'test123',
        'hostname' => 'server.ezscale.cloud',
        'ip_address' => '10.0.0.1',
        'port' => null,
        'panel_url' => null,
    ];

    // Test VPS service type
    $notification = new ServiceCredentialsNotification($this->service, $credentials);
    $mail = $notification->toMail($this->user);
    expect($mail->subject)->toBe('Your Vps Server Credentials - EZSCALE');

    // Test dedicated service type
    $dedicatedService = Service::factory()->create([
        'user_id' => $this->user->id,
        'service_type' => 'dedicated',
    ]);
    $notification = new ServiceCredentialsNotification($dedicatedService, $credentials);
    $mail = $notification->toMail($this->user);
    expect($mail->subject)->toBe('Your Dedicated Server Credentials - EZSCALE');

    // Test hosting service type
    $hostingService = Service::factory()->create([
        'user_id' => $this->user->id,
        'service_type' => 'hosting',
    ]);
    $notification = new ServiceCredentialsNotification($hostingService, $credentials);
    $mail = $notification->toMail($this->user);
    expect($mail->subject)->toBe('Your Hosting Server Credentials - EZSCALE');

    // Test game_server service type (with underscore)
    $gameService = Service::factory()->create([
        'user_id' => $this->user->id,
        'service_type' => 'game_server',
    ]);
    $notification = new ServiceCredentialsNotification($gameService, $credentials);
    $mail = $notification->toMail($this->user);
    expect($mail->subject)->toBe('Your Game server Server Credentials - EZSCALE');
});
