package vehicle

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

type FeeConfigService struct{}

// CreateFeeConfig 创建收费配置
func (s *FeeConfigService) CreateFeeConfig(req request.FeeConfigCreate) error {
	// 检查车辆类型是否存在
	var vehicleType dao.VehicleType
	result := global.GVA_DB.First(&vehicleType, req.VehicleTypeID)
	if result.RowsAffected == 0 {
		return errors.New("车辆类型不存在")
	}

	// 检查车辆类型是否已存在收费配置
	var existingConfig dao.FeeConfig
	result = global.GVA_DB.Where("vehicle_type_id = ?", req.VehicleTypeID).First(&existingConfig)
	if result.RowsAffected > 0 {
		return errors.New("该车辆类型的收费配置已存在")
	}

	config := dao.FeeConfig{
		VehicleTypeID: req.VehicleTypeID,
		StartTime:     req.StartTime,
		StartFee:      req.StartFee,
		UnitTime:      req.UnitTime,
		UnitFee:       req.UnitFee,
		DailyMaxFee:   req.DailyMaxFee,
		IsVIPFree:     req.IsVIPFree,
		VIPDiscount:   req.VIPDiscount,
		Description:   req.Description,
	}

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

// UpdateFeeConfig 更新收费配置
func (s *FeeConfigService) UpdateFeeConfig(req request.FeeConfigUpdate) error {
	var config dao.FeeConfig
	result := global.GVA_DB.First(&config, req.ID)
	if result.RowsAffected == 0 {
		return errors.New("收费配置不存在")
	}

	// 检查车辆类型是否存在
	var vehicleType dao.VehicleType
	result = global.GVA_DB.First(&vehicleType, req.VehicleTypeID)
	if result.RowsAffected == 0 {
		return errors.New("车辆类型不存在")
	}

	// 检查车辆类型是否被其他配置使用
	var existingConfig dao.FeeConfig
	result = global.GVA_DB.Where("vehicle_type_id = ? AND id != ?", req.VehicleTypeID, req.ID).First(&existingConfig)
	if result.RowsAffected > 0 {
		return errors.New("该车辆类型的收费配置已存在")
	}

	config.VehicleTypeID = req.VehicleTypeID
	config.StartTime = req.StartTime
	config.StartFee = req.StartFee
	config.UnitTime = req.UnitTime
	config.UnitFee = req.UnitFee
	config.DailyMaxFee = req.DailyMaxFee
	config.IsVIPFree = req.IsVIPFree
	config.VIPDiscount = req.VIPDiscount
	config.Description = req.Description

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

// GetFeeConfigByID 根据ID获取收费配置
func (s *FeeConfigService) GetFeeConfigByID(id uint) (dao.FeeConfig, error) {
	var config dao.FeeConfig
	result := global.GVA_DB.First(&config, id)
	if result.RowsAffected == 0 {
		return dao.FeeConfig{}, errors.New("收费配置不存在")
	}
	return config, nil
}

// GetFeeConfigByVehicleType 根据车辆类型ID获取收费配置
func (s *FeeConfigService) GetFeeConfigByVehicleType(vehicleTypeID uint) (dao.FeeConfig, error) {
	var config dao.FeeConfig
	result := global.GVA_DB.Where("vehicle_type_id = ?", vehicleTypeID).First(&config)
	if result.RowsAffected == 0 {
		return dao.FeeConfig{}, errors.New("收费配置不存在")
	}
	return config, nil
}

// ListFeeConfigs 获取收费配置列表
func (s *FeeConfigService) ListFeeConfigs(req request.FeeConfigQuery) (int64, []dao.FeeConfig, error) {
	var configs []dao.FeeConfig
	var total int64

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

	if req.VehicleTypeID > 0 {
		query = query.Where("vehicle_type_id = ?", req.VehicleTypeID)
	}

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

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

	return total, configs, nil
}

// DeleteFeeConfig 删除收费配置
func (s *FeeConfigService) DeleteFeeConfig(id uint) error {
	result := global.GVA_DB.Delete(&dao.FeeConfig{}, id)
	if result.RowsAffected == 0 {
		return errors.New("收费配置不存在")
	}
	return result.Error
}
