package cms

import (
	"os"
	"path/filepath"
)

func Status(root string) (StatusReport, error) {
	project, err := LoadProject(root)
	if err != nil {
		return StatusReport{}, err
	}
	check, err := Check(root)
	if err != nil {
		return StatusReport{}, err
	}
	report := StatusReport{
		Root:        root,
		Config:      project.Config,
		Menus:       len(project.Menus),
		Sections:    len(project.Sections),
		Widgets:     len(project.Widgets),
		PublicFiles: countPublicFiles(filepath.Join(root, "public")),
		Issues:      check.Issues,
	}
	for _, doc := range project.Docs {
		switch doc.Type {
		case "page":
			report.Pages++
		case "article":
			report.Articles++
		}
		if doc.Draft {
			report.Drafts++
		}
	}
	return report, nil
}

func countPublicFiles(root string) int {
	count := 0
	filepath.WalkDir(root, func(_ string, entry os.DirEntry, err error) error {
		if err != nil || entry.IsDir() {
			return nil
		}
		count++
		return nil
	})
	return count
}
