<?php

namespace App\Http\Repositories;


use App\Models\PrePackedAddon;
use Illuminate\Support\Str;

class PrePackedAddOnRepository
{

    public function get($request)
    {
        $column = ['id', 'name', 'price', 'position', 'updated_at', 'id'];
        $searchColumns = ['day','name', 'price', 'position', 'updated_at', 'id'];
        $search = $request['search'];
        $order = $request['orderedColumn'];
        $orderBy = $request['orderBy'];

        // Query
        $query = PrePackedAddon::distinct();

        // search
        if ($search != '') {
            $query->where(function ($q) use ($search, $searchColumns) {
                foreach ($searchColumns as $key => $value) {
                    ($key == 0) ? $q->where($value, 'LIKE', '%' . $search . '%') : $q->orWhere($value, 'LIKE', '%' . $search . '%');
                }

            });
        }

        // sorting
        ($order == '') ? $query->orderByDesc('id') : $query->orderBy($order, $orderBy);

        return $query->paginate(isset($request['perPage']) ? $request['perPage'] : 15);
    }

    public function store($request)
    {
        $data = $request->validated();
        PrePackedAddon::create($data);
        return true;
    }

    public function update($request,$category)
    {
        $data = $request->validated();
        $category->update($data);
        return true;
    }

    public function updateStatus($category)
    {
        $existing = PrePackedAddon::find($category);

        $existing->update([
            'status' => !$existing->status
        ]);
        
        return true;
    }
   

}
