package com.smyhub.infogenie.infogeniebackendjava.controller;

import com.smyhub.infogenie.infogeniebackendjava.dto.request.FeatureClickIncrementRequest;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.FeatureClickResponse;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.SiteDisabledResponse;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.SiteSourceResponse;
import com.smyhub.infogenie.infogeniebackendjava.service.FeatureClickService;
import com.smyhub.infogenie.infogeniebackendjava.service.SiteConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Public site-configuration read endpoints and feature-card click tracking.
 *
 * GET  /api/site/60s-disabled
 * GET  /api/site/60s-source
 * GET  /api/site/ai-model-disabled
 * GET  /api/site/feature-card-clicks?section=...
 * POST /api/site/feature-card-clicks/increment
 */
@RestController
@RequestMapping("/api/site")
@RequiredArgsConstructor
public class SiteConfigController {

    private final SiteConfigService siteConfigService;
    private final FeatureClickService featureClickService;

    @GetMapping("/60s-disabled")
    public ResponseEntity<SiteDisabledResponse> get60sDisabled() {
        return ResponseEntity.ok(
                new SiteDisabledResponse(siteConfigService.get60sDisabled()));
    }

    @GetMapping("/60s-source")
    public ResponseEntity<SiteSourceResponse> get60sSource() {
        return ResponseEntity.ok(siteConfigService.get60sSource());
    }

    @GetMapping("/ai-model-disabled")
    public ResponseEntity<SiteDisabledResponse> getAiModelDisabled() {
        return ResponseEntity.ok(
                new SiteDisabledResponse(siteConfigService.getAiModelDisabled()));
    }

    @GetMapping("/feature-card-clicks")
    public ResponseEntity<?> getFeatureClicks(@RequestParam(required = false) String section) {
        if (section == null || section.isBlank()) {
            return ResponseEntity.badRequest()
                    .body(Map.of("error", "section parameter is required"));
        }
        return ResponseEntity.ok(featureClickService.getClicksForSection(section));
    }

    @PostMapping("/feature-card-clicks/increment")
    public ResponseEntity<?> incrementClick(@RequestBody FeatureClickIncrementRequest req) {
        if (req.getSection() == null || req.getItemId() == null) {
            return ResponseEntity.badRequest()
                    .body(Map.of("error", "section and item_id are required"));
        }
        FeatureClickResponse resp =
                featureClickService.increment(req.getSection(), req.getItemId());
        return ResponseEntity.ok(resp);
    }
}
