package repository

import (
	"context"
	"strings"
	"time"

	app "haixun-backend/internal/library/errors"
	"haixun-backend/internal/library/errors/code"
	libmongo "haixun-backend/internal/library/mongo"
	"haixun-backend/internal/model/scan_post/domain/entity"
	domrepo "haixun-backend/internal/model/scan_post/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
	}
	return libmongo.EnsureIndexes(ctx, r.collection, []mongo.IndexModel{
		{
			Keys:    bson.D{{Key: "tenant_id", Value: 1}, {Key: "owner_uid", Value: 1}, {Key: "brand_id", Value: 1}, {Key: "permalink", Value: 1}},
			Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.M{"brand_id": bson.M{"$gt": ""}})},
		{
			Keys:    bson.D{{Key: "tenant_id", Value: 1}, {Key: "owner_uid", Value: 1}, {Key: "persona_id", Value: 1}, {Key: "permalink", Value: 1}},
			Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.M{"persona_id": bson.M{"$gt": ""}})},
		{
			Keys: bson.D{{Key: "tenant_id", Value: 1}, {Key: "owner_uid", Value: 1}, {Key: "brand_id", Value: 1}, {Key: "priority", Value: 1}},
		},
		{
			Keys: bson.D{{Key: "tenant_id", Value: 1}, {Key: "owner_uid", Value: 1}, {Key: "persona_id", Value: 1}, {Key: "priority", Value: 1}},
		},
		{
			Keys: bson.D{{Key: "scan_job_id", Value: 1}},
		},
	})
}

func brandOwnerFilter(tenantID, ownerUID, brandID string) bson.M {
	filter := bson.M{
		"tenant_id": tenantID,
		"owner_uid": ownerUID,
	}
	for k, v := range libmongo.BrandScopeFilter(brandID) {
		filter[k] = v
	}
	return filter
}

func personaViralFilter(tenantID, ownerUID, personaID string) bson.M {
	return bson.M{
		"tenant_id":  tenantID,
		"owner_uid":  ownerUID,
		"persona_id": strings.TrimSpace(personaID),
		"flow":       entity.FlowViral,
	}
}

func (r *mongoRepository) ReplaceForViralScan(ctx context.Context, tenantID, ownerUID, personaID, scanJobID string, posts []entity.ScanPost) error {
	if r.collection == nil {
		return app.For(code.Persona).DBUnavailable("Mongo is not configured")
	}
	_, err := r.collection.DeleteMany(ctx, personaViralFilter(tenantID, ownerUID, personaID))
	if err != nil {
		return err
	}
	if len(posts) == 0 {
		return nil
	}
	docs := make([]any, 0, len(posts))
	for _, post := range posts {
		docs = append(docs, post)
	}
	_, err = r.collection.InsertMany(ctx, docs)
	return err
}

func (r *mongoRepository) ReplaceForScan(ctx context.Context, tenantID, ownerUID, brandID, scanJobID string, posts []entity.ScanPost) error {
	if r.collection == nil {
		return app.For(code.Brand).DBUnavailable("Mongo is not configured")
	}
	_, err := r.collection.DeleteMany(ctx, brandOwnerFilter(tenantID, ownerUID, brandID))
	if err != nil {
		return err
	}
	if len(posts) == 0 {
		return nil
	}
	docs := make([]any, 0, len(posts))
	for _, post := range posts {
		docs = append(docs, post)
	}
	_, err = r.collection.InsertMany(ctx, docs)
	return err
}

func (r *mongoRepository) Get(ctx context.Context, tenantID, ownerUID, brandID, postID string) (*entity.ScanPost, error) {
	if r.collection == nil {
		return nil, app.For(code.Brand).DBUnavailable("Mongo is not configured")
	}
	filter := brandOwnerFilter(tenantID, ownerUID, brandID)
	filter["_id"] = strings.TrimSpace(postID)
	var out entity.ScanPost
	err := r.collection.FindOne(ctx, filter).Decode(&out)
	if err == mongo.ErrNoDocuments {
		return nil, app.For(code.Brand).ResNotFound("scan post not found")
	}
	if err != nil {
		return nil, err
	}
	return &out, nil
}

func (r *mongoRepository) UpdateOutreach(
	ctx context.Context,
	tenantID, ownerUID, brandID, postID string,
	patch entity.OutreachPatch,
) (*entity.ScanPost, error) {
	if r.collection == nil {
		return nil, app.For(code.Brand).DBUnavailable("Mongo is not configured")
	}
	set := bson.M{}
	if strings.TrimSpace(patch.Status) != "" {
		set["outreach_status"] = strings.TrimSpace(patch.Status)
	}
	if strings.TrimSpace(patch.PublishedReplyID) != "" {
		set["published_reply_id"] = strings.TrimSpace(patch.PublishedReplyID)
	}
	if strings.TrimSpace(patch.PublishedPermalink) != "" {
		set["published_permalink"] = strings.TrimSpace(patch.PublishedPermalink)
	}
	if len(set) == 0 {
		return r.Get(ctx, tenantID, ownerUID, brandID, postID)
	}
	set["outreach_update_at"] = time.Now().UnixNano()

	filter := brandOwnerFilter(tenantID, ownerUID, brandID)
	filter["_id"] = strings.TrimSpace(postID)
	opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
	var out entity.ScanPost
	err := r.collection.FindOneAndUpdate(ctx, filter, bson.M{"$set": set}, opts).Decode(&out)
	if err == mongo.ErrNoDocuments {
		return nil, app.For(code.Brand).ResNotFound("scan post not found")
	}
	if err != nil {
		return nil, err
	}
	return &out, nil
}

func (r *mongoRepository) GetForPersona(ctx context.Context, tenantID, ownerUID, personaID, postID string) (*entity.ScanPost, error) {
	if r.collection == nil {
		return nil, app.For(code.Persona).DBUnavailable("Mongo is not configured")
	}
	filter := personaViralFilter(tenantID, ownerUID, personaID)
	filter["_id"] = strings.TrimSpace(postID)
	var out entity.ScanPost
	err := r.collection.FindOne(ctx, filter).Decode(&out)
	if err == mongo.ErrNoDocuments {
		return nil, app.For(code.Persona).ResNotFound("viral scan post not found")
	}
	if err != nil {
		return nil, err
	}
	return &out, nil
}

func (r *mongoRepository) ListForPersona(ctx context.Context, tenantID, ownerUID string, filter domrepo.PersonaListFilter) ([]entity.ScanPost, error) {
	if r.collection == nil {
		return nil, app.For(code.Persona).DBUnavailable("Mongo is not configured")
	}
	query := personaViralFilter(tenantID, ownerUID, filter.PersonaID)
	if flow := strings.TrimSpace(filter.Flow); flow != "" {
		query["flow"] = flow
	}
	limit := filter.Limit
	if limit <= 0 {
		limit = 100
	}
	if limit > 500 {
		limit = 500
	}
	opts := options.Find().
		SetSort(bson.D{{Key: "engagement_score", Value: -1}, {Key: "create_at", Value: -1}}).
		SetLimit(int64(limit))
	cur, err := r.collection.Find(ctx, query, opts)
	if err != nil {
		return nil, err
	}
	defer cur.Close(ctx)
	var out []entity.ScanPost
	if err := cur.All(ctx, &out); err != nil {
		return nil, err
	}
	return out, nil
}

func (r *mongoRepository) List(ctx context.Context, tenantID, ownerUID string, filter domrepo.ListFilter) ([]entity.ScanPost, error) {
	if r.collection == nil {
		return nil, app.For(code.Brand).DBUnavailable("Mongo is not configured")
	}
	query := brandOwnerFilter(tenantID, ownerUID, filter.BrandID)
	if strings.TrimSpace(filter.Priority) != "" {
		query["priority"] = strings.TrimSpace(filter.Priority)
	}
	if filter.ProductFitMin > 0 {
		query["product_fit_score"] = bson.M{"$gte": filter.ProductFitMin}
	}
	limit := filter.Limit
	if limit <= 0 {
		limit = 100
	}
	if limit > 500 {
		limit = 500
	}
	opts := options.Find().
		SetSort(bson.D{{Key: "placement_score", Value: -1}, {Key: "create_at", Value: -1}}).
		SetLimit(int64(limit))
	cur, err := r.collection.Find(ctx, query, opts)
	if err != nil {
		return nil, err
	}
	defer cur.Close(ctx)
	var out []entity.ScanPost
	if err := cur.All(ctx, &out); err != nil {
		return nil, err
	}
	if filter.Recent7dOnly {
		filtered := make([]entity.ScanPost, 0, len(out))
		for _, item := range out {
			if item.Priority == "gold" || item.Priority == "recent" {
				filtered = append(filtered, item)
			}
		}
		return filtered, nil
	}
	return out, nil
}

func (r *mongoRepository) CountByBrand(ctx context.Context, tenantID, ownerUID, brandID string) (int, error) {
	if r.collection == nil {
		return 0, app.For(code.Brand).DBUnavailable("Mongo is not configured")
	}
	count, err := r.collection.CountDocuments(ctx, brandOwnerFilter(tenantID, ownerUID, brandID))
	return int(count), err
}
