package parking

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

type boothService struct{}

func NewBoothService() *boothService {
	return &boothService{}
}

// CreateBooth 创建岗亭
func (s *boothService) CreateBooth(req request.BoothCreate) error {
	// 检查岗亭编码是否已存在
	var existingBooth dao.Booth
	result := global.GVA_DB.Where("booth_code = ?", req.BoothCode).First(&existingBooth)
	if result.RowsAffected > 0 {
		return errors.New("岗亭编码已存在")
	}

	booth := dao.Booth{
		BoothCode:    req.BoothCode,
		BoothName:    req.BoothName,
		IPAddress:    req.IPAddress,
		Description:  req.Description,
		ParkingLotID: req.ParkingLotID,
	}

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

// UpdateBooth 更新岗亭
func (s *boothService) UpdateBooth(req request.BoothUpdate) error {
	var booth dao.Booth
	result := global.GVA_DB.Preload("Channels").Preload("Channels.UHFReaders").First(&booth, req.ID)
	if result.RowsAffected == 0 {
		return errors.New("岗亭不存在")
	}

	// 检查岗亭编码是否被其他岗亭使用
	var existingBooth dao.Booth
	result = global.GVA_DB.Where("booth_code = ? AND id != ?", req.BoothCode, req.ID).First(&existingBooth)
	if result.RowsAffected > 0 {
		return errors.New("岗亭编码已被其他岗亭使用")
	}

	booth.BoothCode = req.BoothCode
	booth.BoothName = req.BoothName
	booth.IPAddress = req.IPAddress
	booth.Description = req.Description
	booth.ParkingLotID = req.ParkingLotID

	// 修改岗亭的区域id，那么它下面的子级关系的区域id都要改
	for _, channel := range booth.Channels {
		global.GVA_DB.Model(&channel).Where("id = ?", channel.ID).Update("parking_lot_id", req.ParkingLotID)
		for _, reader := range channel.UHFReaders {
			global.GVA_DB.Model(&reader).Where("id = ?", channel.ID).Update("parking_lot_id", req.ParkingLotID)
		}
	}

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

// GetBoothByID 根据ID获取岗亭
func (s *boothService) GetBoothByID(id uint) (dao.Booth, error) {
	var booth dao.Booth
	result := global.GVA_DB.First(&booth, id)
	if result.RowsAffected == 0 {
		return dao.Booth{}, errors.New("岗亭不存在")
	}
	return booth, nil
}

// GetBoothByCode 根据编码获取岗亭
func (s *boothService) GetBoothByCode(code string) (dao.Booth, error) {
	var booth dao.Booth
	result := global.GVA_DB.Where("booth_code = ?", code).First(&booth)
	if result.RowsAffected == 0 {
		return dao.Booth{}, errors.New("岗亭不存在")
	}
	return booth, nil
}

func (s *boothService) QueryAllBooths() ([]dao.Booth, error) {
	var booths []dao.Booth
	err := global.GVA_DB.Find(&booths).Error
	if err != nil {
		return []dao.Booth{}, err
	}
	return booths, nil
}

// ListBooths 获取岗亭列表
func (s *boothService) ListBooths(req request.BoothQuery) (int64, []dao.Booth, error) {
	var booths []dao.Booth
	var total int64

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

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

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

	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).Preload("ParkingLot").Find(&booths)
	if result.Error != nil {
		return 0, nil, result.Error
	}

	return total, booths, nil
}

// DeleteBooth 删除岗亭
func (s *boothService) DeleteBooth(id uint) error {
	result := global.GVA_DB.Delete(&dao.Booth{}, id)
	if result.RowsAffected == 0 {
		return errors.New("岗亭不存在")
	}
	return result.Error
}
