package com.maternalmall.controller;

import com.maternalmall.common.ApiResponse;
import com.maternalmall.domain.Banner;
import com.maternalmall.domain.Product;
import com.maternalmall.domain.Review;
import com.maternalmall.repository.ReviewRepository;
import com.maternalmall.service.MallService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/public")
public class PublicController {
    private final MallService mallService;
    private final ReviewRepository reviewRepository;

    public PublicController(MallService mallService, ReviewRepository reviewRepository) {
        this.mallService = mallService;
        this.reviewRepository = reviewRepository;
    }

    @GetMapping("/products")
    public ApiResponse<List<Product>> products(@RequestParam(required = false) String keyword) {
        return ApiResponse.ok(mallService.searchProducts(keyword));
    }

    @GetMapping("/products/{id}")
    public ApiResponse<Product> product(@PathVariable Long id) {
        return ApiResponse.ok(mallService.getProduct(id));
    }

    @GetMapping("/products/{id}/reviews")
    public ApiResponse<List<Review>> reviews(@PathVariable Long id) {
        return ApiResponse.ok(reviewRepository.findByProductId(id));
    }

    @GetMapping("/banners")
    public ApiResponse<List<Banner>> banners() {
        return ApiResponse.ok(mallService.banners());
    }
}
