<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\DB;
use Laravel\Fortify\Features;

abstract class TestCase extends BaseTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        // SQLite lacks GREATEST/LEAST scalar functions — register PHP-backed
        // shims so the haversine and other math expressions used in
        // production-style queries run identically in :memory: tests.
        // Idempotent: registering twice on the same PDO is harmless.
        if (DB::connection()->getDriverName() === 'sqlite') {
            $pdo = DB::connection()->getPdo();
            $pdo->sqliteCreateFunction('GREATEST', fn (...$args) => max($args), -1);
            $pdo->sqliteCreateFunction('LEAST', fn (...$args) => min($args), -1);
        }
    }

    protected function skipUnlessFortifyHas(string $feature, ?string $message = null): void
    {
        if (! Features::enabled($feature)) {
            $this->markTestSkipped($message ?? "Fortify feature [{$feature}] is not enabled.");
        }
    }
}
