<?php

namespace App\Livewire\Admin\Careers;

use Livewire\Component;
use App\Models\Career;
use Illuminate\Support\Str;

class CareerEdit extends Component
{
    public $careerId;

    public $title;
    public $experience;
    public $location;
    public $skills;
    public $responsibilities;
    public $status;

    public function mount($id)
    {
        $career = Career::findOrFail($id);

        $this->careerId = $career->id;
        $this->title = $career->title;
        $this->experience = $career->experience;
        $this->location = $career->location;
        $this->skills = $career->skills;
        $this->responsibilities = $career->responsibilities;
        $this->status = $career->status;
    }

    public function save()
    {
        $this->validate([
            'title' => 'required|string|max:255',
            'experience' => 'nullable|string|max:100',
            'location' => 'nullable|string|max:150',
            'skills' => 'nullable|string',
            'responsibilities' => 'nullable|string',
            'status' => 'required|boolean',
        ]);

        $career = Career::findOrFail($this->careerId);

        $career->update([
            'title' => $this->title,
            'slug' => Str::slug($this->title),
            'experience' => $this->experience,
            'location' => $this->location,
            'skills' => $this->skills,
            'responsibilities' => $this->responsibilities,
            'status' => $this->status,
        ]);

        session()->flash('success', 'Job updated successfully 🎉');

        return redirect('/console/job-openings');
    }

    public function render()
    {
        return view('livewire.admin.careers.career-edit')
            ->layout('layouts.admin');
    }
}
