// Package services contains business logic and application services.
package services

import (
	"context"

	"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
	"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
)

// FilamentService wraps FilamentRepository with business logic and validation.
type FilamentService struct {
	repo *repositories.FilamentRepository
}

// NewFilamentService creates a FilamentService backed by the given repository.
func NewFilamentService(repo *repositories.FilamentRepository) *FilamentService {
	return &FilamentService{repo: repo}
}

// List returns paginated filament spools filtered by the given criteria.
func (s *FilamentService) List(ctx context.Context, filter repositories.FilamentFilter) ([]models.FilamentSpool, int, error) {
	return s.repo.GetAll(ctx, filter)
}

// GetByID returns a single filament spool by ID.
func (s *FilamentService) GetByID(ctx context.Context, id int) (*models.FilamentSpool, error) {
	return s.repo.GetByID(ctx, id)
}

// Create validates and creates a new filament spool.
func (s *FilamentService) Create(ctx context.Context, spool *models.FilamentSpool) (*models.FilamentSpool, error) {
	if err := validateFilamentSpool(spool); err != nil {
		return nil, err
	}
	return s.repo.Create(ctx, spool)
}

// Update applies partial updates to a filament spool after validation.
func (s *FilamentService) Update(ctx context.Context, id int, updates map[string]interface{}) (*models.FilamentSpool, error) {
	return s.repo.Update(ctx, id, updates)
}

// SoftDelete marks a filament spool as deleted.
func (s *FilamentService) SoftDelete(ctx context.Context, id int) (bool, error) {
	return s.repo.SoftDelete(ctx, id)
}

// PrinterService wraps PrinterRepository.
type PrinterService struct {
	repo *repositories.PrinterRepository
}

// NewPrinterService creates a PrinterService backed by the given repository.
func NewPrinterService(repo *repositories.PrinterRepository) *PrinterService {
	return &PrinterService{repo: repo}
}

// List returns all printers.
func (s *PrinterService) List(ctx context.Context) ([]models.Printer, error) {
	return s.repo.GetAll(ctx)
}

// PrintJobService wraps PrintJobRepository.
type PrintJobService struct {
	repo *repositories.PrintJobRepository
}

// NewPrintJobService creates a PrintJobService backed by the given repository.
func NewPrintJobService(repo *repositories.PrintJobRepository) *PrintJobService {
	return &PrintJobService{repo: repo}
}

// List returns paginated print jobs filtered by the given criteria.
func (s *PrintJobService) List(ctx context.Context, filter repositories.PrintJobFilter) ([]models.PrintJob, int, error) {
	return s.repo.GetAll(ctx, filter)
}

// GetByID returns a single print job by ID.
func (s *PrintJobService) GetByID(ctx context.Context, id int) (*models.PrintJob, error) {
	return s.repo.GetByID(ctx, id)
}
