// static/admin/js/user-create.js

// Create custom logger for user create
const userCreateLog = window.LogConfig.createLogger('USER-CREATE');

function adminUserCreate() {
    return {
        // Inherit base layout functionality from init-alpine.js
        ...data(),

        // User create page specific state
        currentPage: 'user-create',
        formData: {
            username: '',
            email: '',
            password: '',
            first_name: '',
            last_name: '',
            role: 'vendor'
        },
        errors: {},
        saving: false,

        // Initialize
        async init() {
            userCreateLog.info('=== USER CREATE PAGE INITIALIZING ===');

            // Prevent multiple initializations
            if (window._userCreateInitialized) {
                userCreateLog.warn('User create page already initialized, skipping...');
                return;
            }
            window._userCreateInitialized = true;

            userCreateLog.info('=== USER CREATE PAGE INITIALIZATION COMPLETE ===');
        },

        // Submit form
        async handleSubmit() {
            userCreateLog.info('=== CREATING USER ===');
            userCreateLog.debug('Form data:', { ...this.formData, password: '[REDACTED]' });

            this.errors = {};
            this.saving = true;

            try {
                const url = `/admin/users`;
                window.LogConfig.logApiCall('POST', url, { ...this.formData, password: '[REDACTED]' }, 'request');

                const startTime = performance.now();
                const response = await apiClient.post(url, this.formData);
                const duration = performance.now() - startTime;

                window.LogConfig.logApiCall('POST', url, response, 'response');
                window.LogConfig.logPerformance('Create User', duration);

                Utils.showToast('User created successfully', 'success');
                userCreateLog.info(`User created successfully in ${duration}ms`, response);

                // Redirect to the new user's detail page
                setTimeout(() => {
                    window.location.href = `/admin/users/${response.id}`;
                }, 1500);

            } catch (error) {
                window.LogConfig.logError(error, 'Create User');

                // Handle validation errors
                if (error.details && error.details.validation_errors) {
                    error.details.validation_errors.forEach(err => {
                        const field = err.loc?.[1] || err.loc?.[0];
                        if (field) {
                            this.errors[field] = err.msg;
                        }
                    });
                    userCreateLog.debug('Validation errors:', this.errors);
                }

                // Handle specific errors
                if (error.message) {
                    if (error.message.includes('Email already registered')) {
                        this.errors.email = 'This email is already registered';
                    } else if (error.message.includes('Username already taken')) {
                        this.errors.username = 'This username is already taken';
                    }
                }

                Utils.showToast(error.message || 'Failed to create user', 'error');
            } finally {
                this.saving = false;
                userCreateLog.info('=== USER CREATION COMPLETE ===');
            }
        }
    };
}

userCreateLog.info('User create module loaded');
