const DB_NAME = 'PartyMediaDB';
const DB_VERSION = 2;
let dbInstance = null;

export const MediaStorage = {
    open: () => {
        if (dbInstance) return Promise.resolve(dbInstance);
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(DB_NAME, DB_VERSION);
            request.onupgradeneeded = (e) => {
                const db = e.target.result;
                if (!db.objectStoreNames.contains('music')) db.createObjectStore('music');
                if (!db.objectStoreNames.contains('tapes')) db.createObjectStore('tapes');
                if (!db.objectStoreNames.contains('poster')) db.createObjectStore('poster');
            };
            request.onsuccess = (e) => {
                dbInstance = e.target.result;
                dbInstance.onclose = () => { dbInstance = null; };
                dbInstance.onversionchange = () => { if (dbInstance) dbInstance.close(); dbInstance = null; };
                resolve(dbInstance);
            };
            request.onerror = (e) => reject(e);
        });
    },
    saveMusic: async (file) => {
        const db = await MediaStorage.open();
        return new Promise((resolve, reject) => {
            const tx = db.transaction('music', 'readwrite');
            tx.oncomplete = () => resolve();
            tx.onerror = (e) => reject(e.target.error);
            tx.objectStore('music').put(file, 'currentSong');
        });
    },
    getMusic: async () => {
        const db = await MediaStorage.open();
        return new Promise((resolve) => {
            const req = db.transaction('music', 'readonly').objectStore('music').get('currentSong');
            req.onsuccess = () => resolve(req.result);
            req.onerror = () => resolve(null);
        });
    },
    saveTapes: async (files) => {
        const db = await MediaStorage.open();
        const tx = db.transaction('tapes', 'readwrite');
        const store = tx.objectStore('tapes');
        store.clear();
        Array.from(files).forEach((file, i) => store.put(file, i));
    },
    getTapes: async () => {
        const db = await MediaStorage.open();
        return new Promise((resolve) => {
            const req = db.transaction('tapes', 'readonly').objectStore('tapes').getAll();
            req.onsuccess = () => resolve(req.result);
            req.onerror = () => resolve([]);
        });
    },
    savePoster: async (file) => {
        const db = await MediaStorage.open();
        const tx = db.transaction('poster', 'readwrite');
        tx.objectStore('poster').put(file, 'currentPoster');
    },
    getPoster: async () => {
        const db = await MediaStorage.open();
        return new Promise((resolve) => {
            const req = db.transaction('poster', 'readonly').objectStore('poster').get('currentPoster');
            req.onsuccess = () => resolve(req.result);
            req.onerror = () => resolve(null);
        });
    },
    deletePoster: async () => {
        const db = await MediaStorage.open();
        const tx = db.transaction('poster', 'readwrite');
        tx.objectStore('poster').delete('currentPoster');
    },
    clear: async () => {
        const db = await MediaStorage.open();
        const tx = db.transaction(['music', 'tapes', 'poster'], 'readwrite');
        tx.objectStore('music').clear();
        tx.objectStore('tapes').clear();
        tx.objectStore('poster').clear();
    }
};