<?php

namespace App\Services;

use Illuminate\Support\Facades\DB;

class JsonTranslationService
{
    /**
     * Get translation data from request
     */
    public static function getTranslationData($request, $fields, $languages = ['en', 'ar', 'fr'])
    {
        $translations = [];

        foreach ($languages as $lang) {
            $translationData = [];
            foreach ($fields as $field) {
                $key = "{$field}_{$lang}";
                if ($request->has($key)) {
                    $translationData[$field] = $request->input($key);
                }
            }

            if (!empty($translationData)) {
                $translations[$lang] = $translationData;
            }
        }

        return $translations;
    }

    /**
     * Prepare JSON data for database storage
     */
    public static function prepareJsonData($request, $fields, $languages = null)
    {
        // Use form languages from config if not provided
        if ($languages === null) {
            $languages = config('app.form_languages', ['en', 'ar', 'fr', 'de']);
        }

        $jsonData = [];

        foreach ($fields as $field) {
            $fieldTranslations = [];
            $hasNonEmptyValue = false;

            foreach ($languages as $lang) {
                $key = "{$field}_{$lang}";
                $value = $request->input($key, '');
                
                if (is_string($value)) {
                    $value = trim($value);
                }

                // Auto-generate slug if field is 'slug' and value is empty
                if ($field === 'slug' && empty($value)) {
                    $nameKey = "name_{$lang}";
                    $nameValue = $request->input($nameKey, '');
                    if (!empty($nameValue)) {
                        if ($lang === 'ar') {
                            // For Arabic, keep it as Arabic characters and just replace spaces with dashes
                            $value = preg_replace('/\s+/u', '-', trim($nameValue));
                            $value = preg_replace('/[^\p{L}\p{N}_-]+/u', '', $value);
                        } else {
                            $value = \Illuminate\Support\Str::slug($nameValue);
                        }
                    } else {
                        // If name is also empty, try to get name from any language as fallback
                        foreach ($languages as $fallbackLang) {
                            $fallbackNameKey = "name_{$fallbackLang}";
                            $fallbackNameValue = $request->input($fallbackNameKey, '');
                            if (!empty($fallbackNameValue)) {
                                if ($lang === 'ar' || $fallbackLang === 'ar') {
                                    $value = preg_replace('/\s+/u', '-', trim($fallbackNameValue));
                                    $value = preg_replace('/[^\p{L}\p{N}_-]+/u', '', $value);
                                } else {
                                    $value = \Illuminate\Support\Str::slug($fallbackNameValue);
                                }
                                break;
                            }
                        }
                    }
                }
                
                // Normalize slug: replace spaces with hyphens (even if manually entered)
                if ($field === 'slug' && !empty($value)) {
                    // Replace all spaces with hyphens
                    $value = preg_replace('/\s+/u', '-', trim($value));
                    // Remove multiple consecutive hyphens
                    $value = preg_replace('/-+/u', '-', $value);
                    // Remove leading/trailing hyphens
                    $value = trim($value, '-');
                }

                if ($value === '' || $value === null) {
                    $fieldTranslations[$lang] = null;
                } else {
                    $fieldTranslations[$lang] = $value;
                    $hasNonEmptyValue = true;
                }
            }

            // For slug field, ensure we always have at least one value
            if ($field === 'slug' && !$hasNonEmptyValue) {
                // Try to generate slug from name in any available language
                foreach ($languages as $lang) {
                    $nameKey = "name_{$lang}";
                    $nameValue = $request->input($nameKey, '');
                    if (!empty($nameValue)) {
                        if ($lang === 'ar') {
                            $slugValue = preg_replace('/\s+/u', '-', trim($nameValue));
                            $slugValue = preg_replace('/[^\p{L}\p{N}_-]+/u', '', $slugValue);
                        } else {
                            $slugValue = \Illuminate\Support\Str::slug($nameValue);
                        }
                        // Normalize
                        $slugValue = preg_replace('/\s+/u', '-', trim($slugValue));
                        $slugValue = preg_replace('/-+/u', '-', $slugValue);
                        $slugValue = trim($slugValue, '-');
                        
                        if (!empty($slugValue)) {
                            $fieldTranslations[$lang] = $slugValue;
                            $hasNonEmptyValue = true;
                            // Use this slug for all languages if they don't have one
                            foreach ($languages as $otherLang) {
                                if (!isset($fieldTranslations[$otherLang]) || empty($fieldTranslations[$otherLang])) {
                                    $fieldTranslations[$otherLang] = $slugValue;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            if (!empty($fieldTranslations)) {
                $jsonData[$field] = $hasNonEmptyValue ? $fieldTranslations : null;
            } elseif ($field === 'slug') {
                // If slug is still empty, create a default slug based on timestamp
                $defaultSlug = 'service-' . time();
                foreach ($languages as $lang) {
                    $fieldTranslations[$lang] = $defaultSlug;
                }
                $jsonData[$field] = $fieldTranslations;
            }
        }

        return $jsonData;
    }

    /**
     * Create model with JSON translations
     */
    public static function createWithTranslations($modelClass, $data, $request, $translationFields)
    {
        // Don't start a new transaction if we're already in one
        $shouldCommit = !DB::transactionLevel();
        
        if ($shouldCommit) {
            DB::beginTransaction();
        }

        try {
            // Prepare JSON translations
            $jsonTranslations = self::prepareJsonData($request, $translationFields);

            // Merge with main data
            $allData = array_merge($data, $jsonTranslations);

            // Create the model
            $model = $modelClass::create($allData);

            if ($shouldCommit) {
                DB::commit();
            }
            return $model;
        } catch (\Exception $e) {
            if ($shouldCommit) {
                DB::rollBack();
            }
            throw $e;
        }
    }

    /**
     * Update model with JSON translations
     */
    public static function updateWithTranslations($model, $data, $request, $translationFields)
    {
        // Don't start a new transaction if we're already in one
        $shouldCommit = !DB::transactionLevel();
        
        if ($shouldCommit) {
            DB::beginTransaction();
        }

        try {
            // Prepare JSON translations
            $jsonTranslations = self::prepareJsonData($request, $translationFields);

            // Merge with main data
            $allData = array_merge($data, $jsonTranslations);

            // Update the model
            $model->update($allData);

            if ($shouldCommit) {
                DB::commit();
            }
            return $model;
        } catch (\Exception $e) {
            if ($shouldCommit) {
                DB::rollBack();
            }
            throw $e;
        }
    }

    /**
     * Get supported languages
     */
    public static function getSupportedLanguages()
    {
        return config('app.form_languages', ['en', 'ar', 'fr']);
    }

    /**
     * Get translation fields for a model type
     */
    public static function getTranslationFields($modelType)
    {
        $fields = [
            'blog' => ['name', 'short_desc', 'long_desc', 'meta_title', 'meta_desc', 'slug'],
            'product' => ['name', 'short_desc', 'long_desc', 'meta_title', 'meta_desc', 'slug', 'clients', 'location'],
            'service' => ['name', 'short_desc', 'long_desc', 'meta_title', 'meta_desc', 'slug'],
            'project' => ['name', 'short_desc', 'long_desc', 'meta_title', 'meta_desc', 'slug'],
            'category' => ['name', 'short_desc', 'long_desc', 'meta_title', 'meta_desc', 'slug'],
            'faq' => ['question', 'answer'],
            'page' => ['title', 'content', 'slug'],
            'menu' => ['name'],
            'benefit' => ['name', 'description'],
            'testimonial' => ['name', 'content', 'position'],
            'slider' => ['title', 'title2', 'text'],
            'section' => ['name', 'title', 'short_desc', 'long_desc'],
            'about_struct' => ['name', 'long_desc'],
            'plan' => ['name', 'description'],
            'job_position' => ['title', 'description', 'requirements'],
            'phone' => ['name', 'description'],
        ];

        return $fields[$modelType] ?? [];
    }

    /**
     * Generate slugs for all languages
     */
    public static function generateSlugs($translations, $field = 'name')
    {
        $slugs = [];

        foreach ($translations as $lang => $data) {
            if (isset($data[$field])) {
                if ($lang === 'ar') {
                    $slugs[$lang] = preg_replace('/\s+/u', '-', trim($data[$field]));
                    $slugs[$lang] = preg_replace('/[^\p{L}\p{N}_-]+/u', '', $slugs[$lang]);
                    // Remove multiple consecutive hyphens
                    $slugs[$lang] = preg_replace('/-+/u', '-', $slugs[$lang]);
                    // Remove leading/trailing hyphens
                    $slugs[$lang] = trim($slugs[$lang], '-');
                } else {
                    $slugs[$lang] = \Illuminate\Support\Str::slug($data[$field]);
                }
            }
        }

        return $slugs;
    }
}
