// Package customer 提供客户管理的业务逻辑服务
// 包含客户信息管理、客户查询等功能
package customer

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/internal/store"
	"github.com/break/junhong_cmp_fiber/internal/store/postgres"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
)

// Service 个人客户业务服务
type Service struct {
	customerStore *postgres.PersonalCustomerStore
}

// New 创建个人客户服务
func New(customerStore *postgres.PersonalCustomerStore) *Service {
	return &Service{
		customerStore: customerStore,
	}
}

// Create 创建个人客户
func (s *Service) Create(ctx context.Context, req *dto.CreatePersonalCustomerRequest) (*model.PersonalCustomer, error) {
	// 检查手机号唯一性
	if req.Phone != "" {
		existing, err := s.customerStore.GetByPhone(ctx, req.Phone)
		if err == nil && existing != nil {
			return nil, errors.New(errors.CodeCustomerPhoneExists, "手机号已存在")
		}
	}

	// 创建个人客户
	// 注意：根据新的数据模型，手机号应该存储在 PersonalCustomerPhone 表中
	// 这里暂时先创建客户记录，手机号的存储后续通过 PersonalCustomerPhoneStore 实现
	customer := &model.PersonalCustomer{
		Nickname:  req.Nickname,
		AvatarURL: req.AvatarURL,
		WxOpenID:  req.WxOpenID,
		WxUnionID: req.WxUnionID,
		Status:    constants.StatusEnabled,
	}

	if err := s.customerStore.Create(ctx, customer); err != nil {
		return nil, err
	}

	// TODO: 创建 PersonalCustomerPhone 记录，需要通过 PersonalCustomerPhoneStore 创建手机号关联

	return customer, nil
}

// Update 更新个人客户信息
func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdatePersonalCustomerRequest) (*model.PersonalCustomer, error) {
	// 查询客户
	customer, err := s.customerStore.GetByID(ctx, id)
	if err != nil {
		return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
	}

	// TODO: 手机号的更新逻辑需要通过 PersonalCustomerPhoneStore 更新或创建手机号记录

	// 更新字段
	if req.Nickname != nil {
		customer.Nickname = *req.Nickname
	}
	if req.AvatarURL != nil {
		customer.AvatarURL = *req.AvatarURL
	}

	if err := s.customerStore.Update(ctx, customer); err != nil {
		return nil, err
	}

	return customer, nil
}

// BindWeChat 绑定微信信息
func (s *Service) BindWeChat(ctx context.Context, id uint, wxOpenID, wxUnionID string) error {
	customer, err := s.customerStore.GetByID(ctx, id)
	if err != nil {
		return errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
	}

	customer.WxOpenID = wxOpenID
	customer.WxUnionID = wxUnionID

	return s.customerStore.Update(ctx, customer)
}

// GetByID 获取个人客户详情
func (s *Service) GetByID(ctx context.Context, id uint) (*model.PersonalCustomer, error) {
	customer, err := s.customerStore.GetByID(ctx, id)
	if err != nil {
		return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
	}
	return customer, nil
}

// GetByPhone 根据手机号获取个人客户
func (s *Service) GetByPhone(ctx context.Context, phone string) (*model.PersonalCustomer, error) {
	customer, err := s.customerStore.GetByPhone(ctx, phone)
	if err != nil {
		return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
	}
	return customer, nil
}

// GetByWxOpenID 根据微信 OpenID 获取个人客户
func (s *Service) GetByWxOpenID(ctx context.Context, wxOpenID string) (*model.PersonalCustomer, error) {
	customer, err := s.customerStore.GetByWxOpenID(ctx, wxOpenID)
	if err != nil {
		return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
	}
	return customer, nil
}

// List 查询个人客户列表
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.PersonalCustomer, int64, error) {
	return s.customerStore.List(ctx, opts, filters)
}
