package parking

import (
	"errors"
	"wails-app/internal/dao"
	"wails-app/internal/global"
	"wails-app/internal/model/parking/request"
)

type channelService struct{}

func NewChannelService() *channelService {
	return &channelService{}
}

// CreateChannel 创建通道
func (s *channelService) CreateChannel(req request.ChannelCreate) error {
	// 检查通道编码是否已存在
	var existingChannel dao.Channel
	result := global.GVA_DB.Where("channel_code = ?", req.ChannelCode).First(&existingChannel)
	if result.RowsAffected > 0 {
		return errors.New("通道编码已存在")
	}

	var booth dao.Booth
	global.GVA_DB.Model(&booth).Where("id = ?", req.BoothId).First(&booth)

	channel := dao.Channel{
		ChannelCode:    req.ChannelCode,
		ChannelName:    req.ChannelName,
		Direction:      req.Direction,
		AllowTemporary: req.AllowTemporary,
		Description:    req.Description,
		BoothID:        req.BoothId,
		ParkingLotID:   booth.ParkingLotID,
	}

	return global.GVA_DB.Create(&channel).Error
}

// UpdateChannel 更新通道
func (s *channelService) UpdateChannel(req request.ChannelUpdate) error {
	var channel dao.Channel
	result := global.GVA_DB.First(&channel, req.ID)
	if result.RowsAffected == 0 {
		return errors.New("通道不存在")
	}

	// 检查通道编码是否被其他通道使用
	var existingChannel dao.Channel
	result = global.GVA_DB.Where("channel_code = ? AND id != ?", req.ChannelCode, req.ID).First(&existingChannel)
	if result.RowsAffected > 0 {
		return errors.New("通道编码已被其他通道使用")
	}

	channel.ChannelCode = req.ChannelCode
	channel.ChannelName = req.ChannelName
	channel.Direction = req.Direction
	channel.AllowTemporary = req.AllowTemporary
	channel.Description = req.Description
	channel.BoothID = req.BoothId

	return global.GVA_DB.Save(&channel).Error
}

// GetChannelByID 根据ID获取通道
func (s *channelService) GetChannelByID(id uint) (dao.Channel, error) {
	var channel dao.Channel
	result := global.GVA_DB.First(&channel, id)
	if result.RowsAffected == 0 {
		return dao.Channel{}, errors.New("通道不存在")
	}
	return channel, nil
}

// GetChannelByCode 根据编码获取通道
func (s *channelService) GetChannelByCode(code string) (dao.Channel, error) {
	var channel dao.Channel
	result := global.GVA_DB.Where("channel_code = ?", code).First(&channel)
	if result.RowsAffected == 0 {
		return dao.Channel{}, errors.New("通道不存在")
	}
	return channel, nil
}

func (s *channelService) QueryAllChannels() ([]dao.Channel, error) {
	var channels []dao.Channel
	err := global.GVA_DB.Find(&channels).Error
	if err != nil {
		return channels, err
	}
	return channels, nil
}

// ListChannels 获取通道列表
func (s *channelService) ListChannels(req request.ChannelQuery) (int64, []dao.Channel, error) {
	var channels []dao.Channel
	var total int64

	query := global.GVA_DB.Model(&dao.Channel{})

	if req.ChannelCode != "" {
		query = query.Where("channel_code LIKE ?", "%"+req.ChannelCode+"%")
	}

	if req.ChannelName != "" {
		query = query.Where("channel_name LIKE ?", "%"+req.ChannelName+"%")
	}

	if req.Direction != "" {
		query = query.Where("direction = ?", req.Direction)
	}

	if req.ParkingLotID != 0 {
		query = query.Where("parking_lot_id = ?", req.ParkingLotID)
	}

	// 计算总数
	query.Count(&total)

	// 分页查询
	offset := (req.Page - 1) * req.PageSize
	result := query.Offset(offset).Limit(req.PageSize).Find(&channels)
	if result.Error != nil {
		return 0, nil, result.Error
	}

	return total, channels, nil
}

// DeleteChannel 删除通道
func (s *channelService) DeleteChannel(id uint) error {
	result := global.GVA_DB.Delete(&dao.Channel{}, id)
	if result.RowsAffected == 0 {
		return errors.New("通道不存在")
	}
	return result.Error
}
