// Load and render the Amazon links status table
chrome.storage.local.get({ amazonLinksStatus: [] }, async (result) => {
    const amazonLinksStatus = result.amazonLinksStatus;
    console.log('amazonLinksStatus', amazonLinksStatus);

    const tableBody = document.querySelector('#table-body');

    for (let i = 0; i < amazonLinksStatus.length; i++) {
        const item = amazonLinksStatus[i];

        const row = tableBody.insertRow();
        const indexCell = row.insertCell();
        const urlCell = row.insertCell();
        const statusCell = row.insertCell();
        const messageCell = row.insertCell();
        const ebayCell = row.insertCell();

        // Column 1: Row number
        indexCell.textContent = i + 1;

        // Column 2: Amazon URL (as clickable link if present)
        urlCell.textContent = '';
        if (item.url) {
            const link = document.createElement('a');
            link.href = item.url;
            link.textContent = item.url;
            link.target = '_blank';
            link.rel = 'noopener noreferrer';
            urlCell.appendChild(link);
        }

        // Column 3: Status (colored red/green based on value)
        statusCell.textContent = item.status;
        if (item.status === 'itemFailed') {
            statusCell.style.color = 'red';
        } else if (item.status === 'itemListed') {
            statusCell.style.color = 'green';
        }

        // Column 4: Message
        messageCell.textContent = item.message;

        // Column 5: eBay item link (as clickable link if present)
        ebayCell.textContent = '';
        if (item.ebayItemLink) {
            const link = document.createElement('a');
            link.href = item.ebayItemLink;
            link.textContent = item.ebayItemLink;
            link.target = '_blank';
            link.rel = 'noopener noreferrer';
            ebayCell.appendChild(link);
        }
    }
});

// Clear button: wipe the status list and reload the page
document.querySelector('#clear').addEventListener('click', () => {
    chrome.storage.local.set({ amazonLinksStatus: [] }, () => {
        console.log('amazonLinksStatus cleared');
        window.location.reload();
    });
});



