<?php

namespace Tests;

use App\SiteRepository;
use App\Tier;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Mockery\MockInterface;
use Mtc\MercuryDataModels\Tenant;
use Spatie\Permission\Middlewares\PermissionMiddleware;
use Spatie\Permission\Middlewares\RoleMiddleware;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;

abstract class TenantTestCase extends \Orchestra\Testbench\TestCase
{
    use CreatesApplication;
    use RefreshDatabase;
    use DispatchesJobs;

    protected function setUp(): void
    {
        parent::setUp();
        $fakeTenant = new Tenant([
            'tier' => Tier::ENTERPRISE->value,
            'name' => 'foo faz',
        ]);
        $fakeTenant->id = 'test-tenant';

        $this->withoutMiddleware([
            'auth:sanctum',
            InitializeTenancyByRequestData::class,
            PermissionMiddleware::class,
            RoleMiddleware::class,
        ]);

        \tenancy()->initialize($fakeTenant);
        Config::set('database.default', 'sqlite');

        $this->partialMock(SiteRepository::class, function (MockInterface $mock) {
            $mock->shouldReceive('baseUrl')->andReturn('/foo.localhost');
        });


        $this->app->bind('feature-flags', TestFeatureRepository::class);
        $this->app->bind('site-settings', TestSettingRepository::class);
    }
    protected function migrateUsing()
    {
        return [
            '--schema-path' => database_path('schema/test-tenant-schema.sql'),
            '--path' => database_path('migrations/tenant'),
            '--realpath' => true,
        ];
    }
}
