// app/modules/catalog/static/admin/js/product-edit.js
/**
 * Admin store product edit page logic
 * Edit store product information with translations
 */

const adminStoreProductEditLog = window.LogConfig.loggers.adminStoreProductEdit ||
    window.LogConfig.createLogger('adminStoreProductEdit', false);

adminStoreProductEditLog.info('Loading...');

function adminStoreProductEdit() {
    adminStoreProductEditLog.info('adminStoreProductEdit() called');

    // Extract product ID from URL
    const pathParts = window.location.pathname.split('/');
    const productId = parseInt(pathParts[pathParts.length - 2]); // /store-products/{id}/edit

    // Default translations structure
    const defaultTranslations = () => ({
        en: { title: '', description: '' },
        fr: { title: '', description: '' },
        de: { title: '', description: '' },
        lu: { title: '', description: '' }
    });

    return {
        // Inherit base layout state
        ...data(),

        // Include media picker functionality (store ID comes from loaded product)
        ...mediaPickerMixin(() => null, false),

        // Set page identifier
        currentPage: 'store-products',

        // Product ID from URL
        productId: productId,

        // Loading states
        loading: true,
        saving: false,
        error: '',

        // Product data from API
        product: null,

        // Active language tab
        activeLanguage: 'en',

        // Form data
        form: {
            // Translations by language
            translations: defaultTranslations(),
            // Product identifiers
            store_sku: '',
            brand: '',
            gtin: '',
            gtin_type: 'ean13',
            // Pricing
            price: null,
            sale_price: null,
            currency: 'EUR',
            tax_rate_percent: 17,
            availability: '',
            // Images
            primary_image_url: '',
            additional_images: [],
            // Product type & status
            is_digital: false,
            is_active: true,
            is_featured: false,
            // Optional supplier info
            supplier: '',
            cost: null
        },

        async init() {
            // Guard against multiple initialization
            if (window._adminStoreProductEditInitialized) {
                adminStoreProductEditLog.warn('Already initialized, skipping');
                return;
            }
            window._adminStoreProductEditInitialized = true;

            try {
                // Load i18n translations
                await I18n.loadModule('catalog');

                adminStoreProductEditLog.info('Store Product Edit init() called, ID:', this.productId);

                // Load product data
                await this.loadProduct();

                adminStoreProductEditLog.info('Store Product Edit initialization complete');
            } catch (error) {
                adminStoreProductEditLog.error('Init failed:', error);
                this.error = 'Failed to initialize product edit page';
            }
        },

        /**
         * Load product details and populate form
         */
        async loadProduct() {
            this.loading = true;
            this.error = '';

            try {
                const response = await apiClient.get(`/admin/store-products/${this.productId}`);
                this.product = response;

                adminStoreProductEditLog.info('Loaded product:', response);

                // Populate translations from store_translations
                const translations = defaultTranslations();
                if (response.store_translations) {
                    for (const lang of ['en', 'fr', 'de', 'lu']) {
                        if (response.store_translations[lang]) {
                            translations[lang] = {
                                title: response.store_translations[lang].title || '',
                                description: response.store_translations[lang].description || ''
                            };
                        }
                    }
                }

                // Populate form with current values
                this.form = {
                    translations: translations,
                    // Product identifiers
                    store_sku: response.store_sku || '',
                    brand: response.brand || '',
                    gtin: response.gtin || '',
                    gtin_type: response.gtin_type || 'ean13',
                    // Pricing (convert cents to euros if stored as cents)
                    price: response.price || null,
                    sale_price: response.sale_price || null,
                    currency: response.currency || 'EUR',
                    tax_rate_percent: response.tax_rate_percent ?? 17,
                    availability: response.availability || '',
                    // Images
                    primary_image_url: response.primary_image_url || '',
                    additional_images: response.additional_images || [],
                    // Product type & status
                    is_digital: response.is_digital ?? false,
                    is_active: response.is_active ?? true,
                    is_featured: response.is_featured ?? false,
                    // Optional supplier info
                    supplier: response.supplier || '',
                    cost: response.cost || null
                };

                adminStoreProductEditLog.info('Form initialized:', this.form);
            } catch (error) {
                adminStoreProductEditLog.error('Failed to load product:', error);
                this.error = error.message || 'Failed to load product details';
            } finally {
                this.loading = false;
            }
        },

        /**
         * Check if form is valid (all mandatory fields filled)
         */
        isFormValid() {
            // English title and description are required
            if (!this.form.translations.en.title?.trim()) return false;
            if (!this.form.translations.en.description?.trim()) return false;

            // Product identifiers
            if (!this.form.store_sku?.trim()) return false;
            if (!this.form.brand?.trim()) return false;
            if (!this.form.gtin?.trim()) return false;
            if (!this.form.gtin_type) return false;

            // Pricing
            if (this.form.price === null || this.form.price === '' || this.form.price < 0) return false;
            if (!this.form.currency) return false;
            if (this.form.tax_rate_percent === null || this.form.tax_rate_percent === '') return false;

            // Image
            if (!this.form.primary_image_url?.trim()) return false;

            return true;
        },

        /**
         * Generate a unique store SKU
         * Format: XXXX_XXXX_XXXX (includes store_id for uniqueness)
         */
        generateSku() {
            const storeId = this.product?.store_id || 0;

            // Generate random alphanumeric segments
            const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            const generateSegment = (length) => {
                let result = '';
                for (let i = 0; i < length; i++) {
                    result += chars.charAt(Math.floor(Math.random() * chars.length));
                }
                return result;
            };

            // First segment includes store ID (padded)
            const storeSegment = storeId.toString().padStart(4, '0').slice(-4);

            // Generate SKU: VID + random + random
            const sku = `${storeSegment}_${generateSegment(4)}_${generateSegment(4)}`;
            this.form.store_sku = sku;

            adminStoreProductEditLog.info('Generated SKU:', sku);
        },

        /**
         * Save product changes
         */
        async saveProduct() {
            if (!this.isFormValid()) {
                Utils.showToast(I18n.t('catalog.messages.please_fill_in_all_required_fields'), 'error');
                return;
            }

            this.saving = true;

            try {
                // Build translations object for API
                const translations = {};
                for (const lang of ['en', 'fr', 'de', 'lu']) {
                    const t = this.form.translations[lang];
                    // Only include if there's actual content
                    if (t.title?.trim() || t.description?.trim()) {
                        translations[lang] = {
                            title: t.title?.trim() || null,
                            description: t.description?.trim() || null
                        };
                    }
                }

                // Build update payload
                const payload = {
                    translations: Object.keys(translations).length > 0 ? translations : null,
                    // Product identifiers
                    store_sku: this.form.store_sku?.trim() || null,
                    brand: this.form.brand?.trim() || null,
                    gtin: this.form.gtin?.trim() || null,
                    gtin_type: this.form.gtin_type || null,
                    // Pricing
                    price: this.form.price !== null && this.form.price !== ''
                        ? parseFloat(this.form.price) : null,
                    sale_price: this.form.sale_price !== null && this.form.sale_price !== ''
                        ? parseFloat(this.form.sale_price) : null,
                    currency: this.form.currency || null,
                    tax_rate_percent: this.form.tax_rate_percent !== null && this.form.tax_rate_percent !== ''
                        ? parseInt(this.form.tax_rate_percent) : null,
                    availability: this.form.availability || null,
                    // Images
                    primary_image_url: this.form.primary_image_url?.trim() || null,
                    additional_images: this.form.additional_images?.length > 0
                        ? this.form.additional_images : null,
                    // Status
                    is_digital: this.form.is_digital,
                    is_active: this.form.is_active,
                    is_featured: this.form.is_featured,
                    // Optional supplier info
                    supplier: this.form.supplier?.trim() || null,
                    cost: this.form.cost !== null && this.form.cost !== ''
                        ? parseFloat(this.form.cost) : null
                };

                adminStoreProductEditLog.info('Saving payload:', payload);

                await apiClient.patch(`/admin/store-products/${this.productId}`, payload);

                adminStoreProductEditLog.info('Product saved:', this.productId);

                Utils.showToast(I18n.t('catalog.messages.product_updated_successfully'), 'success');

                // Redirect to detail page
                setTimeout(() => {
                    window.location.href = `/admin/store-products/${this.productId}`;
                }, 1000);
            } catch (error) {
                adminStoreProductEditLog.error('Failed to save product:', error);
                Utils.showToast(error.message || I18n.t('catalog.messages.failed_to_save_product'), 'error');
            } finally {
                this.saving = false;
            }
        },

        // === Media Picker Overrides ===
        // These override the mixin methods to use proper form/product context

        /**
         * Load media library for the product's store
         */
        async loadMediaLibrary() {
            const storeId = this.product?.store_id;

            if (!storeId) {
                adminStoreProductEditLog.warn('Media picker: No store ID available');
                return;
            }

            this.mediaPickerState.loading = true;
            this.mediaPickerState.skip = 0;

            try {
                const params = new URLSearchParams({
                    skip: '0',
                    limit: this.mediaPickerState.limit.toString(),
                    media_type: 'image',
                });

                if (this.mediaPickerState.search) {
                    params.append('search', this.mediaPickerState.search);
                }

                const response = await apiClient.get(
                    `/admin/media/stores/${storeId}?${params.toString()}`
                );

                this.mediaPickerState.media = response.media || [];
                this.mediaPickerState.total = response.total || 0;
            } catch (error) {
                adminStoreProductEditLog.error('Failed to load media library:', error);
                Utils.showToast(I18n.t('catalog.messages.failed_to_load_media_library'), 'error');
            } finally {
                this.mediaPickerState.loading = false;
            }
        },

        /**
         * Load more media (pagination)
         */
        async loadMoreMedia() {
            const storeId = this.product?.store_id;
            if (!storeId) return;

            this.mediaPickerState.loading = true;
            this.mediaPickerState.skip += this.mediaPickerState.limit;

            try {
                const params = new URLSearchParams({
                    skip: this.mediaPickerState.skip.toString(),
                    limit: this.mediaPickerState.limit.toString(),
                    media_type: 'image',
                });

                if (this.mediaPickerState.search) {
                    params.append('search', this.mediaPickerState.search);
                }

                const response = await apiClient.get(
                    `/admin/media/stores/${storeId}?${params.toString()}`
                );

                this.mediaPickerState.media = [
                    ...this.mediaPickerState.media,
                    ...(response.media || [])
                ];
            } catch (error) {
                adminStoreProductEditLog.error('Failed to load more media:', error);
            } finally {
                this.mediaPickerState.loading = false;
            }
        },

        /**
         * Upload a new media file
         */
        async uploadMediaFile(event) {
            const file = event.target.files?.[0];
            if (!file) return;

            const storeId = this.product?.store_id;

            if (!storeId) {
                Utils.showToast(I18n.t('catalog.messages.no_store_associated_with_this_product'), 'error');
                return;
            }

            if (!file.type.startsWith('image/')) {
                Utils.showToast(I18n.t('catalog.messages.please_select_an_image_file'), 'error');
                return;
            }

            if (file.size > 10 * 1024 * 1024) {
                Utils.showToast(I18n.t('catalog.messages.image_must_be_less_than_10mb'), 'error');
                return;
            }

            this.mediaPickerState.uploading = true;

            try {
                const formData = new FormData();
                formData.append('file', file);

                const response = await apiClient.postFormData(
                    `/admin/media/stores/${storeId}/upload?folder=products`,
                    formData
                );

                if (response.success && response.media) {
                    this.mediaPickerState.media.unshift(response.media);
                    this.mediaPickerState.total++;
                    this.toggleMediaSelection(response.media);
                    Utils.showToast(I18n.t('catalog.messages.image_uploaded_successfully'), 'success');
                }
            } catch (error) {
                adminStoreProductEditLog.error('Failed to upload image:', error);
                Utils.showToast(error.message || I18n.t('catalog.messages.failed_to_upload_image'), 'error');
            } finally {
                this.mediaPickerState.uploading = false;
                event.target.value = '';
            }
        },

        /**
         * Set main image from media picker
         */
        setMainImage(media) {
            this.form.primary_image_url = media.url;
            adminStoreProductEditLog.info('Main image set:', media.url);
        },

        /**
         * Add additional images from media picker
         */
        addAdditionalImages(mediaList) {
            const newUrls = mediaList.map(m => m.url);
            this.form.additional_images = [
                ...this.form.additional_images,
                ...newUrls
            ];
            adminStoreProductEditLog.info('Additional images added:', newUrls);
        },

        /**
         * Remove an additional image by index
         */
        removeAdditionalImage(index) {
            this.form.additional_images.splice(index, 1);
        },

        /**
         * Clear the main image
         */
        clearMainImage() {
            this.form.primary_image_url = '';
        }
    };
}
