<?php

namespace Tests\Feature\Audit;

use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Services\CompanyProvisioner;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CompanyProvisionerTest extends TestCase
{
    use RefreshDatabase;

    public function test_suspend_changes_status_to_suspended(): void
    {
        $company = $this->makeCompany('active');
        app(CompanyProvisioner::class)->suspend($company);
        $this->assertEquals('suspended', $company->fresh()->status);
    }

    public function test_reactivate_restores_active_status(): void
    {
        $company = $this->makeCompany('suspended');
        app(CompanyProvisioner::class)->reactivate($company);
        $this->assertEquals('active', $company->fresh()->status);
    }

    public function test_archive_marks_status_and_soft_deletes(): void
    {
        $company = $this->makeCompany('active');
        $id = $company->id;
        app(CompanyProvisioner::class)->archive($company);

        $row = Company::withTrashed()->find($id);
        $this->assertEquals('archived', $row->status);
        $this->assertNotNull($row->deleted_at, 'soft-deleted');

        // Default (with SoftDeletes scope) no longer returns it.
        $this->assertNull(Company::find($id));
    }

    private function makeCompany(string $status): Company
    {
        $plan = Plan::firstOrCreate(['slug' => 'test'], ['name' => 'T', 'price' => 0, 'features' => []]);
        return Company::create([
            'plan_id' => $plan->id, 'slug' => 'cp-' . uniqid(),
            'name' => 'CP Co', 'status' => $status,
        ]);
    }
}
