const fs = require('fs');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const html = fs.readFileSync('index.html', 'utf8');
const dom = new JSDOM(html, {
    url: "http://localhost/",
    runScripts: "dangerously"
});

const window = dom.window;
global.window = window;
global.document = window.document;
global.navigator = window.navigator;

window.indexedDB = { open: () => ({ onupgradeneeded: null, onsuccess: null, onerror: null }) };

try {
    window.eval(fs.readFileSync('db.js', 'utf8'));
    window.eval(fs.readFileSync('app_final.js', 'utf8'));
    window.eval(fs.readFileSync('vouchers_returns.js', 'utf8'));
    window.eval(fs.readFileSync('subcontracts.js', 'utf8'));

    // Mock data
    window.currentProjects = [{id: 1, name: 'P1'}];
    window.currentContacts = [{id: 1, name: 'C1', type: 'supplier'}];
    window.currentSubcontracts = [{id: 1, project_id: 1, subcontractor_id: 1, contract_number: '123', total_value: 1000}];
    
    console.log("Calling openSubcontractModal()...");
    window.openSubcontractModal(1);
    console.log("openSubcontractModal() successful!");

    console.log("Calling openClientAbstractModal()...");
    if (typeof window.openClientAbstractModal === 'function') {
        window.openClientAbstractModal(null);
        console.log("openClientAbstractModal() successful!");
    } else {
        console.error("openClientAbstractModal is not defined!");
    }

} catch (e) {
    console.error("Error:", e);
}
