package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.TagBusinessRelation;
import shop.alien.entity.store.TagCategory;
import shop.alien.entity.store.TagStoreRelation;
import shop.alien.entity.store.vo.TagCategoryVo;
import shop.alien.storeplatform.service.StorePlatformTagBusinessRelationService;
import shop.alien.storeplatform.service.StorePlatformTagCategoryService;
import shop.alien.storeplatform.service.StorePlatformTagRelationService;

import java.util.List;

/**
 * 标签前端控制器
 *
 * @author zhangchen
 * @since 2025-07-16
 */
@Slf4j
@Api(tags = {"商户平台-标签信息"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/storePlatformTag")
@RequiredArgsConstructor
public class StorePlatformTagCategoryController {
    private final StorePlatformTagCategoryService tagCategoryService;
    private final StorePlatformTagBusinessRelationService storeTagBusinessRelationService;
    private final StorePlatformTagRelationService tagStoreRelationService;

    @ApiOperation("创建标签")
    @ApiOperationSupport(order = 1)
    @PostMapping("/createTag")
    public R<TagCategory> createTag(@RequestBody TagCategory tagCategory) {
        log.info("TagCategoryController.createTag?tagCategoryVO={}", tagCategory);
        TagCategory tag = tagCategoryService.createTag(tagCategory);
        return R.data(tag);
    }

    @ApiOperation("获取标签列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/getTagList")
    public R<IPage<TagCategory>> getTagList(@RequestParam(defaultValue = "1") int pageNum,
                                            @RequestParam(defaultValue = "10") int pageSize,
                                            @RequestParam(required = false) String label,
                                            @RequestParam(required = false) String labelType,
                                            @RequestParam(required = false) String labelButtonType
    ) {
        log.info("TagCategoryController.getTagList");
        IPage<TagCategory> storeCouponList = tagCategoryService.getTagList(pageNum, pageSize,label,labelType,labelButtonType);
        return R.data(storeCouponList);
    }

    @ApiOperation("删除标签")
    @ApiOperationSupport(order = 3)
    @PostMapping("/deleteTagCategory")
    public  R<Boolean> deleteTagCategory(@RequestBody TagCategory tagCategory) {
        log.info("TagCategoryController.deleteTagCategory");
        int result = tagCategoryService.deleteTag(tagCategory);
        if (result > 0) {
            return R.data(true);
        }
        return R.fail("失败");
    }

    @ApiOperation("修改标签")
    @ApiOperationSupport(order = 4)
    @PostMapping("/updateTagCategory")
    public R<Boolean> updateTagCategory(@RequestBody TagCategory tagCategory) {
        log.info("TagCategoryController.updateTagCategory?tagCategory={}", tagCategory);
        int num = tagCategoryService.updateTagCategory(tagCategory);
        if (num > 0) {
            return R.data(true);
        }
        return R.fail("失败");
    }

    @ApiOperation("创建标签经营类型关系")
    @ApiOperationSupport(order = 5)
    @PostMapping("/createTagBusinessRelation")
    public R<TagBusinessRelation> createTagBusinessRelation(@RequestBody TagBusinessRelation tagBusinessRelation) {
        log.info("TagCategoryController.createTagBusinessRelation?tagBusinessRelation={}", tagBusinessRelation);
        TagBusinessRelation tag = storeTagBusinessRelationService.createTagBusinessRelation(tagBusinessRelation);
        return R.data(tag);
    }

    @ApiOperation("获取经营类型标签信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/getBusinessRelationTagList")
    public R<List<TagCategoryVo>> getBusinessRelationTagList(@RequestParam(required = false) Integer businessSection) {
        log.info("TagCategoryController.getBusinessRelationTagList");
        List<TagCategoryVo> tagCategoryVoList = tagCategoryService.getBusinessRelationTagList(businessSection);
        return R.data(tagCategoryVoList);
    }


    @ApiOperation("获取标签经营类型关系")
    @ApiOperationSupport(order = 7)
    @GetMapping("/getTagBusinessRelationList")
    public R<IPage<TagBusinessRelation>> getTagBusinessRelationList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int size, @RequestParam(required = false) int businessSection,
                                            @RequestParam(required = false) String businessSectionName
    ) {
        log.info("TagCategoryController.getTagBusinessRelationList");
        IPage<TagBusinessRelation> tagBusinessRelationList = storeTagBusinessRelationService.getTagBusinessRelationList(page, size, businessSection, businessSectionName);
        return R.data(tagBusinessRelationList);
    }

    @ApiOperation("删除标签经营类型关系")
    @ApiOperationSupport(order = 8)
    @PostMapping("/deleteTagBusinessRelation")
    public  R<Boolean> deleteTagBusinessRelation(@RequestBody TagBusinessRelation tagBusinessRelation) {
        log.info("TagCategoryController.deleteTagBusinessRelation");
        int result = storeTagBusinessRelationService.deleteTagBusinessRelation(tagBusinessRelation);
        if (result > 0) {
            return R.data(true);
        }
        return R.fail("失败");
    }

    @ApiOperation("创建/更新店铺标签关系")
    @ApiOperationSupport(order = 9)
    @PostMapping("/createOrUpdateTagStoreRelation")
    public R<TagStoreRelation> createOrUpdateTagStoreRelation(@RequestBody TagStoreRelation tagStoreRelation) {
        log.info("TagCategoryController.createOrUpdateTagStoreRelation?tagStoreRelation={}", tagStoreRelation);
        TagStoreRelation tag = tagStoreRelationService.createOrUpdateTagStoreRelation(tagStoreRelation);
        return R.data(tag);
    }

    @ApiOperation("获取标签经营类型关系")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getTagStoreRelationByStoreId")
    public R<TagStoreRelation> getTagStoreRelationByStoreId( @RequestParam(required = true) Integer storeId) {
        log.info("TagCategoryController.getTagStoreRelationByStoreId={}", storeId);
        TagStoreRelation tagStoreRelation = tagStoreRelationService.getTagStoreRelationByStoreId(storeId);
        return R.data(tagStoreRelation);
    }
}
