<?php
declare(strict_types=1);

namespace Tests;

use App\Core\Testing\TestDatabase;

abstract class DatabaseTestCase extends TestCase
{
    protected TestDatabase $db;

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

        try {
            $this->db = new TestDatabase(
                $_ENV['DB_HOST'] ?? '127.0.0.1',
                (int) ($_ENV['DB_PORT'] ?? 3306),
                $_ENV['DB_NAME'] ?? 'club_erp_test',
                $_ENV['DB_USER'] ?? 'root',
                $_ENV['DB_PASS'] ?? ''
            );
        } catch (\PDOException $e) {
            $this->markTestSkipped('Database not available: ' . $e->getMessage());
        }

        $this->db->beginTransaction();

        AppTestHarness::activate($this->db, $this->createTestEmployee());
    }

    protected function tearDown(): void
    {
        $this->db->forceRollBack();
        AppTestHarness::deactivate();
        parent::tearDown();
    }

    protected function createTestEmployee(): object
    {
        return (object) [
            'id' => 1,
            'full_name_ar' => 'موظف اختبار',
            'full_name_en' => 'Test Employee',
            'is_active' => 1,
        ];
    }

    protected function factory(string $factoryClass): object
    {
        return new $factoryClass($this->db);
    }
}
