<?php

namespace App\Repositories;

use App\Models\PaintingCategory;
use App\Repositories\Interfaces\PaintingCategoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class PaintingCategoryRepository implements PaintingCategoryInterface
{
    public function list(Request $request)
    {
        $query = PaintingCategory::where('is_delete', 0);

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

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

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

    public function store(array $data)
    {
        $data['slug'] = Str::slug($data['name_en']);
        return PaintingCategory::create($data);
    }

    public function update($id, array $data)
    {
        $category = $this->find($id);
        if (!$category) {
            return null;
        }
        if (isset($data['name_en'])) {
            $data['slug'] = Str::slug($data['name_en']);
        }
        $category->update($data);
        return $category;
    }

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

    public function getAllActive()
    {
        return PaintingCategory::where('is_delete', 0)->where('status', 1)->orderBy('name_en')->get();
    }
}
