<?php

use App\Domains\Bio\Models\BioPage;
use App\Domains\Workspace\Models\Workspace;
use App\Enums\BioUrlFormat;

it('returns free path-url for free plan workspace', function () {
    $ws = Workspace::factory()->free()->create(['slug' => 'nexxo']);
    $bio = BioPage::factory()->for($ws)->create(['slug' => 'my-bio']);

    expect($bio->publicUrl())->toBe('https://nimu.li/b/nexxo/my-bio');
});

it('returns shortPublicUrl without scheme', function () {
    $ws = Workspace::factory()->free()->create(['slug' => 'nexxo']);
    $bio = BioPage::factory()->for($ws)->create(['slug' => 'my-bio']);

    expect($bio->shortPublicUrl())->toBe('nimu.li/b/nexxo/my-bio');
});

it('returns pro subdomain-url when bio_url_format=Subdomain and subdomain_full set', function () {
    $ws = Workspace::factory()->pro()->create([
        'slug'           => 'acme',
        'subdomain_full' => 'acme.nimu.li',
        'bio_url_format' => BioUrlFormat::Subdomain,
    ]);
    $bio = BioPage::factory()->for($ws)->create(['slug' => 'john']);

    expect($bio->publicUrl())->toBe('https://acme.nimu.li/b/john');
});

it('returns pro path-url by default (no bio_url_format set)', function () {
    $ws = Workspace::factory()->pro()->create([
        'slug'           => 'acme',
        'subdomain_full' => 'acme.nimu.li',
    ]);
    $bio = BioPage::factory()->for($ws)->create(['slug' => 'john']);

    expect($bio->publicUrl())->toBe('https://nimu.li/b/acme/john');
});

it('bio url_format_override=Subdomain overrides workspace format', function () {
    $ws = Workspace::factory()->pro()->create([
        'slug'           => 'acme',
        'subdomain_full' => 'acme.nimu.li',
    ]);
    $bio = BioPage::factory()->for($ws)->create([
        'slug'                => 'john',
        'url_format_override' => BioUrlFormat::Subdomain,
    ]);

    expect($bio->publicUrl())->toBe('https://acme.nimu.li/b/john');
});

it('returns custom-domain-url when subdomain_host is non-apex domain', function () {
    $ws = Workspace::factory()->pro()->create(['slug' => 'acme']);
    $bio = BioPage::factory()->for($ws)->create([
        'slug'           => 'page',
        'subdomain_host' => 'links.acme.de',
    ]);

    expect($bio->publicUrl())->toBe('https://links.acme.de/b/page');
});

it('falls back to free path when no subdomain_full on paid plan', function () {
    $ws = Workspace::factory()->pro()->create([
        'slug'           => 'acme',
        'subdomain_full' => null,
    ]);
    $bio = BioPage::factory()->for($ws)->create(['slug' => 'test']);

    expect($bio->publicUrl())->toBe('https://nimu.li/b/acme/test');
});

it('returns empty string when needs_migration is true', function () {
    $ws = Workspace::factory()->free()->create(['slug' => 'nexxo']);
    $bio = BioPage::factory()->for($ws)->create([
        'slug'            => 'migrating',
        'needs_migration' => true,
    ]);

    expect($bio->publicUrl())->toBe('');
});
