<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Page;
use Str;
use App\Models\Translation;
use Stichoza\GoogleTranslate\GoogleTranslate;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $page_data = [
            'title' => 'Extra Pages',
            'slug' => 'All Pages'
        ];
        $pages= Page::select('id','title')->get();
        // dd($pages);
        return view('admin.pages.extra-pages.index',get_defined_vars());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $page_data = [
            'title' => 'Extra Pages',
            'slug' => 'Add New Page'
        ];
        return view('admin.pages.extra-pages.create',get_defined_vars());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        
         $source = $request->input('source', 'en'); // default English
        $targets = explode(',', $request->input('targets', 'pl'));
        $page = new Page();
        $page->title = $request->title;
        $page->slug = Str::slug($request->title);
        $page->details = $request->details;
        $page->meta_title = $request->meta_title;
        $page->meta_description = $request->meta_description;
        if($page->save())
        {
            foreach ($targets as $target) {
                    $this->info("🔤 Translating to {$target}...");
                    
                    $translator = new GoogleTranslate($target, $source);
                    $successCount = 0;

                    // Translate Blogs (including description)
                    $this->info("   📝 Translating blogs...");
                    $page = Page::find($page->id);
                    
                        try {
                    usleep(500000); // Delay

                    $translatedName = $translator->translate($page->title);
                    Translation::setTranslated($target, 'pages', $page->id . '_title', $translatedName);

                    if($page->details){
                        $translatedDetails = $translator->translate($page->details);
                        Translation::setTranslated($target, 'pages', $page->id . '_details', $translatedDetails);
                    }

                    $this->line("     ✅ Page: {$page->title} → {$translatedName}");
                    $successCount++;
                    
                } catch (\Exception $e) {
                    $this->error("     ❌ Failed: {$page->title}");
                }
                    
                }
            return redirect()->route('pages.index')->withSuccess('Page Added Successfully!');
        }
        else{
            return back()->withError('Something went wrong!');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $page_data = [
            'title' => 'Extra Pages',
            'slug' => 'Edit Page'
        ];
        $page = Page::find($id);
        return view('admin.pages.extra-pages.edit',get_defined_vars());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
         $source = $request->input('source', 'en'); // default English
        $targets = explode(',', $request->input('targets', 'pl'));
        // dd($request->all());
        $page = Page::find($id);
        $page->title = $request->title;
        $page->slug = Str::slug($request->title);
        $page->details = $request->details;
        $page->meta_title = $request->meta_title;
        $page->meta_description = $request->meta_description;
        if($page->update())
        {
            foreach ($targets as $target) {
                    $this->info("🔤 Translating to {$target}...");
                    
                    $translator = new GoogleTranslate($target, $source);
                    $successCount = 0;

                    // Translate Blogs (including description)
                    $this->info("   📝 Translating blogs...");
                    $page = Page::find($page->id);
                    
                        try {
                    usleep(500000); // Delay

                    $translatedName = $translator->translate($page->title);
                    Translation::setTranslated($target, 'pages', $page->id . '_title', $translatedName);

                    if($page->details){
                        $translatedDetails = $translator->translate($page->details);
                        Translation::setTranslated($target, 'pages', $page->id . '_details', $translatedDetails);
                    }

                    $this->line("     ✅ Page: {$page->title} → {$translatedName}");
                    $successCount++;
                    
                } catch (\Exception $e) {
                    $this->error("     ❌ Failed: {$page->title}");
                }
                    
                }
            return redirect()->route('pages.index')->withSuccess('Page updated Successfully!');
        }
        else{
            return back()->withError('Something went wrong!');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $page = Page::find($id);
        if($page->delete())
        {
            return redirect()->route('pages.index')->withSuccess('Page deleted Successfully!');
        }
        else{
            return back()->withError('Something went wrong!');
        }

    }
}
