<?php

use App\Domains\Link\Models\Link;
use App\Domains\Subscription\Models\Plan;
use App\Domains\Subscription\Services\PlanLimitsService;
use App\Domains\Workspace\Models\Workspace;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

it('allows link creation when under limit', function () {
    $plan = Plan::factory()->create(['link_limit' => 10]);
    $workspace = Workspace::factory()->create(['plan_id' => $plan->id]);

    $service = new PlanLimitsService;
    expect($service->canCreateLink($workspace))->toBeTrue();
});

it('denies link creation when at limit', function () {
    $plan = Plan::factory()->create(['link_limit' => 2]);
    $workspace = Workspace::factory()->create(['plan_id' => $plan->id]);
    Link::factory()->count(2)->create(['workspace_id' => $workspace->id]);

    $service = new PlanLimitsService;
    expect($service->canCreateLink($workspace))->toBeFalse();
});

it('returns true when workspace has no plan', function () {
    $workspace = Workspace::factory()->create(['plan_id' => null]);

    $service = new PlanLimitsService;
    // No plan = free tier or trial = allow
    expect($service->canCreateLink($workspace))->toBeTrue();
});
