package bootstrap

import (
	_ "embed"
	"encoding/json"
	"fmt"
)

//go:embed standard_permissions.json
var standardPermissionsJSON []byte

// StandardPermissionRow describes one row in the permission catalog pack.
// The pack is the single source of truth for platform-shipped permission
// declarations — each row becomes a _System_Permission entry and, via
// DefaultGrantedTo, the initial grants into each profile's backing
// PermissionSet. Custom permissions authored at runtime by admins go into
// _System_CustomPermission (different table); this pack is for
// platform-owned Salesforce-compatible UserPermissions only.
type StandardPermissionRow struct {
	APIName          string   `json:"apiName"`
	Label            string   `json:"label"`
	Category         string   `json:"category"`
	Description      string   `json:"description"`
	Requires         string   `json:"requires,omitempty"`
	DefaultGrantedTo []string `json:"defaultGrantedTo,omitempty"`
}

// LoadStandardPermissions parses the shipped permission catalog pack. Fails
// fast on empty/malformed — a platform boot with zero catalog entries
// would leave every row-driven reader with no permissions to describe, so
// refusing to proceed is safer than silently seeding nothing.
func LoadStandardPermissions() ([]StandardPermissionRow, error) {
	var rows []StandardPermissionRow
	if err := json.Unmarshal(standardPermissionsJSON, &rows); err != nil {
		return nil, fmt.Errorf("parse standard_permissions.json: %w", err)
	}
	if len(rows) == 0 {
		return nil, fmt.Errorf("standard_permissions.json is empty — refusing to seed a platform with zero permissions")
	}
	return rows, nil
}
