package httpapi

import (
	"context"

	"github.com/dtoro/oikos/internal/httpapi/gen"
)

// ─── Risk Classes ──────────────────────────────────────────────────────

func (s *Server) ListRiskClasses(ctx context.Context, req gen.ListRiskClassesRequestObject) (gen.ListRiskClassesResponseObject, error) {
	rows, err := s.pool.Query(ctx, `SELECT name, description, approval_required, autonomy_allowed FROM risk_classes ORDER BY name`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	items := []gen.RiskClass{}
	for rows.Next() {
		var rc gen.RiskClass
		if err := rows.Scan(&rc.Name, &rc.Description, &rc.ApprovalRequired, &rc.AutonomyAllowed); err != nil {
			return nil, err
		}
		items = append(items, rc)
	}
	if rows.Err() != nil {
		return nil, rows.Err()
	}
	if items == nil {
		items = []gen.RiskClass{}
	}
	return gen.ListRiskClasses200JSONResponse{Items: items}, nil
}
