package postgres

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"gorm.io/gorm"
)

// PersonalCustomerPhoneStore 个人客户手机号数据访问层
type PersonalCustomerPhoneStore struct {
	db *gorm.DB
}

// NewPersonalCustomerPhoneStore 创建个人客户手机号 Store
func NewPersonalCustomerPhoneStore(db *gorm.DB) *PersonalCustomerPhoneStore {
	return &PersonalCustomerPhoneStore{
		db: db,
	}
}

// Create 创建手机号绑定记录
func (s *PersonalCustomerPhoneStore) Create(ctx context.Context, phone *model.PersonalCustomerPhone) error {
	return s.db.WithContext(ctx).Create(phone).Error
}

// GetByCustomerID 根据客户 ID 获取所有手机号
func (s *PersonalCustomerPhoneStore) GetByCustomerID(ctx context.Context, customerID uint) ([]*model.PersonalCustomerPhone, error) {
	var phones []*model.PersonalCustomerPhone
	if err := s.db.WithContext(ctx).
		Where("customer_id = ?", customerID).
		Order("is_primary DESC, created_at DESC").
		Find(&phones).Error; err != nil {
		return nil, err
	}
	return phones, nil
}

// GetPrimaryPhone 获取客户的主手机号
func (s *PersonalCustomerPhoneStore) GetPrimaryPhone(ctx context.Context, customerID uint) (*model.PersonalCustomerPhone, error) {
	var phone model.PersonalCustomerPhone
	if err := s.db.WithContext(ctx).
		Where("customer_id = ? AND is_primary = ? AND status = ?", customerID, true, 1).
		First(&phone).Error; err != nil {
		return nil, err
	}
	return &phone, nil
}

// GetByPhone 根据手机号查询绑定记录
func (s *PersonalCustomerPhoneStore) GetByPhone(ctx context.Context, phone string) (*model.PersonalCustomerPhone, error) {
	var record model.PersonalCustomerPhone
	if err := s.db.WithContext(ctx).
		Where("phone = ? AND status = ?", phone, 1).
		First(&record).Error; err != nil {
		return nil, err
	}
	return &record, nil
}

// SetPrimary 设置主手机号
// 将指定的手机号设置为主号，同时将该客户的其他手机号设置为非主号
func (s *PersonalCustomerPhoneStore) SetPrimary(ctx context.Context, id uint, customerID uint) error {
	return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
		// 将该客户的所有手机号设置为非主号
		if err := tx.Model(&model.PersonalCustomerPhone{}).
			Where("customer_id = ?", customerID).
			Update("is_primary", false).Error; err != nil {
			return err
		}

		// 将指定的手机号设置为主号
		if err := tx.Model(&model.PersonalCustomerPhone{}).
			Where("id = ? AND customer_id = ?", id, customerID).
			Update("is_primary", true).Error; err != nil {
			return err
		}

		return nil
	})
}

// UpdateStatus 更新手机号状态
func (s *PersonalCustomerPhoneStore) UpdateStatus(ctx context.Context, id uint, status int) error {
	return s.db.WithContext(ctx).
		Model(&model.PersonalCustomerPhone{}).
		Where("id = ?", id).
		Update("status", status).Error
}

// Delete 软删除手机号
func (s *PersonalCustomerPhoneStore) Delete(ctx context.Context, id uint) error {
	return s.db.WithContext(ctx).Delete(&model.PersonalCustomerPhone{}, id).Error
}

// ExistsByPhone 检查手机号是否已被绑定
func (s *PersonalCustomerPhoneStore) ExistsByPhone(ctx context.Context, phone string) (bool, error) {
	var count int64
	if err := s.db.WithContext(ctx).
		Model(&model.PersonalCustomerPhone{}).
		Where("phone = ? AND status = ?", phone, 1).
		Count(&count).Error; err != nil {
		return false, err
	}
	return count > 0, nil
}

// ExistsByCustomerAndPhone 检查某个客户是否已绑定该手机号
func (s *PersonalCustomerPhoneStore) ExistsByCustomerAndPhone(ctx context.Context, customerID uint, phone string) (bool, error) {
	var count int64
	if err := s.db.WithContext(ctx).
		Model(&model.PersonalCustomerPhone{}).
		Where("customer_id = ? AND phone = ? AND status = ?", customerID, phone, 1).
		Count(&count).Error; err != nil {
		return false, err
	}
	return count > 0, nil
}
