package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.entity.MarketingActivity;
import com.haha.entity.dto.ActivityCreateDTO;
import com.haha.entity.dto.ActivityQueryDTO;
import com.haha.entity.dto.ActivityUpdateDTO;
import com.haha.service.MarketingActivityService;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/marketing/activity")
@RequiredArgsConstructor
public class MarketingActivityController {

    private final MarketingActivityService activityService;

    @RequirePermission("marketing:activity:read")
    @GetMapping("/list")
    public Result<PageResult<MarketingActivity>> list(ActivityQueryDTO queryDTO) {
        IPage<MarketingActivity> page = activityService.getPage(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("marketing:activity:read")
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getById(@PathVariable Long id) {
        MarketingActivity activity = activityService.getDetail(id);
        List<Long> shopIds = activityService.getActivityShopIds(id);
        List<Long> productIds = activityService.getActivityProductIds(id);

        Map<String, Object> result = new HashMap<>();
        result.put("activity", activity);
        result.put("shopIds", shopIds);
        result.put("productIds", productIds);

        return Result.success("查询成功", result);
    }

    @RequirePermission("marketing:activity:create")
    @Log(module = "营销管理", operation = OperationType.INSERT, summary = "创建活动")
    @PostMapping
    public Result<MarketingActivity> create(@RequestBody ActivityCreateDTO dto) {
        Long creatorId = StpUtil.getLoginIdAsLong();
        String creatorName = (String) StpUtil.getSession().get("name");

        MarketingActivity activity = activityService.create(dto, creatorId, creatorName);
        return Result.success("创建成功", activity);
    }

    @RequirePermission("marketing:activity:edit")
    @Log(module = "营销管理", operation = OperationType.UPDATE, summary = "编辑活动")
    @PutMapping("/{id}")
    public Result<MarketingActivity> update(@PathVariable Long id, @RequestBody ActivityUpdateDTO dto) {
        dto.setId(id);
        MarketingActivity activity = activityService.update(dto);
        return Result.success("更新成功", activity);
    }

    @RequirePermission("marketing:activity:publish")
    @Log(module = "营销管理", operation = OperationType.UPDATE, summary = "发布活动")
    @PutMapping("/{id}/publish")
    public Result<Void> publish(@PathVariable Long id) {
        activityService.publish(id);
        return Result.success("发布成功", null);
    }

    @RequirePermission("marketing:activity:edit")
    @Log(module = "营销管理", operation = OperationType.UPDATE, summary = "暂停活动")
    @PutMapping("/{id}/pause")
    public Result<Void> pause(@PathVariable Long id) {
        activityService.pause(id);
        return Result.success("暂停成功", null);
    }

    @RequirePermission("marketing:activity:edit")
    @Log(module = "营销管理", operation = OperationType.UPDATE, summary = "恢复活动")
    @PutMapping("/{id}/resume")
    public Result<Void> resume(@PathVariable Long id) {
        activityService.resume(id);
        return Result.success("恢复成功", null);
    }

    @RequirePermission("marketing:activity:delete")
    @Log(module = "营销管理", operation = OperationType.DELETE, summary = "删除活动")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        activityService.deleteActivity(id);
        return Result.success("删除成功", null);
    }

    @RequirePermission("marketing:activity:read")
    @GetMapping("/{id}/statistics")
    public Result<Map<String, Object>> getStatistics(@PathVariable Long id) {
        Map<String, Object> stats = activityService.getStatistics(id);
        return Result.success("查询成功", stats);
    }

    @RequirePermission("marketing:activity:read")
    @GetMapping("/ongoing")
    public Result<List<MarketingActivity>> getOngoingActivities() {
        List<MarketingActivity> activities = activityService.getOngoingActivities();
        return Result.success("查询成功", activities);
    }
}
