package postgres

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/store"
	"github.com/break/junhong_cmp_fiber/pkg/middleware"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

// AccountStore 账号数据访问层
type AccountStore struct {
	db    *gorm.DB
	redis *redis.Client
}

// NewAccountStore 创建账号 Store
func NewAccountStore(db *gorm.DB, redis *redis.Client) *AccountStore {
	return &AccountStore{
		db:    db,
		redis: redis,
	}
}

// Create 创建账号
func (s *AccountStore) Create(ctx context.Context, account *model.Account) error {
	return s.db.WithContext(ctx).Create(account).Error
}

// GetByID 根据 ID 获取账号
func (s *AccountStore) GetByID(ctx context.Context, id uint) (*model.Account, error) {
	var account model.Account
	query := s.db.WithContext(ctx).Where("id = ?", id)
	// 根据当前用户类型应用数据权限过滤
	// 代理用户：过滤 shop_id；企业用户：过滤 enterprise_id
	query = middleware.ApplyShopFilter(ctx, query)
	query = middleware.ApplyEnterpriseFilter(ctx, query)
	if err := query.First(&account).Error; err != nil {
		return nil, err
	}
	return &account, nil
}

// GetByUsername 根据用户名获取账号
func (s *AccountStore) GetByUsername(ctx context.Context, username string) (*model.Account, error) {
	var account model.Account
	if err := s.db.WithContext(ctx).Where("username = ?", username).First(&account).Error; err != nil {
		return nil, err
	}
	return &account, nil
}

// GetByPhone 根据手机号获取账号
func (s *AccountStore) GetByPhone(ctx context.Context, phone string) (*model.Account, error) {
	var account model.Account
	if err := s.db.WithContext(ctx).Where("phone = ?", phone).First(&account).Error; err != nil {
		return nil, err
	}
	return &account, nil
}

// GetByUsernameOrPhone 根据用户名或手机号获取账号
func (s *AccountStore) GetByUsernameOrPhone(ctx context.Context, identifier string) (*model.Account, error) {
	var account model.Account
	if err := s.db.WithContext(ctx).Where("username = ? OR phone = ?", identifier, identifier).First(&account).Error; err != nil {
		return nil, err
	}
	return &account, nil
}

// GetByShopID 根据店铺 ID 查询账号列表
func (s *AccountStore) GetByShopID(ctx context.Context, shopID uint) ([]*model.Account, error) {
	var accounts []*model.Account
	query := s.db.WithContext(ctx).Where("shop_id = ?", shopID)
	// 应用数据权限过滤
	query = middleware.ApplyShopFilter(ctx, query)
	if err := query.Find(&accounts).Error; err != nil {
		return nil, err
	}
	return accounts, nil
}

// GetByEnterpriseID 根据企业 ID 查询账号列表
func (s *AccountStore) GetByEnterpriseID(ctx context.Context, enterpriseID uint) ([]*model.Account, error) {
	var accounts []*model.Account
	query := s.db.WithContext(ctx).Where("enterprise_id = ?", enterpriseID)
	// 应用企业数据权限过滤
	query = middleware.ApplyEnterpriseFilter(ctx, query)
	if err := query.Find(&accounts).Error; err != nil {
		return nil, err
	}
	return accounts, nil
}

// Update 更新账号
func (s *AccountStore) Update(ctx context.Context, account *model.Account) error {
	return s.db.WithContext(ctx).Save(account).Error
}

// BindWeCom 更新账号的企业微信成员绑定快照。
func (s *AccountStore) BindWeCom(ctx context.Context, accountID uint, corpID, userID, name string, updater uint) error {
	return s.db.WithContext(ctx).Model(&model.Account{}).Where("id = ?", accountID).Updates(map[string]any{
		"wecom_corp_id": corpID, "wecom_userid": userID, "wecom_name": name, "updater": updater,
	}).Error
}

// Delete 软删除账号
func (s *AccountStore) Delete(ctx context.Context, id uint) error {
	return s.db.WithContext(ctx).Delete(&model.Account{}, id).Error
}

// List 查询账号列表
func (s *AccountStore) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Account, int64, error) {
	var accounts []*model.Account
	var total int64

	query := s.db.WithContext(ctx).Model(&model.Account{})
	// 根据当前用户类型应用数据权限过滤
	// 代理用户：过滤 shop_id；企业用户：过滤 enterprise_id
	query = middleware.ApplyShopFilter(ctx, query)
	query = middleware.ApplyEnterpriseFilter(ctx, query)

	// 应用过滤条件
	if username, ok := filters["username"].(string); ok && username != "" {
		query = query.Where("username LIKE ?", "%"+username+"%")
	}
	if phone, ok := filters["phone"].(string); ok && phone != "" {
		query = query.Where("phone LIKE ?", "%"+phone+"%")
	}
	if userType, ok := filters["user_type"].(int); ok {
		query = query.Where("user_type = ?", userType)
	}
	if status, ok := filters["status"].(int); ok {
		query = query.Where("status = ?", status)
	}
	if shopID, ok := filters["shop_id"].(uint); ok && shopID > 0 {
		query = query.Where("shop_id = ?", shopID)
	}
	if enterpriseID, ok := filters["enterprise_id"].(uint); ok && enterpriseID > 0 {
		query = query.Where("enterprise_id = ?", enterpriseID)
	}

	// 计算总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 分页
	if opts == nil {
		opts = store.DefaultQueryOptions()
	}
	offset := (opts.Page - 1) * opts.PageSize
	query = query.Offset(offset).Limit(opts.PageSize)

	// 排序
	if opts.OrderBy != "" {
		query = query.Order(opts.OrderBy)
	}

	// 执行查询
	if err := query.Find(&accounts).Error; err != nil {
		return nil, 0, err
	}

	return accounts, total, nil
}

// ListPlatformAccounts 查询平台账号列表（自动筛选 user_type IN (1, 2)）
func (s *AccountStore) ListPlatformAccounts(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Account, int64, error) {
	var accounts []*model.Account
	var total int64

	query := s.db.WithContext(ctx).Model(&model.Account{})

	// 固定筛选平台账号：超级管理员(1) 和 平台用户(2)
	query = query.Where("user_type IN ?", []int{1, 2})

	// 应用过滤条件
	if username, ok := filters["username"].(string); ok && username != "" {
		query = query.Where("username LIKE ?", "%"+username+"%")
	}
	if phone, ok := filters["phone"].(string); ok && phone != "" {
		query = query.Where("phone LIKE ?", "%"+phone+"%")
	}
	if status, ok := filters["status"].(int); ok {
		query = query.Where("status = ?", status)
	}

	// 计算总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 分页
	if opts == nil {
		opts = store.DefaultQueryOptions()
	}
	offset := (opts.Page - 1) * opts.PageSize
	query = query.Offset(offset).Limit(opts.PageSize)

	// 排序
	if opts.OrderBy != "" {
		query = query.Order(opts.OrderBy)
	}

	// 执行查询
	if err := query.Find(&accounts).Error; err != nil {
		return nil, 0, err
	}

	return accounts, total, nil
}

// UpdatePassword 更新账号密码
func (s *AccountStore) UpdatePassword(ctx context.Context, id uint, hashedPassword string, updater uint) error {
	return s.db.WithContext(ctx).
		Model(&model.Account{}).
		Where("id = ?", id).
		Updates(map[string]interface{}{
			"password": hashedPassword,
			"updater":  updater,
		}).Error
}

// UpdateStatus 更新账号状态
func (s *AccountStore) UpdateStatus(ctx context.Context, id uint, status int, updater uint) error {
	return s.db.WithContext(ctx).
		Model(&model.Account{}).
		Where("id = ?", id).
		Updates(map[string]interface{}{
			"status":  status,
			"updater": updater,
		}).Error
}

// BulkUpdateStatus 批量更新账号状态
func (s *AccountStore) BulkUpdateStatus(ctx context.Context, ids []uint, status int, updater uint) error {
	return s.db.WithContext(ctx).
		Model(&model.Account{}).
		Where("id IN ?", ids).
		Updates(map[string]interface{}{
			"status":  status,
			"updater": updater,
		}).Error
}

func (s *AccountStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Account, error) {
	if len(ids) == 0 {
		return []*model.Account{}, nil
	}
	var accounts []*model.Account
	query := s.db.WithContext(ctx).Where("id IN ?", ids)
	// 根据当前用户类型应用数据权限过滤
	query = middleware.ApplyShopFilter(ctx, query)
	query = middleware.ApplyEnterpriseFilter(ctx, query)
	if err := query.Find(&accounts).Error; err != nil {
		return nil, err
	}
	return accounts, nil
}

// GetDisplayAccountsByIDs 批量读取历史业务记录的账号展示信息。
// 该查询只返回 ID 和用户名，包含已软删除账号，不用于授权判定。
func (s *AccountStore) GetDisplayAccountsByIDs(ctx context.Context, ids []uint) ([]*model.Account, error) {
	if len(ids) == 0 {
		return []*model.Account{}, nil
	}
	var accounts []*model.Account
	if err := s.db.WithContext(ctx).Unscoped().
		Select("id", "username").
		Where("id IN ?", ids).
		Find(&accounts).Error; err != nil {
		return nil, err
	}
	return accounts, nil
}

func (s *AccountStore) GetPrimaryAccountsByShopIDs(ctx context.Context, shopIDs []uint) ([]*model.Account, error) {
	if len(shopIDs) == 0 {
		return []*model.Account{}, nil
	}
	var accounts []*model.Account
	query := s.db.WithContext(ctx).
		Where("shop_id IN ? AND is_primary = ?", shopIDs, true)
	// 注意：此处不再应用数据权限过滤
	// 因为调用方（Service 层）已经保证了 shopIDs 的合法性
	// 在资金概况等批量投影场景中，调用方已经保证 shopIDs 位于当前数据范围内
	if err := query.Find(&accounts).Error; err != nil {
		return nil, err
	}
	return accounts, nil
}

// ListByShopID 按店铺ID分页查询账号列表
func (s *AccountStore) ListByShopID(ctx context.Context, shopID uint, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Account, int64, error) {
	var accounts []*model.Account
	var total int64

	query := s.db.WithContext(ctx).Model(&model.Account{}).Where("shop_id = ?", shopID)
	// 应用数据权限过滤
	query = middleware.ApplyShopFilter(ctx, query)

	if username, ok := filters["username"].(string); ok && username != "" {
		query = query.Where("username LIKE ?", "%"+username+"%")
	}
	if phone, ok := filters["phone"].(string); ok && phone != "" {
		query = query.Where("phone LIKE ?", "%"+phone+"%")
	}
	if status, ok := filters["status"].(int); ok {
		query = query.Where("status = ?", status)
	}

	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	if opts == nil {
		opts = store.DefaultQueryOptions()
	}
	offset := (opts.Page - 1) * opts.PageSize
	query = query.Offset(offset).Limit(opts.PageSize)

	if opts.OrderBy != "" {
		query = query.Order(opts.OrderBy)
	}

	if err := query.Find(&accounts).Error; err != nil {
		return nil, 0, err
	}

	return accounts, total, nil
}
