<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Page;
use App\Models\SiteDetails;
use App\Models\HomePageDetails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class PageController extends Controller
{
    // Show the form for a specific page (About Us, Privacy Policy, Terms)
    public function showForm($title)
    {
        
        $title = ucfirst(str_replace('-', ' ', $title));
        // Get page by title or create a new record if not found
        $page = Page::where('title', ucfirst(str_replace('-', ' ', $title)))->first();

        if (!$page) {
            // Create a new record if it doesn't exist
            $page = new Page();
            $page->title = ucfirst(str_replace('-', ' ', $title));
            $page->content = ''; // Initialize with empty content
            $page->status = '0'; 
            $page->save();
        }

        // Return the view with the page content
        return view('admin.cms_pages.form', compact('page', 'title'));
    }

    // Save the page content
    public function savePage(Request $request, $title)
    {
        // Validate the form data
        $request->validate([
            'title_ar' => 'required|string',
            'content_ar' => 'required|string',
            'content' => 'required|string',
            'status' => 'required|in:1,0',
        ]);

        // Get the page or create a new one
        $page = Page::where('title', ucfirst(str_replace('-', ' ', $title)))->first();

        // If page doesn't exist, create a new one
        if (!$page) {
            $page = new Page();
            $page->title = ucfirst(str_replace('-', ' ', $title));
        }

        // Update content and status
        $page->title_ar = $request->title_ar;
        $page->content = $request->content;
        $page->content_ar = $request->content_ar;
        $page->status = $request->status;
        $page->save();

        // Redirect back with success message
        return redirect()->route('admin.pages.show', ['page' => $title])->with('success', 'Page content saved successfully.');
        
    }

    
    public function site_details()
    {
        $page_heading = "Site Details";
        $page  = SiteDetails::all();
        if($page == null){
            $page  = new SiteDetails();
        }
        
        return view('admin.site.site',compact('page_heading','page'));
    }

    public function site_setting_store(Request $request)
    {
             foreach ($request->all() as $key => $value) {

                if ($key == '_token' ) {
                    continue;
                }


                $contact = SiteDetails::where('key', $key)->first();


                if ($contact) {
                    if ($request->hasFile($key)) {
                         $file = $request->file($key);
                        $imageName = time() . '_' . $file->getClientOriginalName();
                        $filePath = 'uploads/site/' . $imageName;
                        $file->move(public_path('uploads/site/'), $imageName);
                        $imagePublicUrl = asset($filePath);
                        
                        $contact->value = $imagePublicUrl;

                    } else {

                        $contact->value = $value;
                    }
                    $contact->save();
                } else {
                            // Create a new record
                    $contact = new SiteDetails();
                    $contact->key = $key;
                    if ($request->hasFile($key)) {

                        $file = $request->file($key);
                        $imageName = time() . '_' . $file->getClientOriginalName();
                        $filePath = 'uploads/site/' . $imageName;
                        $file->move(public_path('uploads/site/'), $imageName);
                        $imagePublicUrl = asset($filePath);
                        $contact->meta_value = $imagePublicUrl;
                    } else {

                        $contact->value = $value;
                    }
                    $contact->save();
                }
            }

            $message = 'Site settings have been updated.';
            return redirect()->back()->with('success', $message);
    }




    public function home_details()
    {
        $page_heading = "Site Details";
        $page  = HomePageDetails::all();
        if($page == null){
            $page  = new HomePageDetails();
        }
        
        return view('admin.site.home',compact('page_heading','page'));
    }

    public function home_page_details_store(Request $request)
    {
             foreach ($request->all() as $key => $value) {

                if ($key == '_token' ) {
                    continue;
                }


                $contact = HomePageDetails::where('key', $key)->first();


                if ($contact) {
                    if ($request->hasFile($key)) {
                         $file = $request->file($key);
                        $imageName = time() . '_' . $file->getClientOriginalName();
                        $filePath = 'uploads/site/' . $imageName;
                        $file->move(public_path('uploads/site/'), $imageName);
                        $imagePublicUrl = asset($filePath);
                        
                        $contact->value = $imagePublicUrl;

                    } else {

                        $contact->value = $value;
                    }
                    $contact->save();
                } else {
                            // Create a new record
                    $contact = new HomePageDetails();
                    $contact->key = $key;
                    if ($request->hasFile($key)) {

                        $file = $request->file($key);
                        $imageName = time() . '_' . $file->getClientOriginalName();
                        $filePath = 'uploads/site/' . $imageName;
                        $file->move(public_path('uploads/site/'), $imageName);
                        $imagePublicUrl = asset($filePath);
                        $contact->meta_value = $imagePublicUrl;
                    } else {

                        $contact->value = $value;
                    }
                    $contact->save();
                }
            }

            $message = 'Home Page details have been updated.';
            return redirect()->back()->with('success', $message);
    }
}
