<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Http;
use Mockery;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function setUp(): void
    {
        parent::setUp();

        // Prevent Vite manifest errors in tests (no npm build in CI)
        $this->withoutVite();

        // Clean up any existing Mockery instances before each test
        if (class_exists(Mockery::class)) {
            Mockery::close();
            Mockery::globalHelpers();
        }

        // Prevent any external HTTP requests during tests unless explicitly faked in a test
        Http::preventStrayRequests();
    }

    protected function tearDown(): void
    {
        // Clear HTTP fakes between tests to prevent interference
        Http::clearResolvedInstances();

        // Clear all facade instances to prevent interference
        Facade::clearResolvedInstances();

        // Ensure Mockery is properly closed to prevent facade interference
        if (class_exists(Mockery::class)) {
            Mockery::close();
        }

        parent::tearDown();
    }
}
