package com.haha.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
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.TimedDiscountActivity;
import com.haha.entity.TimedDiscountRecord;
import com.haha.entity.TimedDiscountStatistics;
import com.haha.entity.PriceAdjustmentLog;
import com.haha.entity.dto.TimedDiscountCreateDTO;
import com.haha.entity.dto.TimedDiscountQueryDTO;
import com.haha.entity.dto.TimedDiscountRecordQueryDTO;
import com.haha.entity.dto.TimedDiscountUpdateDTO;
import com.haha.service.TimedDiscountService;
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("/timed-discount")
@RequiredArgsConstructor
public class TimedDiscountController {

    private final TimedDiscountService timedDiscountService;

    @RequirePermission("timed-discount:read")
    @GetMapping("/list")
    public Result<PageResult<TimedDiscountActivity>> list(TimedDiscountQueryDTO queryDTO) {
        IPage<TimedDiscountActivity> page = timedDiscountService.getPage(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("timed-discount:read")
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getById(@PathVariable Long id) {
        TimedDiscountActivity activity = timedDiscountService.getDetail(id);
        List<Long> shopIds = timedDiscountService.getActivityShopIds(id);
        List<Long> productIds = timedDiscountService.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("timed-discount:create")
    @Log(module = "定时折扣", operation = OperationType.INSERT, summary = "创建活动")
    @PostMapping
    public Result<TimedDiscountActivity> create(@RequestBody TimedDiscountCreateDTO dto) {
        Long creatorId = StpUtil.getLoginIdAsLong();
        String creatorName = (String) StpUtil.getSession().get("name");

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

    @RequirePermission("timed-discount:edit")
    @Log(module = "定时折扣", operation = OperationType.UPDATE, summary = "编辑活动")
    @PutMapping("/{id}")
    public Result<TimedDiscountActivity> update(@PathVariable Long id, @RequestBody TimedDiscountUpdateDTO dto) {
        dto.setId(id);
        TimedDiscountActivity activity = timedDiscountService.update(dto);
        return Result.success("更新成功", activity);
    }

    @RequirePermission("timed-discount:edit")
    @Log(module = "定时折扣", operation = OperationType.UPDATE, summary = "更新状态")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        timedDiscountService.updateStatus(id, status);
        return Result.success("状态更新成功", null);
    }

    @RequirePermission("timed-discount:delete")
    @Log(module = "定时折扣", operation = OperationType.DELETE, summary = "删除活动")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        timedDiscountService.deleteActivity(id);
        return Result.success("删除成功", null);
    }

    @RequirePermission("timed-discount:execute")
    @Log(module = "定时折扣", operation = OperationType.UPDATE, summary = "手动执行活动")
    @PostMapping("/{id}/execute")
    public Result<Void> execute(@PathVariable Long id) {
        timedDiscountService.executeDiscountActivity(id);
        return Result.success("执行成功", null);
    }

    @RequirePermission("timed-discount:read")
    @GetMapping("/records")
    public Result<PageResult<TimedDiscountRecord>> getRecords(TimedDiscountRecordQueryDTO queryDTO) {
        IPage<TimedDiscountRecord> page = timedDiscountService.getRecordPage(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("timed-discount:read")
    @GetMapping("/records/{recordId}")
    public Result<TimedDiscountRecord> getRecordDetail(@PathVariable Long recordId) {
        TimedDiscountRecord record = timedDiscountService.getRecordDetail(recordId);
        return Result.success("查询成功", record);
    }

    @RequirePermission("timed-discount:read")
    @GetMapping("/records/{recordId}/logs")
    public Result<PageResult<PriceAdjustmentLog>> getPriceAdjustmentLogs(
            @PathVariable Long recordId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        IPage<PriceAdjustmentLog> pageResult = timedDiscountService.getPriceAdjustmentLogs(recordId, page, pageSize);
        return Result.success("查询成功", PageResult.of(pageResult));
    }

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

    @RequirePermission("timed-discount:read")
    @GetMapping("/{id}/statistics/daily")
    public Result<PageResult<TimedDiscountStatistics>> getDailyStatistics(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        IPage<TimedDiscountStatistics> pageResult = timedDiscountService.getStatisticsPage(id, page, pageSize);
        return Result.success("查询成功", PageResult.of(pageResult));
    }

    @RequirePermission("timed-discount:read")
    @GetMapping("/enabled")
    public Result<List<TimedDiscountActivity>> getEnabledActivities() {
        List<TimedDiscountActivity> activities = timedDiscountService.getEnabledActivities();
        return Result.success("查询成功", activities);
    }
}
