package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.pricing.dto.req.SceneCouponConfigPageReq;
import com.ycwl.basic.pricing.dto.req.SceneCouponConfigSaveReq;
import com.ycwl.basic.pricing.dto.resp.SceneCouponConfigResp;
import com.ycwl.basic.pricing.service.ISceneCouponService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 场景优惠券配置管理控制器（后台管理端）
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing/admin/scene-coupon")
@RequiredArgsConstructor
public class SceneCouponConfigController {

    private final ISceneCouponService sceneCouponService;

    /**
     * 分页查询场景优惠券配置
     */
    @PostMapping("/page")
    public ApiResponse<PageInfo<SceneCouponConfigResp>> page(@RequestBody SceneCouponConfigPageReq req) {
        try {
            PageInfo<SceneCouponConfigResp> pageInfo = sceneCouponService.pageConfig(req);
            return ApiResponse.success(pageInfo);
        } catch (Exception e) {
            log.error("场景优惠券|分页查询失败", e);
            return ApiResponse.fail("分页查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取配置详情
     */
    @GetMapping("/detail/{id}")
    public ApiResponse<SceneCouponConfigResp> getDetail(@PathVariable("id") Long id) {
        try {
            SceneCouponConfigResp detail = sceneCouponService.getConfigDetail(id);
            if (detail == null) {
                return ApiResponse.fail("记录不存在");
            }
            return ApiResponse.success(detail);
        } catch (Exception e) {
            log.error("场景优惠券|详情查询失败 id={}", id, e);
            return ApiResponse.fail("详情查询失败: " + e.getMessage());
        }
    }

    /**
     * 保存配置（新增/更新）
     */
    @PostMapping("/save")
    public ApiResponse<Boolean> save(@RequestBody SceneCouponConfigSaveReq req) {
        try {
            boolean success = sceneCouponService.saveConfig(req);
            return ApiResponse.success(success);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fail(e.getMessage());
        } catch (Exception e) {
            log.error("场景优惠券|保存失败", e);
            return ApiResponse.fail("保存失败: " + e.getMessage());
        }
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Boolean> delete(@PathVariable("id") Long id) {
        try {
            boolean success = sceneCouponService.deleteConfig(id);
            return ApiResponse.success(success);
        } catch (Exception e) {
            log.error("场景优惠券|删除失败 id={}", id, e);
            return ApiResponse.fail("删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有已配置的场景列表（用于前端下拉选择）
     */
    @GetMapping("/scenes")
    public ApiResponse<List<String>> listSceneKeys() {
        try {
            List<String> sceneKeys = sceneCouponService.listSceneKeys();
            return ApiResponse.success(sceneKeys);
        } catch (Exception e) {
            log.error("场景优惠券|场景列表查询失败", e);
            return ApiResponse.fail("场景列表查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据场景和景区查询配置列表
     */
    @GetMapping("/by-scene")
    public ApiResponse<List<SceneCouponConfigResp>> listByScene(
            @RequestParam String sceneKey,
            @RequestParam Long scenicId) {
        try {
            List<SceneCouponConfigResp> list = sceneCouponService.listBySceneKeyAndScenicId(sceneKey, scenicId);
            return ApiResponse.success(list);
        } catch (Exception e) {
            log.error("场景优惠券|按场景查询失败 sceneKey={}, scenicId={}", sceneKey, scenicId, e);
            return ApiResponse.fail("按场景查询失败: " + e.getMessage());
        }
    }
}
