<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;

class ProductController extends Controller
{
    public function index(): JsonResponse
    {
        $products = Product::query()
            ->select(['id', 'merchant_id', 'title', 'slug', 'sku', 'summary', 'price', 'original_price', 'stock', 'status'])
            ->latest()
            ->get();

        return ApiResponse::success($products, '商品列表获取成功');
    }

    public function show(int $id): JsonResponse
    {
        $product = Product::query()->find($id);
        if (! $product) {
            return ApiResponse::error('商品不存在', 404, null, 404);
        }

        return ApiResponse::success($product, '商品详情获取成功');
    }
}
