<?php

namespace App\Http\Controllers\Admin;

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.addictions.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',
            'code'              => 'required',
            'min_percentage'    => 'required|numeric|regex:/^\d*\.?\d{0,2}$/',
            'max_percentage'    => 'required|numeric|regex:/^\d*\.?\d{0,2}$/',
            'status'            => 'required',
        ]);

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

        $addiction = Addiction::create([
            'name'              => $request->name,
            'code'              => $request->code,
            'description'       => $request->description,
            'solution'          => $request->solution,
            'min_percentage'    => $request->min_percentage,
            'max_percentage'    => $request->max_percentage,
            'status'            => $request->status,
        ]);

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

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Addiction  $addiction
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Addiction $addiction)
    {
        $validator = Validator::make($request->all(), [
            'name'              => 'required',
            'code'              => 'required',
            'min_percentage'    => 'required|numeric|regex:/^\d*\.?\d{0,2}$/',
            'max_percentage'    => 'required|numeric|regex:/^\d*\.?\d{0,2}$/',
            'status'            => 'required',
        ]);

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

        $addiction->update([
            'name'              => $request->name,
            'code'              => $request->code,
            'description'       => $request->description,
            'solution'          => $request->solution,
            'min_percentage'    => $request->min_percentage,
            'max_percentage'    => $request->max_percentage,
            'status'            => $request->status,
        ]);

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

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