package com.jeeplus.hotelorder.hotelconfig.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.hotelorder.hotelconfig.domain.HotelBusinessHours;
import com.jeeplus.hotelorder.hotelconfig.domain.HotelConfig;
import com.jeeplus.hotelorder.hotelconfig.service.HotelConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 酒店配置管理Controller
 * @author jeeplus
 */
@Api(tags = "酒店配置管理")
@RestController
@RequestMapping(value = "/config")
public class HotelConfigController {

    @Resource
    private HotelConfigService hotelConfigService;

    /**
     * 获取酒店配置
     * @return 酒店配置信息
     */
    @ApiOperation(value = "获取酒店配置")
    @GetMapping("get")
    public ResponseEntity<HotelConfig> getConfig() {
        HotelConfig config = hotelConfigService.getConfig();
        return ResponseEntity.ok(config);
    }

    /**
     * 保存/更新酒店配置
     * @param config 酒店配置信息
     * @return 操作结果
     */
    @ApiOperation(value = "保存/更新酒店配置")
    @PostMapping("save")
    public ResponseEntity<String> saveConfig(@RequestBody HotelConfig config) {
        String result = hotelConfigService.saveConfig(config);
        return ResponseEntity.ok(result);
    }

    // ==================== 营业时段管理 ====================

    /**
     * 分页查询营业时段
     * @param businessHours 查询条件
     * @param page 分页参数
     * @return 分页结果
     * @throws Exception 查询异常
     */
    @ApiOperation(value = "分页查询营业时段")
    @GetMapping("businessHours/list")
    public ResponseEntity<IPage<HotelBusinessHours>> businessHoursList(HotelBusinessHours businessHours, Page<HotelBusinessHours> page) throws Exception {
        IPage<HotelBusinessHours> result = hotelConfigService.findBusinessHoursPage(page, businessHours);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取营业时段列表（全部，含禁用）
     * @return 营业时段列表
     */
    @ApiOperation(value = "获取营业时段列表（全部）")
    @GetMapping("businessHours/all")
    public ResponseEntity<List<HotelBusinessHours>> getBusinessHoursList() {
        List<HotelBusinessHours> list = hotelConfigService.getBusinessHoursList();
        return ResponseEntity.ok(list);
    }

    /**
     * 获取启用的营业时段（顾客端用）
     * @return 启用的营业时段列表
     */
    @ApiOperation(value = "获取启用的营业时段")
    @GetMapping("businessHours/enabled")
    public ResponseEntity<List<HotelBusinessHours>> getEnabledBusinessHoursList() {
        List<HotelBusinessHours> list = hotelConfigService.getEnabledBusinessHoursList();
        return ResponseEntity.ok(list);
    }

    /**
     * 检查当前是否在营业时段内（顾客端用）
     * @return 当前营业时段信息，不在营业时段则返回null
     */
    @ApiOperation(value = "检查当前营业状态")
    @GetMapping("businessHours/checkCurrent")
    public ResponseEntity<HotelBusinessHours> checkCurrentBusinessHours() {
        HotelBusinessHours current = hotelConfigService.checkCurrentBusinessHours();
        return ResponseEntity.ok(current);
    }

    /**
     * 查询营业时段详情
     * @param id 营业时段ID
     * @return 营业时段信息
     */
    @ApiOperation(value = "查询营业时段详情")
    @GetMapping("businessHours/queryById")
    public ResponseEntity<HotelBusinessHours> queryBusinessHoursById(@RequestParam("id") String id) {
        HotelBusinessHours hours = hotelConfigService.getBusinessHoursById(id);
        return ResponseEntity.ok(hours);
    }

    /**
     * 保存营业时段（新增/编辑）
     * @param hours 营业时段信息
     * @return 操作结果
     */
    @ApiOperation(value = "保存营业时段")
    @PostMapping("businessHours/save")
    public ResponseEntity<String> saveBusinessHours(@RequestBody HotelBusinessHours hours) {
        String result = hotelConfigService.saveBusinessHours(hours);
        return ResponseEntity.ok(result);
    }

    /**
     * 切换营业时段状态（启用/禁用）
     * @param id 营业时段ID
     * @return 操作结果
     */
    @ApiOperation(value = "切换营业时段状态")
    @PostMapping("businessHours/toggle/{id}")
    public ResponseEntity<String> toggleBusinessHours(@PathVariable String id) {
        String result = hotelConfigService.toggleBusinessHours(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除营业时段
     * @param id 营业时段ID
     * @return 操作结果
     */
    @ApiOperation(value = "删除营业时段")
    @PostMapping("businessHours/delete/{id}")
    public ResponseEntity<String> deleteBusinessHours(@PathVariable String id) {
        hotelConfigService.deleteBusinessHours(id);
        return ResponseEntity.ok("删除成功");
    }
}
