/**
 * StrictInterface - Ultra-strict base class with visual error enforcement
 * Forces implementation of abstract methods with impossible-to-ignore errors
 */

class ImplementationError extends Error {
    constructor(message, className, methodName) {
        super(message);
        this.name = 'ImplementationError';
        this.className = className;
        this.methodName = methodName;
    }
}

class StrictInterface {
    constructor(implementationName) {
        this._implementationName = implementationName;
        this._validateImplementation();
    }

    _validateImplementation() {
        const proto = Object.getPrototypeOf(this);
        const abstractMethods = this._getAbstractMethods();

        abstractMethods.forEach(method => {
            // Check if method exists and is not the base implementation
            if (!proto[method] || proto[method] === StrictInterface.prototype[method]) {
                this._throwImplementationError(method);
            }
        });
    }

    _getAbstractMethods() {
        // Override in subclasses to define required methods
        return [];
    }

    _throwImplementationError(methodName) {
        const error = new ImplementationError(
            `❌ FATAL: ${this._implementationName} must implement ${methodName}()`,
            this._implementationName,
            methodName
        );

        // Ultra-visible console error
        console.error('%c🔥 IMPLEMENTATION ERROR 🔥',
            'background: red; color: white; font-size: 20px; padding: 10px; font-weight: bold'
        );
        console.error(`Class: ${this._implementationName}`);
        console.error(`Missing Method: ${methodName}()`);
        console.error(error);

        // Full-screen red error UI
        this._showUIError(error);

        throw error;
    }

    _showUIError(error) {
        // Create full-screen red overlay
        const overlay = document.createElement('div');
        overlay.id = 'strict-interface-error-overlay';
        overlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(220, 38, 38, 0.98);
            color: white;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 999999;
            font-family: 'Courier New', monospace;
            padding: 40px;
            animation: errorShake 0.5s;
        `;

        overlay.innerHTML = `
            <div style="max-width: 800px; text-align: center; background: rgba(0,0,0,0.2); padding: 40px; border-radius: 12px; border: 3px solid white;">
                <div style="font-size: 72px; margin-bottom: 20px;">🔥</div>
                <h1 style="font-size: 48px; margin: 0; font-weight: bold; text-shadow: 2px 2px 4px rgba(0,0,0,0.5);">
                    FATAL ERROR
                </h1>
                <h2 style="font-size: 28px; margin: 20px 0; opacity: 0.9;">
                    Implementation Missing
                </h2>
                <div style="background: rgba(0,0,0,0.4); padding: 30px; border-radius: 8px; margin: 30px 0; border-left: 5px solid white;">
                    <p style="font-size: 24px; margin: 15px 0;">
                        <strong>Class:</strong> ${error.className}
                    </p>
                    <p style="font-size: 24px; margin: 15px 0;">
                        <strong>Missing Method:</strong> <code style="background: rgba(255,255,255,0.2); padding: 5px 10px; border-radius: 4px;">${error.methodName}()</code>
                    </p>
                    <p style="font-size: 18px; margin: 25px 0; color: #ffcccc; line-height: 1.6;">
                        ${error.message}
                    </p>
                </div>
                <div style="background: rgba(255,255,255,0.1); padding: 20px; border-radius: 8px; margin: 20px 0;">
                    <p style="font-size: 16px; margin: 10px 0; opacity: 0.9;">
                        ⚠️ All progress items MUST implement the interface contract
                    </p>
                    <p style="font-size: 14px; margin: 10px 0; opacity: 0.8;">
                        Check console for full stack trace
                    </p>
                </div>
                <button onclick="this.parentElement.parentElement.remove()"
                    style="background: white; color: #dc2626; border: none; padding: 18px 50px;
                           font-size: 18px; font-weight: bold; border-radius: 8px; cursor: pointer;
                           margin-top: 20px; font-family: 'Courier New', monospace;
                           box-shadow: 0 4px 6px rgba(0,0,0,0.3); transition: transform 0.2s;"
                    onmouseover="this.style.transform='scale(1.05)'"
                    onmouseout="this.style.transform='scale(1)'">
                    DISMISS (FIX REQUIRED!)
                </button>
            </div>
        `;

        // Add shake animation
        const style = document.createElement('style');
        style.textContent = `
            @keyframes errorShake {
                0%, 100% { transform: translateX(0); }
                10%, 30%, 50%, 70%, 90% { transform: translateX(-10px); }
                20%, 40%, 60%, 80% { transform: translateX(10px); }
            }
        `;
        document.head.appendChild(style);

        // Remove existing error overlay if any
        const existing = document.getElementById('strict-interface-error-overlay');
        if (existing) existing.remove();

        document.body.appendChild(overlay);

        // Play alert sound in dev mode
        if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
            this._playErrorSound();
        }
    }

    _playErrorSound() {
        try {
            // Simple beep sound (data URI for a warning beep)
            const audio = new Audio('data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIF2i78OScTgwNUKzn77djGgU7k9bx0HwoBS15y/HajD4KElyw6OyqWBEJQ5zd8sFuJAUqgc3y2ok3CBdou+/mnU0MDk6r5vC7YxwBN5HX8Mx6LAUueMvx2Yw8ChJcr+jrqlcVCkGc3fS+cygELH7N8tmJOAgWarmq7KBOCw==');
            audio.play().catch(() => {}); // Ignore if autoplay blocked
        } catch (e) {
            // Ignore audio errors
        }
    }
}

export default StrictInterface;
export { ImplementationError };
