<?php
namespace App\Repositories;

use App\Models\Product;
use App\Repositories\Interfaces\Productinterface;
use App\Traits\FileUploadTrait;
use Illuminate\Http\Request;

class ProductRepository implements Productinterface
{

    use FileUploadTrait;
    public function list(Request $request)
    {
        $query = Product::select(
            'id',
            'name_en',
            'cat_id',
            'brand_logo',
            'is_delete',
            'status',
        )
            ->with('cats') // Eager load the category relationship
            ->where('is_delete', 0);

        // Add search filter if provided
        if ($request->has('search') && ! empty($request->search)) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('name_en', 'like', '%' . $search . '%')
                    ->orWhereHas('cats', function ($q) use ($search) {
                        $q->where('name_en', 'like', '%' . $search . '%');
                    });
            });
        }

        // Add category filter if provided
        if ($request->has('cat_id') && ! empty($request->cat_id)) {
            $query->where('cat_id', $request->cat_id);
        }

        $list = $query->orderBy('id', 'DESC')
            ->paginate(10);

        $baseImgPath = $this->getBasePath();

        $list->map(function ($item) use ($baseImgPath) {
            $item['brand_logo'] = $item->brand_logo ? $baseImgPath . $item->brand_logo : null;
        });

        return $list;
    }

    public function find($id)
    {
        $product = Product::find($id);
        if (! $product) {
            return null;
        }

        $baseImgPath           = $this->getBasePath();
        $product['brand_logo'] = $product->brand_logo ? $baseImgPath . $product->brand_logo : null;
        $product['img_1']      = $product->img_1 ? $baseImgPath . $product->img_1 : null;
        $product['img_2']      = $product->img_2 ? $baseImgPath . $product->img_2 : null;
        $product['img_3']      = $product->img_3 ? $baseImgPath . $product->img_3 : null;
        $product['img_4']      = $product->img_4 ? $baseImgPath . $product->img_4 : null;

        return $product;
    }

    public function store(Request $request, $brand_logo, $img_1, $img_2, $img_3, $img_4)
    {
        $product                   = new Product;
        $product->name_en          = $request->name_en;
        $product->cat_id           = $request->cat_id;
        $product->brand_logo       = $brand_logo;
        $product->composition      = $request->composition;
        $product->introduction     = $request->introduction;
        $product->img_1            = $img_1;
        $product->img_2            = $img_2;
        $product->img_3            = $img_3;
        $product->img_4            = $img_4;
        $product->benefits         = $request->benefits;
        $product->details          = $request->details;
        $product->meta_description = $request->meta_description;
        $product->meta_title       = $request->meta_title;
        $product->meta_keywords    = $request->meta_keywords;
        $product->meta_author      = $request->meta_author;
        $product->save();

        return $product;
    }

    public function update($id, array $data)
    {
        $product = Product::find($id);
        if (! $product) {
            return response()->json(['error' => 'Product not found'], 404);
        }

        $product->update($data);
        return $product;
    }

    public function delete($id)
    {
        $city = Product::find($id);
        if (! $city) {
            return null; // or handle the case where doctor is not found
        }
        $city->update(['is_delete' => 1]);
        return $city;
    }

}
