import assert from 'node:assert/strict';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { closeConnection } from '../../../modules/database/connection.js';
import { initializeDatabase } from '../../../modules/database/init-db.js';
import { projectsDb } from '../../../modules/database/repositories/projects.db.js';
import { userDb } from '../../../modules/database/repositories/users.js';
async function withIsolatedDatabase(runTest) {
    const previousDatabasePath = process.env.DATABASE_PATH;
    const tempDirectory = await mkdtemp(path.join(tmpdir(), 'projects-db-'));
    const databasePath = path.join(tempDirectory, 'auth.db');
    closeConnection();
    process.env.DATABASE_PATH = databasePath;
    await initializeDatabase();
    const created = userDb.createUser('test-user', 'hash');
    const userId = Number(created.id);
    try {
        await runTest(userId);
    }
    finally {
        closeConnection();
        if (previousDatabasePath === undefined) {
            delete process.env.DATABASE_PATH;
        }
        else {
            process.env.DATABASE_PATH = previousDatabasePath;
        }
        await rm(tempDirectory, { recursive: true, force: true });
    }
}
test('projectsDb.createProjectPath returns created for fresh paths', async () => {
    await withIsolatedDatabase((userId) => {
        const created = projectsDb.createProjectPath(userId, '/workspace/new-project');
        assert.equal(created.outcome, 'created');
        assert.ok(created.project);
        assert.equal(created.project?.project_path, '/workspace/new-project');
        assert.equal(created.project?.isArchived, 0);
    });
});
test('projectsDb.createProjectPath returns reactivated_archived for archived duplicates', async () => {
    await withIsolatedDatabase((userId) => {
        const initial = projectsDb.createProjectPath(userId, '/workspace/archived-project', 'Archived Project');
        assert.equal(initial.outcome, 'created');
        assert.ok(initial.project);
        projectsDb.updateProjectIsArchived(userId, '/workspace/archived-project', true);
        const reused = projectsDb.createProjectPath(userId, '/workspace/archived-project', 'Renamed Project');
        assert.equal(reused.outcome, 'reactivated_archived');
        assert.ok(reused.project);
        assert.equal(reused.project?.project_id, initial.project?.project_id);
        assert.equal(reused.project?.isArchived, 0);
    });
});
test('projectsDb.createProjectPath returns active_conflict for active duplicates', async () => {
    await withIsolatedDatabase((userId) => {
        const initial = projectsDb.createProjectPath(userId, '/workspace/active-project');
        assert.equal(initial.outcome, 'created');
        assert.ok(initial.project);
        const conflict = projectsDb.createProjectPath(userId, '/workspace/active-project');
        assert.equal(conflict.outcome, 'active_conflict');
        assert.ok(conflict.project);
        assert.equal(conflict.project?.project_id, initial.project?.project_id);
        assert.equal(conflict.project?.isArchived, 0);
    });
});
//# sourceMappingURL=projects.db.integration.test.js.map