package http

import (
	"context"
	"fmt"
	"net/http"

	"source.toby3d.me/toby3d/home/internal/static"
)

type Handler struct {
	static static.UseCase
}

func NewHandler(static static.UseCase) *Handler {
	return &Handler{static: static}
}

func (h *Handler) Handle(ctx context.Context, path string) (http.Handler, error) {
	static, err := h.static.Do(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("cannot handle static: %w", err)
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.ServeContent(w, r, static.Name(), static.ModTime(), static)
	}), nil
}
