package employee_wi

import (
	"context"
	"fmt"

	"gitea.urkob.com/urko/ess-etl-go/pkg/domain"

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

func (p *Repo) Find(ctx context.Context, filter bson.M) ([]domain.EmployeeWorkInformation, error) {
	cursor, err := p.collection.Find(ctx, filter)
	if err != nil {
		return nil, fmt.Errorf("p.collection.Find: %s", err)
	}

	var pList []domain.EmployeeWorkInformation
	if err := cursor.All(ctx, &pList); err != nil {
		return nil, fmt.Errorf("p.cursor.All: %s", err)
	}

	return pList, nil
}

func (p *Repo) FindOne(ctx context.Context) (*domain.EmployeeWorkInformation, error) {
	res := p.collection.FindOne(ctx, bson.M{})
	var model *domain.EmployeeWorkInformation
	if err := res.Decode(&model); err != nil {
		return nil, fmt.Errorf("doc.Decode: %s", err)
	}

	return model, nil
}

func (p *Repo) FindById(ctx context.Context, id primitive.ObjectID) (*domain.EmployeeWorkInformation, error) {
	res := p.collection.FindOne(ctx, bson.M{"_id": id})
	var model *domain.EmployeeWorkInformation
	if err := res.Decode(model); err != nil {
		return nil, fmt.Errorf("doc.Decode: %s", err)
	}

	return model, nil
}