define('custom:handlers/kuendigung/eviction-action', [], function () {
    
    class EvictionActionHandler {
        
        constructor(view) {
            this.view = view;
        }
        
        /**
         * Setup-Methode wird automatisch aufgerufen
         */
        initInitiateEviction() {
            // Optional: Button-Logik nach Render
        }
        
        /**
         * Action-Handler (wird bei Button-Click aufgerufen)
         */
        actionInitiateEviction() {
            console.log('actionInitiateEviction called from Kündigung');
            const model = this.view.model;
            
            // Confirmation Dialog
            this.view.confirm(
                this.view.translate('confirmEviction', 'messages', 'CKuendigung'),
                () => {
                    console.log('Confirmation accepted, initiating eviction from Kündigung');
                    this.initiateEviction(model.id);
                }
            );
        }
        
        /**
         * AJAX Request zum Backend
         */
        initiateEviction(kuendigungId) {
            Espo.Ui.notify(this.view.translate('pleaseWait', 'messages'));
            
            Espo.Ajax.postRequest('CKuendigung/action/initiateEviction', {
                id: kuendigungId
            })
            .then(response => {
                Espo.Ui.success(
                    this.view.translate('evictionCreated', 'messages', 'CKuendigung')
                );
                
                // Navigation zur erstellten Räumungsklage
                this.view.getRouter().navigate(
                    '#CVmhRumungsklage/view/' + response.id, 
                    {trigger: true}
                );
            })
            .catch(xhr => {
                console.error('Eviction initiation failed:', xhr);
                
                let errorMessage = this.view.translate('evictionError', 'messages', 'CKuendigung');
                
                if (xhr.status === 403) {
                    errorMessage = this.view.translate('Access denied', 'messages');
                } else if (xhr.status === 404) {
                    errorMessage = this.view.translate('Not found', 'messages');
                }
                
                Espo.Ui.error(errorMessage);
            });
        }
    }
    
    return EvictionActionHandler;
});
