<?php

namespace App\Http\Controllers\Admin;

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.factors.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',
            'status'    => 'required',
        ]);

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

        $factor = Factor::create([
            'name'      => $request->name,
            'status'    => $request->status,
        ]);

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

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

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

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

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

        $factor->update([
            'name'      => $request->name,
            'status'    => $request->status,
        ]);

        if ($factor) {
            return redirect()->route('admin.factors.index')->with('success', 'Factor updated successfully.');
        } else {
            return redirect()->back()->with('error', 'Failed to update factor.');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Factor  $factor
     * @return \Illuminate\Http\Response
     */
    public function destroy(Factor $factor)
    {
        if (!$factor->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);
    }
}
