<?php

namespace App\Repositories;

use App\Models\ArtTag;
use App\Repositories\Interfaces\ArtTagInterface;
use Illuminate\Http\Request;

class ArtTagRepository implements ArtTagInterface
{
    public function list(Request $request)
    {
        $query = ArtTag::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 getAllActive()
    {
        return ArtTag::where('is_delete', 0)->where('status', 1)->orderBy('name_en')->get();
    }

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

    public function findByName($name)
    {
        return ArtTag::where('is_delete', 0)
                     ->whereRaw('LOWER(name_en) = ?', [strtolower(trim($name))])
                     ->first();
    }

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

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

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