package repository

import (
	"context"
	"strings"

	app "haixun-backend/internal/library/errors"
	"haixun-backend/internal/library/errors/code"
	"haixun-backend/internal/model/publish_analytics/domain/entity"
	domrepo "haixun-backend/internal/model/publish_analytics/domain/repository"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type mongoRepository struct {
	collection *mongo.Collection
}

func NewMongoRepository(db *mongo.Database) domrepo.Repository {
	if db == nil {
		return &mongoRepository{}
	}
	return &mongoRepository{collection: db.Collection(entity.CollectionName)}
}

func (r *mongoRepository) EnsureIndexes(ctx context.Context) error {
	if r.collection == nil {
		return nil
	}
	_, err := r.collection.Indexes().CreateMany(ctx, []mongo.IndexModel{
		{
			Keys: bson.D{
				{Key: "tenant_id", Value: 1},
				{Key: "owner_uid", Value: 1},
				{Key: "media_id", Value: 1},
				{Key: "checkpoint", Value: 1},
			},
		},
	})
	return err
}

func actorFilter(tenantID, ownerUID string) bson.M {
	return bson.M{
		"tenant_id": strings.TrimSpace(tenantID),
		"owner_uid": strings.TrimSpace(ownerUID),
	}
}

func (r *mongoRepository) Create(ctx context.Context, analytics *entity.PublishAnalytics) error {
	if r.collection == nil {
		return app.For(code.AI).DBUnavailable("Mongo is not configured")
	}
	if analytics == nil {
		return app.For(code.AI).InputMissingRequired("analytics is required")
	}
	_, err := r.collection.InsertOne(ctx, analytics)
	return err
}

func (r *mongoRepository) Get(ctx context.Context, tenantID, ownerUID, analyticsID string) (*entity.PublishAnalytics, error) {
	if r.collection == nil {
		return nil, app.For(code.AI).DBUnavailable("Mongo is not configured")
	}
	analyticsID = strings.TrimSpace(analyticsID)
	if analyticsID == "" {
		return nil, app.For(code.AI).InputMissingRequired("analytics_id is required")
	}
	filter := actorFilter(tenantID, ownerUID)
	filter["_id"] = analyticsID
	var out entity.PublishAnalytics
	err := r.collection.FindOne(ctx, filter).Decode(&out)
	if err != nil {
		if err == mongo.ErrNoDocuments {
			return nil, app.For(code.AI).ResNotFound("publish analytics not found")
		}
		return nil, err
	}
	return &out, nil
}

func (r *mongoRepository) ListByMedia(ctx context.Context, tenantID, ownerUID, mediaID string) ([]entity.PublishAnalytics, error) {
	if r.collection == nil {
		return nil, app.For(code.AI).DBUnavailable("Mongo is not configured")
	}
	mediaID = strings.TrimSpace(mediaID)
	if mediaID == "" {
		return nil, app.For(code.AI).InputMissingRequired("media_id is required")
	}
	filter := actorFilter(tenantID, ownerUID)
	filter["media_id"] = mediaID
	opts := options.Find().SetSort(bson.D{{Key: "checked_at", Value: -1}})
	cur, err := r.collection.Find(ctx, filter, opts)
	if err != nil {
		return nil, err
	}
	defer cur.Close(ctx)
	var out []entity.PublishAnalytics
	if err := cur.All(ctx, &out); err != nil {
		return nil, err
	}
	return out, nil
}

func (r *mongoRepository) Upsert(ctx context.Context, analytics *entity.PublishAnalytics) (*entity.PublishAnalytics, error) {
	if r.collection == nil {
		return nil, app.For(code.AI).DBUnavailable("Mongo is not configured")
	}
	if analytics == nil {
		return nil, app.For(code.AI).InputMissingRequired("analytics is required")
	}
	if analytics.ID == "" {
		return nil, app.For(code.AI).InputMissingRequired("analytics id is required")
	}

	filter := actorFilter(analytics.TenantID, analytics.OwnerUID)
	filter["media_id"] = strings.TrimSpace(analytics.MediaID)
	filter["checkpoint"] = strings.TrimSpace(analytics.Checkpoint)
	opts := options.Replace().SetUpsert(true)
	_, err := r.collection.ReplaceOne(ctx, filter, analytics, opts)
	if err != nil {
		return nil, err
	}
	return analytics, nil
}

func (r *mongoRepository) DeleteByMediaIDs(ctx context.Context, tenantID, ownerUID string, mediaIDs []string) (int64, error) {
	if r.collection == nil {
		return 0, app.For(code.AI).DBUnavailable("Mongo is not configured")
	}
	ids := make([]string, 0, len(mediaIDs))
	seen := map[string]struct{}{}
	for _, mediaID := range mediaIDs {
		trimmed := strings.TrimSpace(mediaID)
		if trimmed == "" {
			continue
		}
		if _, ok := seen[trimmed]; ok {
			continue
		}
		seen[trimmed] = struct{}{}
		ids = append(ids, trimmed)
	}
	if len(ids) == 0 {
		return 0, nil
	}
	filter := actorFilter(tenantID, ownerUID)
	filter["media_id"] = bson.M{"$in": ids}
	res, err := r.collection.DeleteMany(ctx, filter)
	if err != nil {
		return 0, err
	}
	return res.DeletedCount, nil
}
