import './bootstrap';
import '../css/app.css';
import '../../src/scss/app.scss';

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy';
import AlertModal from './Components/AlertModal.vue';
import ConfirmModal from './Components/ConfirmModal.vue';

const appName = import.meta.env.VITE_APP_NAME || 'Laravel';

// Global override for window.alert to show as a styled modal
window.alert = (message) => {
    const div = document.createElement('div');
    document.body.appendChild(div);

    const app = createApp(AlertModal, {
        message: String(message),
        title: 'Notification',
        onClose: () => {
            app.unmount();
            div.remove();
        }
    });

    app.mount(div);
};

// Global override for window.confirm to show as a styled confirm modal
window.confirm = (message) => {
    return new Promise((resolve) => {
        const div = document.createElement('div');
        document.body.appendChild(div);

        const lowerMsg = String(message).toLowerCase();
        const isDestructive = lowerMsg.includes('delete') || 
                              lowerMsg.includes('remove') || 
                              lowerMsg.includes('clear') || 
                              lowerMsg.includes('undo');

        const app = createApp(ConfirmModal, {
            message: String(message),
            title: isDestructive ? 'Confirm Deletion' : 'Confirm Action',
            confirmText: isDestructive ? 'Delete' : 'Confirm',
            isDestructive: isDestructive,
            onResolve: (result) => {
                app.unmount();
                div.remove();
                resolve(result);
            }
        });

        app.mount(div);
    });
};

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, App, props, plugin }) {
        return createApp({ render: () => h(App, props) })
            .use(plugin)
            .use(ZiggyVue)
            .mount(el);
    },
    progress: {
        color: '#4B5563',
    },
});
