package http

import (
	"log/slog"
	stdhttp "net/http"

	"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
)

// hostJobsPage is the page-data struct for /hosts/{id}/jobs.
type hostJobsPage struct {
	hostChromeData
	Jobs []store.Job
}

// handleUIHostJobs renders the per-host jobs list. Read-only — no
// actions, just a click-through to the existing /jobs/{id} detail
// page for any row.
func (s *Server) handleUIHostJobs(w stdhttp.ResponseWriter, r *stdhttp.Request) {
	u := s.requireUIUser(w, r)
	if u == nil {
		return
	}
	host, ok := s.loadHostForUI(w, r)
	if !ok {
		return
	}

	jobs, err := s.deps.Store.ListJobsByHost(r.Context(), host.ID, 100)
	if err != nil {
		slog.Error("ui host jobs: list", "host_id", host.ID, "err", err)
		stdhttp.Error(w, "internal", stdhttp.StatusInternalServerError)
		return
	}

	page := hostJobsPage{
		hostChromeData: s.loadHostChrome(r, *host, "jobs", "jobs"),
		Jobs:           jobs,
	}
	view := s.baseView(r, u)
	view.Title = host.Name + " jobs · restic-manager"
	view.Page = page
	if err := s.deps.UI.Render(w, "host_jobs", view); err != nil {
		slog.Error("ui: render host_jobs", "err", err)
		stdhttp.Error(w, "internal", stdhttp.StatusInternalServerError)
	}
}
