<?php

namespace App\Http\Controllers\Website;

use App\Helper\SocialMediaHelper;
use App\Helper\StringHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\Website\SaveCareerApplicationRequest;
use App\Http\Requests\Website\StoreContactUsRequest;
use App\Models\Benefit;
use App\Models\Blog;
use App\Models\Dashboard\AboutStruct;
use App\Models\Dashboard\AboutUs;
use App\Models\Dashboard\Domain;
use App\Models\Faq;
use App\Models\GalleryVideo;
use App\Models\JobPosition;
use App\Models\Phone;
use App\Models\Product;
use App\Models\Project;
use App\Models\Service;
use App\Models\SiteAddress;
use App\Models\Statistic;
use App\Services\Dashboard\SaveApplicationService;
use App\Services\Website\ContactUsService;
use App\Services\Website\StoreContactUsService;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\Section;
use App\Models\Album;
use App\Models\Team;

class WebsiteController extends Controller
{
    public function about()
    {
        $data['about'] = AboutUs::first();
        $data['about_structs'] = AboutStruct::active()->get();
        $data['faqs'] = Faq::active()->general()->get();
        $data['statistics'] = Statistic::active()->take(4)->get();
        $data['teams'] = Team::active()->home()->orderBy('order')->get();
        $data['sections'] = Section::where('status', 1)->get()->keyBy('key');
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.about', $data);
    }

    public function whyUs()
    {
        $data['about_structs'] = AboutStruct::active()->orderBy('order')->get();
        $data['sections'] = \App\Models\Section::where('key', 'whyus')->get();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.why-us', $data);
    }

    public function services()
    {
        $data['services'] = Service::active()->whereNull('parent_id')->orderBy('order')->get();
        $data['sections'] = Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();

        return view('Website.services', $data);
    }

    public function serviceDetails($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded characters and fix mojibake
        $decodedSlug = urldecode($slug);

        // Fix Arabic mojibake encoding issues
        $fixedSlug = StringHelper::fixArabicMojibake($decodedSlug);

        // Try to find service using JSON_EXTRACT for current locale
        $service = Service::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$fixedSlug])->first();

        // If not found, try with original decoded slug
        if (!$service) {
            $service = Service::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$decodedSlug])->first();
        }

        // If not found, try to find by checking all services manually (fallback for double JSON issue)
        if (!$service) {
            $services = Service::all();
            foreach ($services as $s) {
                $serviceSlug = $s->getTranslation('slug', $locale);
                if ($serviceSlug === $fixedSlug || $serviceSlug === $decodedSlug) {
                    $service = $s;
                    break;
                }
            }
        }

        // If still not found, try to find by checking all languages
        if (!$service) {
            $services = Service::all();
            foreach ($services as $s) {
                foreach (['en', 'ar', 'fr'] as $lang) {
                    $serviceSlug = $s->getTranslation('slug', $lang);
                    if ($serviceSlug === $fixedSlug || $serviceSlug === $decodedSlug) {
                        $service = $s;
                        break 2;
                    }
                }
            }
        }

        if (!$service) {
            abort(404);
        }

        // The middleware will handle slug redirections, so we just display the service

        // Get other services (excluding current service)
        $otherServices = Service::active()
            ->where('id', '!=', $service->id)
            ->whereNull('parent_id')
            ->orderBy('order')
            ->limit(5)
            ->get();

        // Get sections
        $sections = Section::all();

        // #call# placeholder will be replaced automatically by the model accessor
        $socialMediaLinks = $this->transformSocialMediaLinks();
        return view('Website.service-details', compact('service', 'otherServices', 'sections', 'socialMediaLinks'));
    }

    public function categories()
    {
        $data['categories'] = Category::active()->get();
        $data['services'] = Service::active()->get();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.categories', $data);
    }

    public function categoryDetails($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded Arabic characters
        $decodedSlug = urldecode($slug);

        // Try to find category using JSON_EXTRACT for current locale
        $category = Category::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$decodedSlug])->first();

        // If not found, try to find by checking all categories manually (fallback for double JSON issue)
        if (!$category) {
            $categories = Category::all();
            foreach ($categories as $c) {
                $categorySlug = $c->getTranslation('slug', $locale);
                if ($categorySlug === $decodedSlug) {
                    $category = $c;
                    break;
                }
            }
        }

        // If still not found, try to find by checking all languages
        if (!$category) {
            $categories = Category::all();
            foreach ($categories as $c) {
                foreach (['en', 'ar', 'fr'] as $lang) {
                    $categorySlug = $c->getTranslation('slug', $lang);
                    if ($categorySlug === $decodedSlug) {
                        $category = $c;
                        break 2;
                    }
                }
            }
        }

        if (!$category) {
            abort(404);
        }

        // #call# placeholder will be replaced automatically by the model accessor
        $socialMediaLinks = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.category-details', compact('category', 'socialMediaLinks'));
    }

    public function showContactUs()
    {
        $data['site_addresses'] = SiteAddress::active()->orderBy('order')->get();
        $data['phones'] = Phone::active()->orderBy('order')->get();
        $data['FormServices'] = Service::active()->orderBy('order')->get();

        // Get sections
        $sections = Section::all();

        $data['sections'] = $sections;
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.contact-us', $data);
    }

    public function saveContactUs(StoreContactUsRequest $request)
    {
        try {

            $data = $request->validated();

            $response = (new ContactUsService)->store($data);

            if (!$response) {
                return redirect()->back()->with(['error' => __('website.failed_to_send_message')]);
            }

            return redirect()->route('website.thank-you');
        } catch (\Exception $e) {

            return redirect()->back()->with(['error' => __('website.something wrong pls try letter')]);
        }
    }

    public function thankYou()
    {
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.thank-you', $data);
    }

    public function products()
    {
        $data['products'] = Product::active()->whereNull('parent_id')->get();
        $data['sections'] = Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();

        return view('Website.products', $data);
    }

    /**
     * Generate slug variations (with spaces, with hyphens, normalized)
     * This helps find products even if slug in DB has spaces but URL has hyphens
     */
    private function generateSlugVariations($slug)
    {
        $variations = [];
        
        // Original slug
        $variations[] = $slug;
        
        // Replace hyphens with spaces
        $variations[] = str_replace('-', ' ', $slug);
        
        // Replace spaces with hyphens
        $variations[] = preg_replace('/\s+/u', '-', trim($slug));
        
        // Normalized version (spaces to hyphens, remove multiple hyphens)
        $normalized = preg_replace('/\s+/u', '-', trim($slug));
        $normalized = preg_replace('/-+/u', '-', $normalized);
        $normalized = trim($normalized, '-');
        if ($normalized !== $slug && !in_array($normalized, $variations)) {
            $variations[] = $normalized;
        }
        
        // Remove duplicates
        return array_unique($variations);
    }

    public function productSubProducts($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded Arabic characters
        $decodedSlug = urldecode($slug);

        // Generate slug variations (with spaces, with hyphens, normalized)
        $slugVariations = $this->generateSlugVariations($decodedSlug);

        // Try to find product using JSON_EXTRACT with all variations
        $product = null;
        foreach ($slugVariations as $variation) {
            $product = Product::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$variation])->first();
            if ($product) {
                break;
            }
        }

        // If not found, try to find by checking all products manually
        if (!$product) {
            $products = Product::all();
            foreach ($products as $p) {
                $productSlug = $p->getTranslation('slug', $locale);
                // Check against all variations
                foreach ($slugVariations as $variation) {
                    if ($productSlug === $variation) {
                        $product = $p;
                        break 2;
                    }
                }
            }
        }

        if (!$product) {
            abort(404);
        }

        // Get sub products (children)
        $data['subProducts'] = $product->children()->where('status', 1)->get();
        $data['parentProduct'] = $product;
        $data['sections'] = Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();

        return view('Website.product-sub-products', $data);
    }

    public function productDetails($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded Arabic characters
        $decodedSlug = urldecode($slug);

        // Generate slug variations (with spaces, with hyphens, normalized)
        $slugVariations = $this->generateSlugVariations($decodedSlug);

        // Try to find product using JSON_EXTRACT with all variations
        $product = null;
        foreach ($slugVariations as $variation) {
            $product = Product::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$variation])->first();
            if ($product) {
                break;
            }
        }

        // If not found, try to find by checking all products manually (fallback for double JSON issue)
        if (!$product) {
            $products = Product::all();
            foreach ($products as $p) {
                $productSlug = $p->getTranslation('slug', $locale);
                // Check against all variations
                foreach ($slugVariations as $variation) {
                    if ($productSlug === $variation) {
                        $product = $p;
                        break 2;
                    }
                }
            }
        }

        if (!$product) {
            abort(404);
        }

        // Load product images relationship
        $product->load('images');

        // #call# placeholder will be replaced automatically by the model accessor
        $socialMediaLinks = SocialMediaHelper::getSocialMediaLinks();
        // Get only partners associated with this product
        $brands = $product->parteners()->where('parteners.status', 1)->orderBy('parteners.order')->get();
        $sections = Section::all();

        // Get related products (same category or same service, excluding current product)
        $relatedProducts = Product::active()
            ->where('id', '!=', $product->id)
            ->where(function ($query) use ($product) {
                if ($product->category_id) {
                    $query->where('category_id', $product->category_id);
                }
                if ($product->service_id) {
                    $query->orWhere('service_id', $product->service_id);
                }
            })
            ->orderBy('order', 'asc')
            ->take(6)
            ->get();

        // If no related products found by category/service, get any active products
        if ($relatedProducts->isEmpty()) {
            $relatedProducts = Product::active()
                ->where('id', '!=', $product->id)
                ->orderBy('order', 'asc')
                ->take(6)
                ->get();
        }

        return view('Website.product-details', compact('product', 'socialMediaLinks', 'brands', 'sections', 'relatedProducts'));
    }

    public function downloadProductPdf($product)
    {
        // Handle both slug and ID
        $locale = app()->getLocale();
        
        if (is_numeric($product)) {
            $productModel = Product::find($product);
        } else {
            $decodedSlug = urldecode($product);
            $productModel = Product::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$decodedSlug])->first();
            
            if (!$productModel) {
                $products = Product::all();
                foreach ($products as $p) {
                    $productSlug = $p->getTranslation('slug', $locale);
                    if ($productSlug === $decodedSlug) {
                        $productModel = $p;
                        break;
                    }
                }
            }
        }

        if (!$productModel || !$productModel->hasPdf()) {
            abort(404);
        }

        $filePath = storage_path('app/public/products/' . $productModel->pdf);
        
        if (!file_exists($filePath)) {
            abort(404);
        }

        return response()->download($filePath, $productModel->name . '.pdf');
    }

    public function projects()
    {
        $data['projects'] = Project::active()->get();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.projects', $data);
    }
    public function projectDetails($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded Arabic characters
        $decodedSlug = urldecode($slug);

        // Try to find project using JSON_EXTRACT
        $project = Project::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$decodedSlug])->first();

        // If not found, try to find by checking all projects manually (fallback for double JSON issue)
        if (!$project) {
            $projects = Project::all();
            foreach ($projects as $p) {
                $projectSlug = $p->getTranslation('slug', $locale);
                if ($projectSlug === $decodedSlug) {
                    $project = $p;
                    break;
                }
            }
        }

        if (!$project) {
            abort(404);
        }

        // Load project images relationship
        $project->load('images', 'category');

        // #call# placeholder will be replaced automatically by the model accessor
        $socialMediaLinks = SocialMediaHelper::getSocialMediaLinks();
        $sections = Section::where('status', 1)->get()->keyBy('key');
        
        return view('Website.project-details', compact('project', 'socialMediaLinks', 'sections'));
    }

    public function blogs()
    {
        $data['blogs'] = Blog::active()->orderBy('created_at', 'desc')->paginate(9);
        $data['sections'] = \App\Models\Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.blogs', $data);
    }
    public function blogDetails($slug)
    {
        $locale = app()->getLocale();

        // Decode URL-encoded Arabic characters
        $decodedSlug = urldecode($slug);

        // Try to find blog using JSON_EXTRACT for current locale
        $blog = Blog::whereRaw("JSON_EXTRACT(slug, '$.{$locale}') = ?", [$decodedSlug])->first();

        // If not found, try to find by checking all blogs manually (fallback for double JSON issue)
        if (!$blog) {
            $blogs = Blog::all();
            foreach ($blogs as $b) {
                $blogSlug = $b->getTranslation('slug', $locale);
                if ($blogSlug === $decodedSlug) {
                    $blog = $b;
                    break;
                }
            }
        }

        // If still not found, try to find by checking all languages
        if (!$blog) {
            $blogs = Blog::all();
            foreach ($blogs as $b) {
                foreach (['en', 'ar', 'fr'] as $lang) {
                    $blogSlug = $b->getTranslation('slug', $lang);
                    if ($blogSlug === $decodedSlug) {
                        $blog = $b;
                        break 2;
                    }
                }
            }
        }

        if (!$blog) {
            abort(404);
        }

        // #call# placeholder will be replaced automatically by the model accessor

        // Get related blogs (excluding current blog)
        $blogs = Blog::active()->where('id', '!=', $blog->id)->limit(5)->get();
        $socialMediaLinks = $this->transformSocialMediaLinks();
        $sections = Section::all();

        return view('Website.blog-details', compact('blog', 'blogs', 'socialMediaLinks', 'sections'));
    }

    public function careers()
    {
        $data['gallery_videos'] = GalleryVideo::active()->get();
        $data['job_positions'] = JobPosition::active()->get();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.careers', $data);
    }

    public function saveApplication(SaveCareerApplicationRequest $request)
    {
        try {

            $response = (new SaveApplicationService)->saveApplication($request);

            if (!$response) {
                return redirect()->back()->with(['error' => __('website.failed_to_send_message')]);
            }

            return redirect()->back()->with(['success' => __('website.thanks_message')]);
        } catch (\Exception $e) {

            return redirect()->back()->with(['error' => __('website.something wrong pls try letter')]);
        }
    }

    public function galleryPhotos()
    {
        // Get gallery images from Gallery model
        $galleries = \App\Models\Gallery::with('images')->active()->orderBy('order')->get();
        
        // Collect all gallery images
        $galleryImages = collect();
        foreach ($galleries as $gallery) {
            if ($gallery->images && $gallery->images->isNotEmpty()) {
                foreach ($gallery->images as $image) {
                    $galleryImages->push((object)[
                        'image_path' => $image->image_path,
                        'image_url' => $image->image_url, // Alias for compatibility
                        'title' => $gallery->title ?? '',
                    ]);
                }
            }
        }
        
        $data['gallery_images'] = $galleryImages;
        $data['sections'] = \App\Models\Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.gallery-photos', $data);
    }

    public function galleryVideos()
    {
        $data['gallery_videos'] = GalleryVideo::active()->get();
        $data['sections'] = \App\Models\Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.gallery-videos', $data);
    }

    public function faqs()
    {
        $data['faqs'] = \App\Models\Faq::active()->orderBy('order')->get();
        $data['sections'] = \App\Models\Section::all();
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.faqs', $data);
    }

    private function transformSocialMediaLinks(): array
    {
        $socialLinks = SocialMediaHelper::getSocialMediaLinks();
        $transformed = [];

        $iconMap = [
            'facebook' => 'fab fa-facebook-f',
            'twitter' => 'fab fa-twitter',
            'instagram' => 'fab fa-instagram',
            'youtube' => 'fab fa-youtube',
            'linkedin' => 'fab fa-linkedin-in',
            'tiktok' => 'fab fa-tiktok',
            'snapchat' => 'fab fa-snapchat-ghost',
            'pinterest' => 'fab fa-pinterest',
            'telegram' => 'fab fa-telegram-plane',
        ];

        $titleMap = [
            'facebook' => 'Facebook',
            'twitter' => 'Twitter',
            'instagram' => 'Instagram',
            'youtube' => 'YouTube',
            'linkedin' => 'LinkedIn',
            'tiktok' => 'TikTok',
            'snapchat' => 'Snapchat',
            'pinterest' => 'Pinterest',
            'telegram' => 'Telegram',
        ];

        foreach ($socialLinks as $platform => $url) {
            if (!empty($url) && $url !== '#') {
                $transformed[] = (object)[
                    'url' => $url,
                    'icon' => $iconMap[$platform] ?? 'fab fa-link',
                    'title' => $titleMap[$platform] ?? ucfirst($platform),
                ];
            }
        }

        return $transformed;
    }

    public function team()
    {
        $data['teams'] = Team::active()->orderBy('order')->get();
        $data['sections'] = Section::where('status', 1)->get()->keyBy('key');
        $data['socialMediaLinks'] = SocialMediaHelper::getSocialMediaLinks();
        return view('Website.team', $data);
    }
}
