<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Skill;

class SkillController extends Controller
{
    // To Show
    public function index()
    {
        //code
        $skills = Skill::orderBy('sort_order', 'ASC')->get();

        return view('Skills.index', compact('skills'));
    }
    //To Create
    public function create()
    {
        //code
        return view('Skills.create');
    }
    //To Store
    public function store(Request $request)
    {
        //code
        $request->validate([
            'category' => 'required',
            'name' => 'required|max:100',
            'icon' => 'required|max:150',
            'color' => 'required|max:20',
            'color_alt' => 'required|max:20',
            'sort_order' => 'required|integer',
            'status' => 'required',
        ]);
        Skill::create([
            'category'=>$request->category,
            'name'=>$request->name,
            'icon'=>$request->icon,
            'color'=>$request->color,
            'color_alt'=>$request->color_alt,
            'sort_order' =>$request->sort_order,
            'status' => $request->status,
        ]);

        return redirect()->route('skills.index')->with('success','Skill created successfully');
    }
    // To Edit
    public function edit($id)
    {
        $skill=Skill::findorFail($id);
        return view('Skills.edit', compact('skill'));
    }
    //To Update
    public function update(Request $request, $id)
    {
        //code
        $request->validate([
            'category' => 'required',
            'name' => 'required|max:100',
            'icon' => 'required|max:150',
            'color' => 'required|max:20',
            'color_alt' => 'required|max:20',
            'sort_order' => 'required|integer',
            'status' => 'required',
        ]);

        $skill=Skill::FindorFail($id);

        $skill->update([
            'category'=>$request->category,
            'name'=>$request->name,
            'icon'=>$request->icon,
            'color'=>$request->color,
            'color_alt'=>$request->color_alt,
            'sort_order'=>$request->sort_order,
            'status'=>$request->status,
        ]);

        return redirect()->route('skills.index')->with('success','Skill updated successfully');
    }
    //To Delete
    public function destroy($id)
    {
        //code
        $skill=Skill::FindorFail($id);

        $skill->delete();

        return redirect()->route('skills.index')->with('success','Skill deleted successfully');
    }
}
