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.ShopArchive;
import com.haha.entity.dto.ShopArchiveCreateDTO;
import com.haha.entity.dto.ShopArchiveQueryDTO;
import com.haha.entity.dto.ShopArchiveUpdateDTO;
import com.haha.service.ShopArchiveService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 门店档案管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/shop-archive")
@RequiredArgsConstructor
public class ShopArchiveController {

    private final ShopArchiveService shopArchiveService;

    /**
     * 分页查询门店档案列表
     * @param queryDTO 查询参数
     * @return 门店档案列表
     */
    @RequirePermission("shop:archive:read")
    @GetMapping("/list")
    public Result<PageResult<ShopArchive>> list(ShopArchiveQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<ShopArchive> archivePage = shopArchiveService.getPage(queryDTO);
        return Result.success("查询成功", PageResult.of(archivePage));
    }

    /**
     * 查询门店档案详情（含固定资产、附件）
     * @param id 档案ID
     * @return 档案详情
     */
    @RequirePermission("shop:archive:read")
    @GetMapping("/{id}")
    public Result<ShopArchive> getById(@PathVariable Long id) {
        ShopArchive archive = shopArchiveService.getDetailById(id);
        if (archive == null) {
            return Result.error(404, "门店档案不存在");
        }
        return Result.success("查询成功", archive);
    }

    /**
     * 根据门店ID查询档案
     * @param shopId 门店ID
     * @return 门店档案
     */
    @RequirePermission("shop:archive:read")
    @GetMapping("/by-shop/{shopId}")
    public Result<ShopArchive> getByShopId(@PathVariable Long shopId) {
        ShopArchive archive = shopArchiveService.getByShopId(shopId);
        if (archive == null) {
            return Result.error(404, "该门店尚未创建档案");
        }
        return Result.success("查询成功", archive);
    }

    /**
     * 创建门店档案
     * @param dto 创建参数
     * @return 创建后的档案
     */
    @RequirePermission("shop:archive:create")
    @Log(module = "门店档案管理", operation = OperationType.INSERT, summary = "创建门店档案")
    @PostMapping
    public Result<ShopArchive> create(@RequestBody @Valid ShopArchiveCreateDTO dto) {
        ShopArchive archive = shopArchiveService.create(dto);
        return Result.success("创建成功", archive);
    }

    /**
     * 更新门店档案
     * @param id 档案ID
     * @param dto 更新参数
     * @return 更新后的档案
     */
    @RequirePermission("shop:archive:update")
    @Log(module = "门店档案管理", operation = OperationType.UPDATE, summary = "更新门店档案")
    @PutMapping("/{id}")
    public Result<ShopArchive> update(@PathVariable Long id, @RequestBody @Valid ShopArchiveUpdateDTO dto) {
        dto.setId(id);
        ShopArchive archive = shopArchiveService.update(dto);
        return Result.success("更新成功", archive);
    }

    /**
     * 删除门店档案
     * @param id 档案ID
     * @return 操作结果
     */
    @RequirePermission("shop:archive:delete")
    @Log(module = "门店档案管理", operation = OperationType.DELETE, summary = "删除门店档案")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        shopArchiveService.delete(id);
        return Result.success("删除成功");
    }

    /**
     * 获取枚举选项（供前端下拉框使用）
     * @param enumType 枚举类型：venueType / businessStatus / contractStatus / depositStatus / billingMode / paymentCycle / assetType / assetStatus / attachmentType / paymentMethod / invoiceType / commissionBaseType
     * @return 枚举选项列表
     */
    @RequirePermission("shop:archive:read")
    @GetMapping("/enum-options")
    public Result<List<Map<String, String>>> getEnumOptions(@RequestParam String enumType) {
        return Result.success("查询成功", shopArchiveService.getEnumOptions(enumType));
    }
}
