<?php

namespace App\Repositories;

use App\Models\Testimonial;
use App\Repositories\Interfaces\TestimonialInterface;
use Illuminate\Http\Request;

class TestimonialRepository implements TestimonialInterface
{
    public function list(Request $request)
    {
        $query = Testimonial::where('is_delete', 0);

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where('client_name', 'like', "%{$search}%");
        }

        return $query->orderBy('id', 'desc')->paginate(10);
    }

    public function find($id)
    {
        return Testimonial::where('is_delete', 0)->find($id);
    }

    public function store(array $data)
    {
        return Testimonial::create($data);
    }

    public function update($id, array $data)
    {
        $testimonial = $this->find($id);
        if (!$testimonial) return null;
        $testimonial->update($data);
        return $testimonial;
    }

    public function delete($id)
    {
        $testimonial = $this->find($id);
        if (!$testimonial) return null;
        $testimonial->update(['is_delete' => 1]);
        return $testimonial;
    }
}
