<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Likert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LikertController extends Controller
{
    public function __construct()
    {
        $this->middleware('role:admin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $likerts = Likert::all();
        return view('admin.likerts.index', compact('likerts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.likerts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name'  => 'required',
            'score' => 'required|numeric',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $likert = Likert::create([
            'name'  => $request->name,
            'score' => $request->score,
        ]);

        if ($likert) {
            return redirect()->route('admin.likerts.index')->with('success', 'Likert created successfully.');
        } else {
            return redirect()->back()->with('error', 'Failed to create likert.');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Likert  $likert
     * @return \Illuminate\Http\Response
     */
    public function show(Likert $likert)
    {
        return view('admin.likerts.detail', compact('likert'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Likert  $likert
     * @return \Illuminate\Http\Response
     */
    public function edit(Likert $likert)
    {
        return view('admin.likerts.edit', compact('likert'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Likert  $likert
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Likert $likert)
    {
        $validator = Validator::make($request->all(), [
            'name'  => 'required',
            'score' => 'required|numeric',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $likert->update([
            'name'  => $request->name,
            'score' => $request->score,
        ]);

        if ($likert) {
            return redirect()->route('admin.likerts.index')->with('success', 'Likert updated successfully.');
        } else {
            return redirect()->back()->with('error', 'Failed to create updated.');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Likert  $likert
     * @return \Illuminate\Http\Response
     */
    public function destroy(Likert $likert)
    {
        if (!$likert->delete()) {
            return response()->json([
                'status'  => false,
                'message' => 'Data has been used on another page'
            ], 400);
        }
        return response()->json([
            'status'  => true,
            'message' => 'Success delete data'
        ], 200);
    }
}
