package com.smyhub.store.mengyastorebackendjava.controller;

import com.smyhub.store.mengyastorebackendjava.dto.response.ApiResponse;
import com.smyhub.store.mengyastorebackendjava.dto.response.MaintenanceResponse;
import com.smyhub.store.mengyastorebackendjava.dto.response.StatsResponse;
import com.smyhub.store.mengyastorebackendjava.repository.OrderRepository;
import com.smyhub.store.mengyastorebackendjava.service.SiteService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class StatsController {

    private final OrderRepository orderRepository;
    private final SiteService siteService;

    @GetMapping("/stats")
    public ResponseEntity<ApiResponse<StatsResponse>> getStats() {
        long total = orderRepository.count();
        int visits = siteService.getTotalVisits();
        return ResponseEntity.ok(ApiResponse.ok(StatsResponse.builder()
                .totalOrders(total)
                .totalVisits(visits)
                .build()));
    }

    @PostMapping("/site/visit")
    public ResponseEntity<ApiResponse<Map<String, Object>>> recordVisit() {
        int total = siteService.incrementVisits();
        return ResponseEntity.ok(ApiResponse.ok(Map.of("total", total, "counted", true)));
    }

    @GetMapping("/site/maintenance")
    public ResponseEntity<ApiResponse<MaintenanceResponse>> getMaintenance() {
        return ResponseEntity.ok(ApiResponse.ok(siteService.getMaintenance()));
    }
}
