<?php

namespace App\Services\APIs\CustomerAPIs;

use App\Models\Aboutus;
use Illuminate\Support\Facades\Log;

use App\Models\Faq;
use App\Models\NewsArticle;
use App\Models\PrivacyPolicy;
use App\Models\Termsandconditions;
use Exception;

class CMSApiServices
{
    public function getfaq()
    {
        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) {
            Log::error('Faq Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function getAboutUs()
    {
        try {
            $data['customer'] = Aboutus::select('id', 'thumbnail_image', 'description')
                ->where('category_xid', '1')
                ->get()
                ->toArray();

            $data['restaurant'] = Aboutus::select('id', 'thumbnail_image', 'description')
                ->where('category_xid', '2')
                ->get()
                ->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) {
            Log::error('About us Get service failed : ' . $ex->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    public function getPrivacyPolicy()
    {
        try {
            $data['customer'] = PrivacyPolicy::select('id', 'description')
                ->where('category_xid', '1')
                ->get()

                ->toArray();


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

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

            $data['restaurant'] = NewsArticle::select('id', 'name', 'description', 'thumbnail_image', 'image')
                ->where([['is_active', '1'], ['news_articles_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('news_article_thumb', $val['thumbnail_image']);
                $data['customer'][$k]['image'] = ListingImageUrl('news_article_image', $val['image']);
            }

            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_image', $val['image']);
            }

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

    public function getTermsConditon()
    {
        try {
            $data['customer'] = Termsandconditions::select('id', 'message')
                ->where('category_xid', '1')
                ->get()
                ->toArray();

            $data['restaurant'] = Termsandconditions::select('id', 'message')
                ->where('category_xid', '2')
                ->get()
                ->toArray();

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