function runStartupTests(db, modules) {
    return new Promise((resolve) => {
        const results = [];
        const tableHeader = '\n┌─────────────────────────────────────┬──────────┬──────────────────────────┐';
        const tableRow = '├─────────────────────────────────────┼──────────┼──────────────────────────┤';
        const tableFooter = '└─────────────────────────────────────┴──────────┴──────────────────────────┘';

        console.log('\n========================================');
        console.log('   ЗАПУСК ТЕСТОВ API ПРИ СТАРТЕ');
        console.log('========================================');

        console.log(tableHeader);
        console.log('│ API Функция                          │ Статус  │ Описание                 │');
        console.log(tableRow);

        function addResult(name, status, description) {
            const statusText = status === 'OK' ? 'OK' : 'FAIL';
            const paddedName = name.padEnd(37);
            const paddedStatus = statusText.padEnd(8);
            const paddedDesc = (description || '').padEnd(26);
            console.log(`│ ${paddedName} │ ${paddedStatus} │ ${paddedDesc} │`);
            results.push({ name, status, description });
        }

        try {
            addResult('DB Connection', 'OK', 'SQLite connected');

            db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='bookings'`, [], (err, row) => {
                if (err || !row) {
                    addResult('Table: bookings', 'FAIL', 'Not found');
                } else {
                    addResult('Table: bookings', 'OK', 'Exists');
                }

                db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='users'`, [], (err, row) => {
                    if (err || !row) {
                        addResult('Table: users', 'FAIL', 'Not found');
                    } else {
                        addResult('Table: users', 'OK', 'Exists');
                    }

                    db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='promocodes'`, [], (err, row) => {
                        if (err || !row) {
                            addResult('Table: promocodes', 'FAIL', 'Not found');
                        } else {
                            addResult('Table: promocodes', 'OK', 'Exists');
                        }

                        db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='rooms'`, [], (err, row) => {
                            if (err || !row) {
                                addResult('Table: rooms', 'FAIL', 'Not found');
                            } else {
                                addResult('Table: rooms', 'OK', 'Exists');
                            }

                            db.all(`SELECT COUNT(*) as cnt FROM users`, [], (err, rows) => {
                                if (err) {
                                    addResult('Query: users count', 'FAIL', err.message.substring(0, 24));
                                } else {
                                    addResult('Query: users count', 'OK', `Found ${rows[0].cnt} users`);
                                }

                                db.all(`SELECT COUNT(*) as cnt FROM bookings`, [], (err, rows) => {
                                    if (err) {
                                        addResult('Query: bookings count', 'FAIL', err.message.substring(0, 24));
                                    } else {
                                        addResult('Query: bookings count', 'OK', `Found ${rows[0].cnt} bookings`);
                                    }

                                    if (modules.auth) {
                                        addResult('Module: auth', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: auth', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.bookings) {
                                        addResult('Module: bookings', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: bookings', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.promocodes) {
                                        addResult('Module: promocodes', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: promocodes', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.rooms) {
                                        addResult('Module: rooms', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: rooms', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.users) {
                                        addResult('Module: users', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: users', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.adminBookings) {
                                        addResult('Module: adminBookings', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: adminBookings', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.settings) {
                                        addResult('Module: settings', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: settings', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.reviews) {
                                        addResult('Module: reviews', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: reviews', 'FAIL', 'Not loaded');
                                    }

                                    if (modules.translations) {
                                        addResult('Module: translations', 'OK', 'Initialized');
                                    } else {
                                        addResult('Module: translations', 'FAIL', 'Not loaded');
                                    }

                                    db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='settings'`, [], (err, row) => {
                                        if (err || !row) {
                                            addResult('Table: settings', 'FAIL', 'Not found');
                                        } else {
                                            addResult('Table: settings', 'OK', 'Exists');
                                        }

                                        db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='reviews'`, [], (err, row) => {
                                            if (err || !row) {
                                                addResult('Table: reviews', 'FAIL', 'Not found');
                                            } else {
                                                addResult('Table: reviews', 'OK', 'Exists');
                                            }

                                            console.log(tableFooter);

                                            const passed = results.filter(r => r.status === 'OK').length;
                                            const total = results.length;
                                            const allPassed = passed === total;

                                            console.log(`\nРезультат: ${passed}/${total} тестов пройдено`);
                                            console.log(allPassed ? '✅ Все системы готовы к работе!' : '❌ Имеются проблемы - проверьте логи');
                                            console.log('========================================\n');

                                            resolve({ results, passed, total, allPassed });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        } catch (error) {
            addResult('Critical Error', 'FAIL', error.message.substring(0, 24));
            console.log(tableFooter);
            console.log(`\n❌ Критическая ошибка: ${error.message}`);
            resolve({ results, passed: 0, total: results.length, allPassed: false });
        }
    });
}

module.exports = { runStartupTests };