package com.honey.honey.controller;

import com.honey.honey.dto.PromotionDetailDto;
import com.honey.honey.dto.PromotionListItemDto;
import com.honey.honey.service.FeatureSwitchService;
import com.honey.honey.service.PublicPromotionService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Public API: list and view promotion details (leaderboard, user progress).
 * Excludes INACTIVE promotions. Requires Bearer auth (app user).
 * When promotions feature switch is false, all endpoints return 404.
 */
@RestController
@RequestMapping("/api/promotions")
@RequiredArgsConstructor
public class PromotionController {

    private final PublicPromotionService publicPromotionService;
    private final FeatureSwitchService featureSwitchService;

    @GetMapping
    public ResponseEntity<List<PromotionListItemDto>> list() {
        if (!featureSwitchService.isPromotionsEnabled()) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(publicPromotionService.listForApp());
    }

    @GetMapping("/{id}")
    public ResponseEntity<PromotionDetailDto> getDetail(@PathVariable int id) {
        if (!featureSwitchService.isPromotionsEnabled()) {
            return ResponseEntity.notFound().build();
        }
        return publicPromotionService.getDetailForApp(id)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }
}
