<?php

namespace App\Repositories;

use App\Models\Career;

class CareerRepository
{
    public static function fetchAvailableItems()
    {
        return Career::orderBy('id', 'desc')->get();
    }

    public static function fetchItems($filters)
    {
        return Career::orderBy('id', 'desc')->simplePaginate(20);
    }

    public static function saveCareer($payload)
    {

        $career = new Career();
        if (isset($payload['id'])) {
            $career = self::fetchCareer(['id' => $payload['id']]);
        }
        $career->title = $payload['title'];
        $career->description = $payload['description'];
        $career->is_available = true;

        return $career->save();
    }

    public static function deleteCareer($filters)
    {
        return Career::where('id', $filters['id'])->delete();
    }

    public static function fetchCareer($filters)
    {
        return Career::where('id', $filters['id'])->first();
    }

    public static function changeStatus($payload)
    {

        $career = self::fetchCareer(['id' => $payload['id']]);
        if ($career) {
            $career->is_available = ! $career->is_available;
            if ($career->save()) {
                return ['status' => true, 'text' => $career->is_available ? 'Yes' : 'No'];
            }
        }

        return false;
    }
}
