<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ProductService;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    use ApiResponse;

    protected $productService;

    public function __construct(ProductService $productService)
    {
        $this->productService = $productService;
    }

    public function getProductById($id)
    {
        try {
            $data = $this->productService->getProductById($id);

            return $this->successResponse($data);
        } catch(\Exception $e) {
            return $this->errorResponse(errorDetails:$e->getMessage());
        }
    }

    public function getProductByName(Request $request, $currentOutletId)
    {   
        $name = $request->query('name');    

        try {
            $data = $this->productService->getProductByName($currentOutletId, $name);

            return $this->successResponse($data);
        } catch(\Exception $e) {
            return $this->errorResponse(errorDetails:$e->getMessage());
        }
    }

    public function getProductByCategory($currentOutletId, $categoryId)
    {
        try {
            $data = $this->productService->getProductByCategory($currentOutletId, $categoryId);

            return $this->successResponse($data);
        } catch(\Exception $e) {
            return $this->errorResponse(errorDetails:$e->getMessage());
        }
    }

    public function getProductBestSellerByOutlet($currentOutletId)
    {
        try {
            $data = $this->productService->getProductBestSellerByOutlet($currentOutletId);

            return $this->successResponse($data);
        } catch(\Exception $e) {
            return $this->errorResponse(errorDetails:$e->getMessage());
        }
    }
}
