package extractor

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/tanagram/cli/parser"
)

// ExtractPoliciesFromFile reads a file and extracts policies using LLM
func ExtractPoliciesFromFile(ctx context.Context, filePath string) ([]parser.Policy, error) {
	content, err := os.ReadFile(filePath)
	if err != nil {
		return nil, fmt.Errorf("failed to read file: %w", err)
	}

	contentStr := string(content)

	// If this is an .mdc file, strip YAML frontmatter
	if strings.HasSuffix(strings.ToLower(filepath.Base(filePath)), ".mdc") {
		contentStr = parser.StripMDCFrontmatter(contentStr)
	}

	return ExtractPolicies(ctx, contentStr)
}
