// static/admin/js/vendor-detail.js

// ✅ Use centralized logger - ONE LINE!
// Create custom logger for vendor detail
const detailLog = window.LogConfig.createLogger('VENDOR-DETAIL');

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

        // Vendor detail page specific state
        currentPage: 'vendor-detail',
        vendor: null,
        loading: false,
        error: null,
        vendorCode: null,

        // Initialize
        async init() {
            detailLog.info('=== VENDOR DETAIL PAGE INITIALIZING ===');

            // Prevent multiple initializations
            if (window._vendorDetailInitialized) {
                detailLog.warn('Vendor detail page already initialized, skipping...');
                return;
            }
            window._vendorDetailInitialized = true;

            // Get vendor code from URL
            const path = window.location.pathname;
            const match = path.match(/\/admin\/vendors\/([^\/]+)$/);

            if (match) {
                this.vendorCode = match[1];
                detailLog.info('Viewing vendor:', this.vendorCode);
                await this.loadVendor();
            } else {
                detailLog.error('No vendor code in URL');
                this.error = 'Invalid vendor URL';
                Utils.showToast('Invalid vendor URL', 'error');
            }

            detailLog.info('=== VENDOR DETAIL PAGE INITIALIZATION COMPLETE ===');
        },

        // Load vendor data
        async loadVendor() {
            detailLog.info('Loading vendor details...');
            this.loading = true;
            this.error = null;

            try {
                const url = `/admin/vendors/${this.vendorCode}`;
                window.LogConfig.logApiCall('GET', url, null, 'request');

                const startTime = performance.now();
                const response = await apiClient.get(url);
                const duration = performance.now() - startTime;

                window.LogConfig.logApiCall('GET', url, response, 'response');
                window.LogConfig.logPerformance('Load Vendor Details', duration);

                this.vendor = response;

                detailLog.info(`Vendor loaded in ${duration}ms`, {
                    vendor_code: this.vendor.vendor_code,
                    name: this.vendor.name,
                    is_verified: this.vendor.is_verified,
                    is_active: this.vendor.is_active
                });
                detailLog.debug('Full vendor data:', this.vendor);

            } catch (error) {
                window.LogConfig.logError(error, 'Load Vendor Details');
                this.error = error.message || 'Failed to load vendor details';
                Utils.showToast('Failed to load vendor details', 'error');
            } finally {
                this.loading = false;
            }
        },

        // Format date (matches dashboard pattern)
        formatDate(dateString) {
            if (!dateString) {
                detailLog.debug('formatDate called with empty dateString');
                return '-';
            }
            const formatted = Utils.formatDate(dateString);
            detailLog.debug(`Date formatted: ${dateString} -> ${formatted}`);
            return formatted;
        },

        // Delete vendor
        async deleteVendor() {
            detailLog.info('Delete vendor requested:', this.vendorCode);

            if (!confirm(`Are you sure you want to delete vendor "${this.vendor.name}"?\n\nThis action cannot be undone and will delete:\n- All products\n- All orders\n- All customers\n- All team members`)) {
                detailLog.info('Delete cancelled by user');
                return;
            }

            // Second confirmation for safety
            if (!confirm(`FINAL CONFIRMATION\n\nType the vendor code to confirm: ${this.vendor.vendor_code}\n\nAre you absolutely sure?`)) {
                detailLog.info('Delete cancelled by user (second confirmation)');
                return;
            }

            try {
                const url = `/admin/vendors/${this.vendorCode}?confirm=true`;
                window.LogConfig.logApiCall('DELETE', url, null, 'request');

                detailLog.info('Deleting vendor:', this.vendorCode);
                await apiClient.delete(url);

                window.LogConfig.logApiCall('DELETE', url, null, 'response');

                Utils.showToast('Vendor deleted successfully', 'success');
                detailLog.info('Vendor deleted successfully');

                // Redirect to vendors list
                setTimeout(() => window.location.href = '/admin/vendors', 1500);

            } catch (error) {
                window.LogConfig.logError(error, 'Delete Vendor');
                Utils.showToast(error.message || 'Failed to delete vendor', 'error');
            }
        },

        // Refresh vendor data
        async refresh() {
            detailLog.info('=== VENDOR REFRESH TRIGGERED ===');
            await this.loadVendor();
            Utils.showToast('Vendor details refreshed', 'success');
            detailLog.info('=== VENDOR REFRESH COMPLETE ===');
        }
    };
}

detailLog.info('Vendor detail module loaded');