<?php

namespace App\Http\Controllers\Admin;

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

class ItemController extends Controller
{
    public function __construct()
    {
        $this->middleware('role:admin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $items = Item::with('factor')
            ->get()
            ->groupBy('factor.name');

        return view('admin.items.index', compact('items'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $factors = Factor::where('status', 'active')->get();
        return view('admin.items.create', compact('factors'));
    }

    /**
     * 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(), [
            'factor_id' => 'required',
            'code'      => 'required',
            'value'     => 'required|numeric',
            'content'   => 'required',
            'status'    => 'required',
        ]);

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

        $item = Item::create([
            'factor_id' => $request->factor_id,
            'code'      => $request->code,
            'value'     => $request->value,
            'content'   => $request->content,
            'status'    => $request->status,
        ]);

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

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function show(Item $item)
    {
        $factors = Factor::where('status', 'active')->get();
        return view('admin.items.detail', compact('item', 'factors'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function edit(Item $item)
    {
        $factors = Factor::where('status', 'active')->get();
        return view('admin.items.edit', compact('item', 'factors'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Item $item)
    {
        $validator = Validator::make($request->all(), [
            'factor_id' => 'required',
            'code'      => 'required',
            'value'     => 'required|numeric',
            'content'   => 'required',
            'status'    => 'required',
        ]);

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

        $item->update([
            'factor_id' => $request->factor_id,
            'code'      => $request->code,
            'value'     => $request->value,
            'content'   => $request->content,
            'status'    => $request->status,
        ]);

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

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