<template>
    <div v-if="hasFormData" class="form-message">
        <div v-if="formData.name" class="form-message-header">
            <i v-if="formData.icon" class="material-icons form-message-icon">{{ formData.icon }}</i>
            <span class="form-message-title">{{ formData.name }}</span>
        </div>

        <div class="form-message-fields">
            <div v-for="field in formattedFields" :key="field.id" class="form-message-field">
                <div class="field-message-label">{{ field.name }}:</div>
                <div class="field-message-value" :class="{'text-value': field.type === 'text'}">{{ field.value }}</div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'FormMessage',
    props: {
        formData: {
            type: Object,
            required: true
        },
        formValues: {
            type: Object,
            required: true
        }
    },
    computed: {
        hasFormData() {
            // Check if formData and fields exist
            if (!this.formData || !this.formData.fields || Object.keys(this.formData.fields).length === 0) {
                return false;
            }
            
            // Check if formValues exist and contain meaningful data
            if (!this.formValues || Object.keys(this.formValues).length === 0) {
                return false;
            }
            
            // Check if any formValues have actual content (not empty, null, or undefined)
            const hasActualValues = Object.entries(this.formValues).some(([key, value]) => {
                // Skip if the field doesn't exist in formData
                if (!this.formData.fields[key]) return false;
                
                // Check for meaningful values
                if (value === null || value === undefined) return false;
                if (typeof value === 'string' && value.trim() === '') return false;
                if (typeof value === 'boolean') return true; // Boolean values are always meaningful
                if (Array.isArray(value) && value.length === 0) return false;
                
                return true;
            });
            
            return hasActualValues;
        },
        formattedFields() {
            if (!this.hasFormData) return [];

            return Object.entries(this.formData.fields).map(([fieldId, field]) => {
                let displayValue = this.formValues[fieldId] || '';

                // Format different field types
                if (field.type === 'boolean') {
                    displayValue = displayValue ? 'Ja' : 'Nee';
                } else if (field.type === 'enum' && !displayValue && field.default) {
                    displayValue = field.default;
                } else if (field.type === 'text') {
                    // Voor tekstgebieden, behoud witruimte
                    // De CSS zal dit tonen met white-space: pre-wrap
                }

                return {
                    id: fieldId,
                    name: field.name,
                    value: displayValue || '-',
                    type: field.type
                };
            });
        }
    }
};
</script>

<style scoped>
/* Styling voor formulier in berichten */
.form-message {
    margin-bottom: 12px;
    border-radius: 8px;
    background-color: rgba(245, 245, 245, 0.7);
    padding: 12px;
    border: 1px solid #e0e0e0;
}

.form-message-header {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    padding-bottom: 8px;
    border-bottom: 1px solid #e0e0e0;
}

.form-message-icon {
    margin-right: 8px;
    font-size: 18px;
    color: #555;
}

.form-message-title {
    font-weight: 600;
    color: #333;
    font-size: 1em;
}

.form-message-fields {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-message-field {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 4px 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.form-message-field:last-child {
    border-bottom: none;
}

.field-message-label {
    font-weight: 500;
    color: #555;
    flex: 0 0 40%;
    padding-right: 10px;
    word-break: break-word;
}

.field-message-value {
    flex: 1;
    color: #333;
    word-break: break-word;
    text-align: right;
}

.field-message-value.text-value {
    white-space: pre-wrap;
    text-align: left;
}

/* Styling voor verschillende message contexten */
.message.user .form-message {
    background-color: rgba(255, 255, 255, 0.1);
}

.message.ai .form-message {
    background-color: rgba(245, 245, 250, 0.7);
}

/* Speciale styling voor read-only formulieren in user messages */
.user-form .form-field {
    margin-bottom: 6px !important;
}

.user-form .field-label {
    font-weight: 500 !important;
    color: #555 !important;
    padding: 2px 0 !important;
}

.user-form .field-value {
    padding: 2px 0 !important;
}

/* Schakel hover effecten uit voor read-only formulieren */
.read-only .form-field:hover {
    background-color: transparent;
}

/* Subtiele scheiding tussen velden */
.dynamic-form.read-only .form-fields {
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    margin-top: 10px;
    padding-top: 8px;
}

/* Verklein vorm titels in berichten */
.message-form .form-title {
    font-size: 1em !important;
}

.message-form .form-description {
    font-size: 0.85em !important;
}

.form-readonly {
    width: 100%;
}

.form-readonly .field-label {
    font-weight: 500;
    color: #555;
}

.form-readonly .field-value {
    word-break: break-word;
}

.form-readonly .text-value {
    white-space: pre-wrap;
}

/* Algemene styling verbetering voor berichten */
.message-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.message-content {
    max-width: 90%;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .form-message-field {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }
    
    .field-message-label {
        flex: none;
        padding-right: 0;
    }
    
    .field-message-value {
        text-align: left;
    }
    
    .form-message {
        padding: 10px;
    }
    
    .form-message-title {
        font-size: 0.9em;
    }
}

/* Dark theme support */
@media (prefers-color-scheme: dark) {
    .form-message {
        background-color: rgba(40, 40, 40, 0.7);
        border-color: #555;
    }
    
    .form-message-header {
        border-bottom-color: #555;
    }
    
    .form-message-title {
        color: #e0e0e0;
    }
    
    .field-message-label {
        color: #ccc;
    }
    
    .field-message-value {
        color: #e0e0e0;
    }
    
    .form-message-field {
        border-bottom-color: rgba(255, 255, 255, 0.1);
    }
}

/* High contrast mode */
@media (prefers-contrast: more) {
    .form-message {
        border: 2px solid #000;
        background-color: #fff;
    }
    
    .form-message-title,
    .field-message-label,
    .field-message-value {
        color: #000;
    }
    
    .form-message-header {
        border-bottom: 2px solid #000;
    }
}

.form-message .message-content {
    max-width: 90%;
    background: white;
    border: 1px solid #e9ecef;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
</style>