<?php

namespace App\Http\Controllers;

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


class FooterFormController extends Controller
{
    //
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = [
            'name' => 'required',
            'email_foot' => 'required',
            'message' => 'required',
            'mobile_number' => 'required',

        ];
    }

    public function indexFooterForm() {
        return $this->response->jsonResponse(false,"Form  Details Fetched Successfully", FooterForm::orderBy('id', 'desc')->get(), 300);
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Form Details', 'store'), FooterForm::create($request->all()), 301);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Form Details', 'show'), $this->findFooterForm($id), 302);
    }
    // public function updateFooterForm(Request $request) {
    //     $validate = $this->response->validate($request->all(), [
    //         'name' => ['required'],
    //         'email_foot' => ['required'],
    //         'message' => ['required'],
    //     ]);
    //     if($validate === true) {
    //         return $this->response->jsonResponse(false, $this->response->message('Form Details', 'update'), $this->findFooterForm($request->id)->update($request->all()), 303);
    //     } else {
    //         return $validate;
    //     }
    // }

    public function updateFooterForm(Request $request) {
        Log::info('request =='. $request);
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, 'Your Details Successfully Updated', FooterForm::create($request->all()), 502);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findFooterForm($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Form Details', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Form Details Not Exists', [], 305);
    }

    public function findFooterForm($id) {
        return FooterForm::find($id);
    }

    public function FooterFormSwitch($id) {
        $size = $this->findFooterForm($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Form Details '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Form Details Not Exists', [], 307);
    }


}
