<?php

namespace App\Services\APIs\RestaurantService;

use App\Models\Aboutus;
use App\Models\MainCategory;
use App\Models\ManageContactus;
use App\Models\NewsArticle;
use App\Models\PrivacyPolicy;
use App\Models\Faq;
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Exception;
use Throwable;

class RestCMSService
{
    public function RestGetFaq()
    {
        try {
            $data['customer'] = Faq::select('id', 'question', 'answers')
                ->where([['is_active', '1'], ['faq_category_id', '1']])
                ->get()
                ->toArray();

            $data['restaurant'] = Faq::select('id', 'question', 'answers')
                ->where([['is_active', '1'], ['faq_category_id', '2']])
                ->get()
                ->toArray();

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('Faq Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function RestAboutUs()
    {
        try {
            $data['customer'] = Aboutus::select('id', 'title', 'thumbnail_image', 'description', 'category_xid')
                ->where('category_xid', '1')
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();

            $data['restaurant'] = Aboutus::select('id', 'title', 'thumbnail_image', 'description', 'category_xid')
                ->where('category_xid', '2')
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();
            foreach ($data['customer'] as $k => $val) {
                $data['customer'][$k]['thumbnail_image'] = ListingImageUrl('about_images', $val['thumbnail_image']);
            }

            foreach ($data['restaurant'] as $k => $val) {
                $data['restaurant'][$k]['thumbnail_image'] = ListingImageUrl('about_images', $val['thumbnail_image']);
            }

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('About us Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function RestPrivacyPolicy()
    {
        try {
            $data['customer'] = PrivacyPolicy::select('id', 'description')
                ->where('category_xid', '1')
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();

            $data['restaurant'] = PrivacyPolicy::select('id', 'description')
                ->where('category_xid', '2')
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();
            return $data;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('Privacy policy Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function RestNewsArticles()
    {
        try {
            $data['customer'] = NewsArticle::select('id', 'news_articles_category_xid','name', 'description', 'thumbnail_image', 'image',)
                ->where([['is_active', '0'], ['news_articles_category_xid', '1']])
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();

            $data['restaurant'] = NewsArticle::select('id', 'news_articles_category_xid','name', 'description', 'thumbnail_image', 'image')
                ->where([['is_active', '0'], ['news_articles_category_xid', '2']])
                ->get()
                ->map(function ($item) {
                    $item['description'] = strip_tags($item['description']);
                    return $item;
                })
                ->toArray();

            //thumbnail_image for 'customer' data
            foreach ($data['customer'] as $k => $val) {
                $data['customer'][$k]['thumbnail_image'] = ListingImageUrl('news_article_thumb', $val['thumbnail_image']);
                $data['customer'][$k]['image'] = ListingImageUrl('news_article', $val['image']);
            }

            //thumbnail_image for 'restaurant' data
            foreach ($data['restaurant'] as $k => $val) {
                $data['restaurant'][$k]['thumbnail_image'] = ListingImageUrl('news_article_thumb', $val['thumbnail_image']);
                $data['restaurant'][$k]['image'] = ListingImageUrl('news_article', $val['image']);
            }

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('News and articles Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function RestContactUs($request)
    {
        try {
            DB::beginTransaction();
            //create user_data
            $user_data = IamPrincipal::where('id', $request['iam_principal_id'])->first();
            if ($user_data) {
                // Create a new instance of ManageContactus model
                $contact = new ManageContactus();
                $contact->principal_xid = $user_data->id;
                $contact->name = $request->name;
                $contact->email = $request->email;
                $contact->message = $request->message;
                // Save the contact data
                $contact->save();

                DB::commit();

                //response data
                Log::info('Contact form data Created successfully');
                return jsonResponseWithSuccessMessageApi(__('success.save_data'), [], 201);
            } else {
                Log::error('Contact not found in addVendorContactForm.');
                return jsonResponseWithErrorMessageApi(__('auth.validation_failed'), 403);
            }
        } catch (Throwable $ex) {
            DB::rollBack();
            Log::error('Contact API failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }
}
