<?php

namespace App\Http\Controllers;

use App\Http\Requests\ContactUsRequest;
use App\Models\AiSupportFeatureItem;
use App\Models\Blog;
use App\Models\ContactUs;
use App\Models\CreditConfiguration;
use App\Models\FrontendSetting;
use App\Models\MainFeatureItem;
use App\Models\Package;
use App\Models\PlaygroundFeatureItem;
use App\Models\PrivacyPolicy;
use App\Models\TermsCondition;
use Illuminate\Support\Facades\Log;

class FrontendController extends Controller
{
    public function index()
    {
        if (getOption('frontend_status') !== FRONTEND_STATUS_ENABLE) {
            return redirect()->route('login');
        }
        $data['pageTitle'] = __('Home');
        $data['frontendData'] = FrontendSetting::first();
        $data['mainFeatureItem'] = MainFeatureItem::where('main_feature_item_status', MAIN_FEATURE_ITEM_STATUS_ENABLE)->get();
        $data['playgroundData'] = PlaygroundFeatureItem::first();
        $data['packages'] = Package::where('status', PACKAGE_STATUS_ENABLE)->where('is_trail', IS_TRAIL_NO)->get();
        $data['aiSupportData'] = AiSupportFeatureItem::where('ai_support_feature_item_status', AI_SUPPORT_FEATURE_ITEM_STATUS_ENABLE)->get();
        $data['blogs'] = Blog::with('user')->get();
        $data['creditConfigData'] = CreditConfiguration::first();
        // return view('home', $data);
        return redirect()->route('login');
    }

    public function blogDetails($title, $id)
    {
        $data['blogDetailsData'] = Blog::with('user')->find(decrypt($id));
        $data['blogs'] = Blog::with('user')->latest()->get();
        $data['pageTitle'] = __('Blog Details');
        return view('admin.frontend-settings.blog-details', $data);
    }

    public function termsPage()
    {
        $data['pageTitle'] = __('Terms & Condition');
        $data['termsConditionData'] = TermsCondition::first();
        return view('terms-and-condition', $data);
    }

    public function privacyPage()
    {
        $data['pageTitle'] = __('Privacy & Policy');
        $data['privacyData'] = PrivacyPolicy::first();
        return view('privacy-and-policy', $data);
    }

    public function dataDeletionPolicy()
    {
        $data['pageTitle'] = __('Data Deletion Policy');
        return view('deletion-policy', $data);
    }

    public function contactStore(ContactUsRequest $request)
    {
        try {
            $contactObj = new ContactUs();
            $contactObj->name = $request->name;
            $contactObj->email = $request->email;
            $contactObj->message = $request->message;
            $contactObj->save();
            return redirect()->back()->with('success', 'Message Sent Successfully');
        } catch (\Exception $exception) {
            Log::error($exception->getMessage());
            return redirect()->back()->with('error', 'Something Went Wrong');
        }
    }
}