<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Seo;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;

class SeoController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRulesOne = [
            'home_meta_title' => 'required',
            'home_meta_description' => 'required',
        ];
        $this->storeRulesTwo = [
            'about_us_meta_title' => 'required',
            'about_us_meta_description' => 'required',
        ];
        $this->storeRulesThree = [
            'facilities_meta_title' => 'required',
            'facilities_meta_description' => 'required',
        ];
        $this->storeRulesFour = [
            'contact_us_meta_title' => 'required',
            'contact_us_meta_description' => 'required',
        ];
    }

    public function getSeoDetails($type) {
        Log::info("type==== ". $type);
        if( $type == 'home') {
            return $this->response->jsonResponse(false, "Home Page SEO Details Listed Successfully", Seo::select('seo_id', 'home_meta_title', 'home_meta_description')->get(), 200);
        } else if( $type == 'aboutus'){
            return $this->response->jsonResponse(false, "About Us Page SEO Details Listed Successfully", Seo::select('seo_id', 'about_us_meta_title', 'about_us_meta_description')->get(), 200);
        } else if( $type == 'facilities'){
            return $this->response->jsonResponse(false, "Facilities SEO Details Listed Successfully", Seo::select('seo_id', 'facilities_meta_title', 'facilities_meta_description')->get(), 200);
        } else if( $type == 'contactus'){
            return $this->response->jsonResponse(false, "Contact Us SEO Details Listed Successfully", Seo::select('seo_id', 'contact_us_meta_title', 'contact_us_meta_description')->get(), 200);
        }
     }

    public function updateSeoDetails(Request $request) {
        Log::info("type==== ". $request->type);
        if( $request->type == 'home') {
            $validate = $this->response->validate($request->all(), $this->storeRulesOne);
            if($validate === true) {
                return $this->response->jsonResponse(false, "Home Page SEO Details Updated Successfully", Seo::first()->update($request->all()), 200);
            } else {
                return $this->response->jsonResponse(true, 'Home Page SEO Details  Update Failed', $validate, 201);
            }
        } else if( $request->type == 'aboutus') {
            $validate = $this->response->validate($request->all(), $this->storeRulesTwo);
            if($validate === true) {
                return $this->response->jsonResponse(false, "About Us Page SEO Details Updated Successfully", Seo::first()->update($request->all()), 200);
            } else {
                return $this->response->jsonResponse(true, 'About Us Page SEO Details  Update Failed', $validate, 201);
            }
        } else if( $request->type == 'facilities') {
            $validate = $this->response->validate($request->all(), $this->storeRulesThree);
            if($validate === true) {
                return $this->response->jsonResponse(false, "Facilities Page SEO Details Updated Successfully", Seo::first()->update($request->all()), 200);
            } else {
                return $this->response->jsonResponse(true, 'Facilities Page SEO Details  Update Failed', $validate, 201);
            }
        } else if( $request->type == 'contactus') {
            $validate = $this->response->validate($request->all(), $this->storeRulesFour);
            if($validate === true) {
                return $this->response->jsonResponse(false, "Contact Us Page SEO Details Updated Successfully", Seo::first()->update($request->all()), 200);
            } else {
                return $this->response->jsonResponse(true, 'Contact Us Page SEO Details  Update Failed', $validate, 201);
            }
        }
    }
}
