package com.hrs.service.impl;

import com.hrs.dao.HouseDao;
import com.hrs.dao.ReservationDao;
import com.hrs.dao.impl.HouseDaoImpl;
import com.hrs.dao.impl.ReservationDaoImpl;
import com.hrs.model.entity.House;
import com.hrs.model.entity.Reservation;
import com.hrs.service.ReservationService;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: ReservationServiceImpl.java
* @Description: 预约看房业务逻辑实现类
*
* @version: v1.0.0
* @author: 黄红
* @date: 2026年4月13日 上午10:06:14 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     黄红           v1.0.0               新建文件
*/
public class ReservationServiceImpl implements ReservationService {

    private ReservationDao reservationDao = new ReservationDaoImpl();
    private HouseDao houseDao = new HouseDaoImpl();

    /**
     * 创建预约
     */
    @Override
    public ReservationResult createReservation(Long tenantId, Long houseId, Date reserveTime, String remark) {
        ReservationResult result = new ReservationResult();

        // 1. 参数校验
        if (tenantId == null || tenantId <= 0) {
            result.setSuccess(false);
            result.setMessage("用户未登录");
            return result;
        }

        if (houseId == null || houseId <= 0) {
            result.setSuccess(false);
            result.setMessage("房源信息错误");
            return result;
        }

        if (reserveTime == null) {
            result.setSuccess(false);
            result.setMessage("请选择预约时间");
            return result;
        }

        // 2. 检查房源是否存在且已上架
        House house = houseDao.findById(houseId);
        if (house == null) {
            result.setSuccess(false);
            result.setMessage("房源不存在");
            return result;
        }

        if (!"1".equals(house.getStatus())) {
            result.setSuccess(false);
            result.setMessage("房源已下架，无法预约");
            return result;
        }

        // 3. 检查预约时间（不能早于当前时间1小时）
        Date now = new Date();
        long minReserveTime = now.getTime() + TimeUnit.HOURS.toMillis(1);
        if (reserveTime.getTime() < minReserveTime) {
            result.setSuccess(false);
            result.setMessage("预约时间至少需要提前1小时");
            return result;
        }

        // 4. 创建预约
        Reservation reservation = new Reservation();
        reservation.setTenantId(tenantId);
        reservation.setHouseId(houseId);
        reservation.setLandlordId(house.getLandlordId());
        reservation.setReserveTime(reserveTime);
        reservation.setRemark(remark);

        boolean success = reservationDao.add(reservation);
        result.setSuccess(success);
        result.setMessage(success ? "预约成功，请等待房东确认" : "预约失败，请稍后重试");

        return result;
    }

    /**
     * 获取租客的预约列表
     */
    @Override
    public List<Reservation> getTenantReservations(Long tenantId) {
        if (tenantId == null || tenantId <= 0) {
            return null;
        }
        return reservationDao.findByTenantId(tenantId);
    }

    /**
     * 获取房东收到的预约列表
     */
    @Override
    public List<Reservation> getLandlordReservations(Long landlordId) {
        if (landlordId == null || landlordId <= 0) {
            return null;
        }
        return reservationDao.findByLandlordId(landlordId);
    }

    /**
     * 租客取消预约
     */
    @Override
    public boolean cancelReservationByTenant(Long reservationId, Long tenantId) {
        if (reservationId == null || tenantId == null) {
            return false;
        }

        Reservation reservation = reservationDao.findById(reservationId);
        if (reservation == null || !reservation.getTenantId().equals(tenantId)) {
            return false;
        }

        // 只有待确认的预约可以取消
        if (!"0".equals(reservation.getStatus())) {
            return false;
        }

        return reservationDao.cancel(reservationId, tenantId);
    }

    /**
     * 房东处理预约（确认/拒绝）
     */
    @Override
    public boolean handleReservation(Long reservationId, Long landlordId, boolean approve) {
        if (reservationId == null || landlordId == null) {
            return false;
        }

        Reservation reservation = reservationDao.findById(reservationId);
        if (reservation == null || !reservation.getLandlordId().equals(landlordId)) {
            return false;
        }

        // 只有待确认的预约可以处理
        if (!"0".equals(reservation.getStatus())) {
            return false;
        }

        String status = approve ? "1" : "2";  // 1-已确认，2-已取消/拒绝
        return reservationDao.updateStatus(reservationId, status);
    }

    /**
     * 获取预约详情
     */
    @Override
    public Reservation getReservationDetail(Long reservationId) {
        if (reservationId == null || reservationId <= 0) {
            return null;
        }
        return reservationDao.findById(reservationId);
    }

    /**
     * 获取房东待处理预约数量
     */
    @Override
    public int getPendingCount(Long landlordId) {
        if (landlordId == null || landlordId <= 0) {
            return 0;
        }
        return reservationDao.countByLandlordId(landlordId);
    }
}
