package shop.alien.storeplatform.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.vo.StoreImgDTO;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.WebAudit;
import shop.alien.entity.store.vo.StoreImgVo;
import shop.alien.entity.storePlatform.vo.StoreLicenseHistoryDto;
import shop.alien.entity.storePlatform.vo.StoreLicenseHistoryVO;
import shop.alien.mapper.WebAuditMapper;
import shop.alien.storeplatform.service.LicenseService;

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


@Slf4j
@Api(tags = {"商家端-证照管理"})
@ApiSort(3)
@CrossOrigin
@RestController
@RequestMapping("/license")
@RequiredArgsConstructor
public class LicenseController {

    private final LicenseService licenseService;

    private final WebAuditMapper webAuditMapper;

    @ApiOperation("获取营业执照图片信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Integer", value = "城市", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/querybusinessLicenseList")
    public R<List<StoreImg>> querybusinessLicenseList(Integer id) {
        log.info("LicenseController.getbusinessLicenseList?id={}", id);
        List<StoreImg> result = licenseService.getbusinessLicenseList(id);
        return R.data(result);
    }

    @ApiOperation("获取合同图片信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "城市", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping("/queryContractList")
    public R<List<StoreImgDTO>> queryContractList(Integer id) {
        log.info("LicenseController.queryContractList?id={}", id);
        List<StoreImgDTO> result = licenseService.getContractList(id);
        return R.data(result);
    }

    @ApiOperation("获取经营许可证图片信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "城市", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping("/queryFoodLicenceList")
    public R<List<StoreImgDTO>> queryFoodLicenceList(Integer id) {
        log.info("LicenseController.queryFoodLicenceList?id={}", id);
        List<StoreImgDTO> result = licenseService.getFoodLicenceList(id);
        return R.data(result);
    }

    @ApiOperation(value = "门店装修-修改食品经营许可证")
    @PostMapping("/uploadfoodLicence")
    public R<String> uploadfoodLicence(@RequestBody StoreImg storeImg) {
        log.info("StoreInfoController.uploadfoodLicence?storeImg={}", storeImg);
        int num = licenseService.uploadfoodLicence(storeImg);
        if (num > 0) {
            WebAudit webAudit = new WebAudit();
            webAudit.setStoreInfoId(storeImg.getStoreId().toString());
            webAudit.setType("7");
            webAudit.setStatus("0");
            webAudit.setContent("经营许可证");
            webAuditMapper.insert(webAudit);
            return R.success("食品经营许可证图片添加成功");
        }
        return R.fail("食品经营许可证图片添加失败");
    }

    @ApiOperation("根据商户ID获取经营许可证历史记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryLicenceByStatusList")
    public R<List<StoreLicenseHistoryDto>> queryLicenceByStatusList(@RequestParam("storeId") Integer storeId) {
        log.info("LicenseController.queryLicenceByStatusList: storeId={}", storeId);
        try {
            List<StoreLicenseHistoryDto> result = licenseService.queryLicenceByStatusList(storeId);
            return R.data(result);
        } catch (Exception e) {
            log.error("LicenseController.queryLicenceByStatusList ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "门店装修-食品经营许可证状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "门店id", dataType = "int", paramType = "query", required = true)})
    @GetMapping("/getStoreFoodLicenceStatus")
    public R<Map<String, Object>> getStoreFoodLicenceStatus(int id) {
        log.info("StoreInfoController.getStoreFoodLicenceStatus?id={}", id);
        return R.data(licenseService.getStoreFoodLicenceStatus(id));
    }


    @ApiOperation(value = "门店装修-续签合同")
    @PostMapping("/uploadRenewalContract")
    public R<String> uploadRenewalContract(@RequestBody List<StoreImg> storeImgList) {
        log.info("StoreInfoController.uploadRenewalContract?storeImgList={}", storeImgList);
        int num = licenseService.uploadRenewalContract(storeImgList);
        if (num <= 0) {
            return R.fail("续签合同图片添加失败");
        }
        return R.success("续签合同图片添加成功");
    }

    @ApiOperation(value = "门店装修-续签合同状态及合同图片")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "门店id", dataType = "int", paramType = "query", required = true)})
    @GetMapping("/getStoreContractStatus")
    public R<Map<String, Object>> getStoreContractStatus(int id) {
        log.info("StoreInfoController.getStoreContractStatus?id={}", id);
        return R.data(licenseService.getStoreContractStatus(id));
    }

    @ApiOperation("根据商户ID获取合同历史记录（按时间分组）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryContractByStatusList")
    public R<List<StoreLicenseHistoryDto>> queryContractByStatusList(@RequestParam("storeId") Integer storeId) {
        log.info("LicenseController.queryContractByStatusList: storeId={}", storeId);
        try {
            List<StoreLicenseHistoryDto> result = licenseService.queryContractByStatusList(storeId);
            return R.data(result);
        } catch (Exception e) {
            log.error("LicenseController.queryContractByStatusList ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取娱乐经营许可证图片信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Integer", value = "城市", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/queryEntertainmentLicenseList")
    public R<List<StoreImgDTO>> queryEntertainmentLicenseList(Integer id) {
        log.info("LicenseController.queryEntertainmentLicenseList?id={}", id);
        List<StoreImgDTO> result = licenseService.getEntertainmentLicenseList(id);
        return R.data(result);
    }


    @ApiOperation(value = "门店装修-修改娱乐经营许可证", notes = "许可证图片由前端直连AI审核，服务端不再同步审图")
    @PostMapping("/uploadEntertainmentLicence")
    public R<String> uploadEntertainmentLicence(@RequestBody StoreImg storeImg) {
        log.info("StoreInfoController.uploadEntertainmentLicence?storeImg={}", storeImg);
        if (storeImg == null) {
            return R.fail("参数不能为空");
        }
        // 许可证配图由前端直连 AI；此处不再调用 aiContentModerationUtil 审图，避免因服务端不审图而拦截上传
        int num = licenseService.uploadEntertainmentLicence(storeImg);
        if (num > 0) {
            WebAudit webAudit = new WebAudit();
            webAudit.setStoreInfoId(storeImg.getStoreId().toString());
            webAudit.setType("8");
            webAudit.setStatus("0");
            webAudit.setContent("娱乐经营许可证");
            webAuditMapper.insert(webAudit);
            return R.success("娱乐经营许可证图片添加成功");
        }
        return R.fail("娱乐经营许可证图片添加失败");
    }

    @ApiOperation("根据商户ID获取经营许可证历史记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/queryEntertainmentByStatusList")
    public R<List<StoreLicenseHistoryDto>> queryEntertainmentByStatusList(@RequestParam("storeId") Integer storeId) {
        log.info("LicenseController.queryLicenceByStatusList: storeId={}", storeId);
        try {
            List<StoreLicenseHistoryDto> result = licenseService.queryEntertainmentByStatusList(storeId);
            return R.data(result);
        } catch (Exception e) {
            log.error("LicenseController.queryLicenceByStatusList ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "门店装修-娱乐经营许可证状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "门店id", dataType = "int", paramType = "query", required = true)})
    @GetMapping("/queryStoreEntertainmentStatus")
    public R<Map<String, Object>> queryStoreEntertainmentStatus(int id) {
        log.info("StoreInfoController.getStoreFoodLicenceStatus?id={}", id);
        return R.data(licenseService.getStoreEntertainmentStatus(id));
    }


}



