<?php

namespace App\Http\Controllers\api;

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

class SkillController extends Controller
{
    public function index(Request $request)
    {
        $skill = Skill::select('id', 'name');

        $search = isset($request->search['value']) ? $request->search['value'] : '';
        if (!empty($search)) {
            $skill->where(function ($query) use ($search) {
                $query->where('name', 'like', '%'.$search.'%');
            });
        }

        $total_data = $skill->count();
        $length = intval(isset($request->length) ? $request->length : 0);
        $start = intval(isset($request->start) ? $request->start : 0);

        if (!isset($request->length) || !isset($request->start)) {
            $skill = $skill->get();
        } else {
            $skill = $skill->skip($start)->take($length)->get();
        }

        return response()->json([
            'error' => false,
            'message' => 'Berhasil mengambil data.',
            'data' => $skill,
            'draw' => $request->draw,
            'recordsTotal' => $total_data,
            'recordsFiltered' => $total_data,
        ], 200);
    }

    public function show($id)
    {
        $skill = Skill::findOrFail($id);
        return response()->json([
            'error' => false,
            'message' => 'Berhasil mengambil data.',
            'data' => $skill
        ], 200);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ], [
            'required' => ':attribute harus diisi.',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'error' => true,
                'message' => Str::ucfirst($validator->errors()->first()),
                'data' => null
            ]);
        }

        $skill = Skill::create([
            'name' => $request->name,
        ]);

        return response()->json([
            'error' => false,
            'message' => 'Skill berhasil ditambahkan.',
            'data' => null
        ]);
    }

    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ], [
            'required' => ':attribute harus diisi.',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'error' => true,
                'message' => Str::ucfirst($validator->errors()->first()),
                'data' => null
            ]);
        }

        $update = [
            'name' => $request->name
        ];

        $skill = Skill::findOrFail($id);
        $skill->update($update);

        return response()->json([
            'error' => false,
            'message' => 'Skill berhasil diubah.',
            'data' => null
        ]);
    }

    public function destroy($id)
    {
        $skill = Skill::findOrFail($id);

        $name = $skill->name;

        $skill->delete();

        return response()->json([
            'error' => false,
            'message' => 'Skill '.$name.' berhasil dihapus.',
            'data' => null
        ]);
    }
}
