package owner

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

type ownerService struct{}

func NewOwnerService() *ownerService {
	return &ownerService{}
}

// CreateOwner 创建车主信息
func (s *ownerService) CreateOwner(req request.OwnerCreate) error {
	// 检查手机号是否已存在
	var existingOwner dao.Owner
	result := global.GVA_DB.Where("owner_phone = ?", req.OwnerPhone).First(&existingOwner)
	if result.RowsAffected > 0 {
		return errors.New("手机号已存在")
	}

	owner := dao.Owner{
		OwnerName:     req.OwnerName,
		OwnerSurname:  req.OwnerSurname,
		OwnerPhone:    req.OwnerPhone,
		IsVip:         req.IsVip,
		VipExpireTime: req.VipExpireTime,
	}

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

// UpdateOwner 更新车主信息
func (s *ownerService) UpdateOwner(req request.OwnerUpdate) error {
	var owner dao.Owner
	result := global.GVA_DB.First(&owner, req.ID)
	if result.RowsAffected == 0 {
		return errors.New("车主不存在")
	}

	// 检查手机号是否被其他车主使用
	var existingOwner dao.Owner
	result = global.GVA_DB.Where("owner_phone = ? AND id != ?", req.OwnerPhone, req.ID).First(&existingOwner)
	if result.RowsAffected > 0 {
		return errors.New("手机号已被其他车主使用")
	}

	owner.OwnerName = req.OwnerName
	owner.OwnerSurname = req.OwnerSurname
	owner.OwnerPhone = req.OwnerPhone
	owner.IsVip = req.IsVip
	owner.VipExpireTime = req.VipExpireTime

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

// GetOwnerByID 根据ID获取车主信息
func (s *ownerService) GetOwnerByID(id uint) (dao.Owner, error) {
	var owner dao.Owner
	result := global.GVA_DB.First(&owner, id)
	if result.RowsAffected == 0 {
		return dao.Owner{}, errors.New("车主不存在")
	}
	return owner, nil
}

// GetOwnerByPhone 根据手机号获取车主信息
func (s *ownerService) GetOwnerByPhone(phone string) ([]dao.Owner, error) {
	var ownerList []dao.Owner
	result := global.GVA_DB.Where("owner_phone LIKE ?", "%"+phone+"%").Find(&ownerList)
	fmt.Println(result)
	//if result.RowsAffected == 0 {
	//	return dao.Owner{}, errors.New("车主不存在")
	//}
	return ownerList, nil
}

// ListOwners 获取车主列表
func (s *ownerService) ListOwners(req request.OwnerQuery) (int64, []dao.Owner, error) {
	var owners []dao.Owner
	var total int64

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

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

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

	if req.IsVip != nil {
		query = query.Where("is_vip = ?", *req.IsVip)
	}

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

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

	return total, owners, nil
}

// DeleteOwner 删除车主
func (s *ownerService) DeleteOwner(id uint) error {
	// 检查车主是否存在
	var owner dao.Owner
	result := global.GVA_DB.First(&owner, id)
	if result.RowsAffected == 0 {
		return errors.New("车主不存在")
	}

	// 检查车主是否有车辆
	var vehicleCount int64
	global.GVA_DB.Model(&dao.Vehicle{}).Where("owner_id = ?", id).Count(&vehicleCount)
	if vehicleCount > 0 {
		return errors.New("车主有车辆，无法删除")
	}

	return global.GVA_DB.Delete(&owner, id).Error
}
