package postgres

import (
	"context"
	"time"

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

// AssetWalletTransactionStore 资产钱包交易记录数据访问层
type AssetWalletTransactionStore struct {
	db    *gorm.DB
	redis *redis.Client
}

// NewAssetWalletTransactionStore 创建资产钱包交易记录 Store
func NewAssetWalletTransactionStore(db *gorm.DB, redis *redis.Client) *AssetWalletTransactionStore {
	return &AssetWalletTransactionStore{
		db:    db,
		redis: redis,
	}
}

// CreateWithTx 创建资产钱包交易记录（带事务）
func (s *AssetWalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.AssetWalletTransaction) error {
	return tx.WithContext(ctx).Create(transaction).Error
}

// ListByResourceID 按资源查询交易记录（支持分页）
func (s *AssetWalletTransactionStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.AssetWalletTransaction, error) {
	var transactions []*model.AssetWalletTransaction
	query := s.db.WithContext(ctx).
		Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
	// 应用数据权限过滤（使用 shop_id_tag 字段）
	query = middleware.ApplyShopTagFilter(ctx, query)
	err := query.Order("created_at DESC").
		Offset(offset).
		Limit(limit).
		Find(&transactions).Error
	if err != nil {
		return nil, err
	}
	return transactions, nil
}

// CountByResourceID 统计资源的交易记录数量
func (s *AssetWalletTransactionStore) CountByResourceID(ctx context.Context, resourceType string, resourceID uint) (int64, error) {
	var count int64
	query := s.db.WithContext(ctx).
		Model(&model.AssetWalletTransaction{}).
		Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
	// 应用数据权限过滤（使用 shop_id_tag 字段）
	query = middleware.ApplyShopTagFilter(ctx, query)
	err := query.Count(&count).Error
	return count, err
}

// ListByWalletID 按钱包查询交易记录（支持分页）
func (s *AssetWalletTransactionStore) ListByWalletID(ctx context.Context, walletID uint, offset, limit int) ([]*model.AssetWalletTransaction, error) {
	var transactions []*model.AssetWalletTransaction
	query := s.db.WithContext(ctx).
		Where("asset_wallet_id = ?", walletID)
	// 应用数据权限过滤（使用 shop_id_tag 字段）
	query = middleware.ApplyShopTagFilter(ctx, query)
	err := query.Order("created_at DESC").
		Offset(offset).
		Limit(limit).
		Find(&transactions).Error
	if err != nil {
		return nil, err
	}
	return transactions, nil
}

// ListByResourceIDWithFilter 按资源查询交易记录（支持 transaction_type 和时间范围过滤，分页）
func (s *AssetWalletTransactionStore) ListByResourceIDWithFilter(ctx context.Context, resourceType string, resourceID uint, transactionType *string, startTime, endTime *time.Time, offset, limit int) ([]*model.AssetWalletTransaction, error) {
	var transactions []*model.AssetWalletTransaction
	query := s.db.WithContext(ctx).
		Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
	if transactionType != nil {
		query = query.Where("transaction_type = ?", *transactionType)
	}
	if startTime != nil {
		query = query.Where("created_at >= ?", *startTime)
	}
	if endTime != nil {
		query = query.Where("created_at <= ?", *endTime)
	}
	// 应用数据权限过滤（使用 shop_id_tag 字段）
	query = middleware.ApplyShopTagFilter(ctx, query)
	err := query.Order("created_at DESC").
		Offset(offset).
		Limit(limit).
		Find(&transactions).Error
	if err != nil {
		return nil, err
	}
	return transactions, nil
}

// CountByResourceIDWithFilter 统计资源的交易记录数量（支持 transaction_type 和时间范围过滤）
func (s *AssetWalletTransactionStore) CountByResourceIDWithFilter(ctx context.Context, resourceType string, resourceID uint, transactionType *string, startTime, endTime *time.Time) (int64, error) {
	var count int64
	query := s.db.WithContext(ctx).
		Model(&model.AssetWalletTransaction{}).
		Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
	if transactionType != nil {
		query = query.Where("transaction_type = ?", *transactionType)
	}
	if startTime != nil {
		query = query.Where("created_at >= ?", *startTime)
	}
	if endTime != nil {
		query = query.Where("created_at <= ?", *endTime)
	}
	// 应用数据权限过滤（使用 shop_id_tag 字段）
	query = middleware.ApplyShopTagFilter(ctx, query)
	err := query.Count(&count).Error
	return count, err
}
