<?php

namespace Tests;

use App\Newsletter\CampaignMonitor;
use App\Newsletter\MailChimp;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenantCreated;

abstract class TenantTestCase extends BaseTestCase
{
    use CreatesApplication;
    use DispatchesJobs;
    use UsesDatabase;
    use UsesTenancy;

    #[\Override]
    protected function setUp(): void
    {
        parent::setUp();

        $this->app->bind(MailChimp::class, fn () => new MailChimp(new FakeMailchimp('test-api-key')));

        $this->app->bind(
            CampaignMonitor::class,
            fn () => new CampaignMonitor(new FakeCampaignMonitor('list-id', ['api_key' => 'test-api-key']))
        );

        $this->needsMasterDatabase = false;
        $this->useRealTenant = false;

        Event::fake([TenantCreated::class]);

        $this->setUpDatabases();
        $this->initializeTenancy();
    }

    #[\Override]
    protected function tearDown(): void
    {
        $this->tearDownDatabase();
        parent::tearDown();
        gc_collect_cycles();
    }
}
