<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\JobseekerSkill;

class JobseekerSkillController extends Controller
{
    // 🔹 list skill user
    public function index()
    {
        return JobseekerSkill::with('skill')
            ->where('jobseeker_id', auth()->user()->jobseeker->id)
            ->get();
    }

    // 🔹 add / attach skill
    public function store(Request $request)
    {
        $data = $request->validate([
            'skill_id' => 'required',
            'proficiency_level' => 'nullable|in:beginner,intermediate,advanced,expert',
            'years_of_experience' => 'nullable|integer'
        ]);

        $data['jobseeker_id'] = auth()->user()->jobseeker->id;

        return JobseekerSkill::updateOrCreate(
            [
                'jobseeker_id' => $data['jobseeker_id'],
                'skill_id' => $data['skill_id']
            ],
            $data
        );
    }

    // 🔹 update skill
    public function update(Request $request, $id)
    {
        $skill = JobseekerSkill::findOrFail($id);

        $skill->update($request->only([
            'proficiency_level',
            'years_of_experience'
        ]));

        return $skill;
    }

    // 🔹 delete skill
    public function destroy($id)
    {
        JobseekerSkill::destroy($id);

        return response()->json(['message'=>'deleted']);
    }
}