package com.smyhub.store.mengyastorebackendjava.controller.admin;

import com.smyhub.store.mengyastorebackendjava.dto.request.AdminProductRequest;
import com.smyhub.store.mengyastorebackendjava.dto.request.ProductStatusRequest;
import com.smyhub.store.mengyastorebackendjava.dto.response.ApiResponse;
import com.smyhub.store.mengyastorebackendjava.dto.response.ProductResponse;
import com.smyhub.store.mengyastorebackendjava.service.ProductService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/products")
@RequiredArgsConstructor
public class AdminProductController {

    private final ProductService productService;

    @GetMapping
    public ResponseEntity<ApiResponse<List<ProductResponse>>> listProducts() {
        return ResponseEntity.ok(ApiResponse.ok(productService.listAllProducts()));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<ProductResponse>> createProduct(
            @Valid @RequestBody AdminProductRequest req) {
        return ResponseEntity.ok(ApiResponse.ok(productService.createProduct(req)));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ProductResponse>> updateProduct(
            @PathVariable String id,
            @Valid @RequestBody AdminProductRequest req) {
        return ResponseEntity.ok(ApiResponse.ok(productService.updateProduct(id, req)));
    }

    @PatchMapping("/{id}/status")
    public ResponseEntity<ApiResponse<ProductResponse>> toggleStatus(
            @PathVariable String id,
            @RequestBody ProductStatusRequest req) {
        return ResponseEntity.ok(ApiResponse.ok(productService.toggleStatus(id, req.isActive())));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteProduct(@PathVariable String id) {
        productService.deleteProduct(id);
        return ResponseEntity.ok(ApiResponse.ok(null));
    }
}
