simular 0.2.0

Unified Simulation Engine for the Sovereign AI Stack
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# Simular Makefile
# Certeza Methodology - Tiered Quality Gates
#
# PERFORMANCE TARGETS (Toyota Way: Zero Defects, Fast Feedback)
# - make test-fast: < 30 seconds (unit tests, reduced property cases)
# - make test:      < 2 minutes (all tests)
# - make coverage:  < 5 minutes (coverage report, 90% required)
# - make test-full: comprehensive (all tests, all features, full property cases)
#
# QUALITY TARGETS
# - Coverage: ≥90% (enforced; terminal-dependent TUI code exempted)
# - Mutation: ≥80% kill rate
# - Property: 100 cases (fast), 1000 cases (full)

# Use bash for shell commands
SHELL := /bin/bash

# Disable built-in rules for performance
.SUFFIXES:

# Delete partially-built files on error
.DELETE_ON_ERROR:

# Multi-line recipes execute in same shell
.ONESHELL:

# Coverage threshold (95% minimum - refactored TUI for testability)
COVERAGE_THRESHOLD := 95

.PHONY: all build test test-fast test-quick test-full lint fmt fmt-check clean doc
.PHONY: tier1 tier2 tier3 tier4 coverage coverage-fast coverage-full coverage-open coverage-check
.PHONY: bench dev pre-push ci check audit deps-validate deny
.PHONY: pmat-score pmat-gates pmat-tdg pmat-analyze pmat-all
.PHONY: quality-report kaizen mutants mutants-fast mutants-check
.PHONY: property-test property-test-fast property-test-full
.PHONY: install-tools help release-check release release-tag examples
.PHONY: serve-tsp serve-orbit serve
.PHONY: quality-gates validate

# Default target
all: tier2

# ============================================================================
# HELP
# ============================================================================

help: ## Show this help message
	@echo 'Simular Development Commands (Tiered Workflow)'
	@echo ''
	@echo 'QUALITY TARGETS:'
	@echo '  Coverage: ≥$(COVERAGE_THRESHOLD)% (enforced)'
	@echo '  Mutation: ≥80% kill rate'
	@echo ''
	@echo 'Tiered TDD-X (Certeza Framework):'
	@echo '  tier1         Sub-second feedback (ON-SAVE)'
	@echo '  tier2         Full validation (ON-COMMIT, 1-5min)'
	@echo '  tier3         Mutation+Coverage (ON-MERGE, hours)'
	@echo '  kaizen        Continuous improvement analysis'
	@echo ''
	@echo 'Quick Commands:'
	@echo '  test-fast     Fast tests (<30s)'
	@echo '  coverage      Coverage report (<5min, ≥$(COVERAGE_THRESHOLD)% required)'
	@echo '  lint          Clippy (zero warnings)'
	@echo '  validate      Full validation (tier2 + coverage check)'
	@echo ''
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | grep -v 'tier\|kaizen' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'

# ============================================================================
# BUILD
# ============================================================================

build: ## Build the project (all features)
	cargo build --all-features

build-release: ## Build release version
	cargo build --release --all-features

# ============================================================================
# TEST TARGETS (Performance-Optimized with nextest)
# ============================================================================

# Fast tests (<30s): Uses nextest for parallelism if available
test-fast: ## Fast unit tests (<30s target)
	@echo "⚡ Running fast tests (target: <30s)..."
	@if command -v cargo-nextest >/dev/null 2>&1; then \
		time env PROPTEST_CASES=50 cargo nextest run --workspace --lib \
			--status-level skip \
			--failure-output immediate \
			--all-features; \
	else \
		echo "💡 Install cargo-nextest for faster tests: cargo install cargo-nextest"; \
		time env PROPTEST_CASES=50 cargo test --workspace --lib --all-features; \
	fi
	@echo "✅ Fast tests passed"

# Quick alias for test-fast
test-quick: test-fast

# Standard tests (<2min): All tests including integration
test: ## Standard tests (<2min target)
	@echo "🧪 Running standard tests (target: <2min)..."
	@if command -v cargo-nextest >/dev/null 2>&1; then \
		time env PROPTEST_CASES=100 cargo nextest run --workspace --all-features \
			--status-level skip \
			--failure-output immediate; \
	else \
		time env PROPTEST_CASES=100 cargo test --workspace --all-features; \
	fi
	@echo "✅ Standard tests passed"

# Full comprehensive tests: All features, all property cases
test-full: ## Comprehensive tests (all features, 256 property cases)
	@echo "🔬 Running full comprehensive tests..."
	@if command -v cargo-nextest >/dev/null 2>&1; then \
		time PROPTEST_CASES=256 cargo nextest run --workspace --all-features; \
	else \
		time PROPTEST_CASES=256 cargo test --workspace --all-features; \
	fi
	@echo "✅ Full tests passed"

# ============================================================================
# PROPERTY TESTING
# ============================================================================

property-test-fast: ## Property tests (50 cases, fast)
	@echo "🎲 Running property tests (50 cases)..."
	@PROPTEST_CASES=50 cargo test --all-features -- prop_
	@echo "✅ Property tests passed (fast)"

property-test: ## Property tests (100 cases, standard)
	@echo "🎲 Running property tests (100 cases)..."
	@PROPTEST_CASES=100 cargo test --all-features -- prop_
	@echo "✅ Property tests passed"

property-test-full: ## Property tests (1000 cases, comprehensive)
	@echo "🎲 Running property tests (1000 cases)..."
	@PROPTEST_CASES=1000 cargo test --all-features -- prop_
	@echo "✅ Property tests passed (comprehensive)"

# ============================================================================
# LINT AND FORMAT
# ============================================================================

lint: ## Run clippy (zero warnings allowed)
	@echo "🔍 Running clippy (zero warnings policy)..."
	@echo "   Note: Using --lib to exclude test code (tests use unwrap)"
	cargo clippy --lib --all-features -- -D warnings

lint-fast: ## Fast clippy (library only)
	@cargo clippy --lib --quiet --all-features -- -D warnings

fmt: ## Format code
	cargo fmt

fmt-check: ## Check formatting without modifying
	cargo fmt -- --check

# ============================================================================
# TIER 1: ON-SAVE (Sub-second feedback)
# ============================================================================

tier1: ## Tier 1: Sub-second feedback for rapid iteration (ON-SAVE)
	@echo "🚀 TIER 1: Sub-second feedback (flow state enabled)"
	@echo ""
	@echo "  [1/4] Type checking..."
	@cargo check --quiet --all-features
	@echo "  [2/4] Linting (fast mode)..."
	@cargo clippy --lib --quiet --all-features -- -D warnings
	@echo "  [3/4] Unit tests (focused)..."
	@cargo test --lib --quiet --all-features
	@echo "  [4/4] Property tests (10 cases)..."
	@PROPTEST_CASES=10 cargo test prop_ --lib --quiet --all-features 2>/dev/null || true
	@echo ""
	@echo "✅ Tier 1 complete - Ready to continue coding!"

# ============================================================================
# TIER 2: ON-COMMIT (1-5 minutes)
# ============================================================================

tier2: ## Tier 2: Full test suite for commits (ON-COMMIT)
	@echo "🔍 TIER 2: Comprehensive validation (1-5 minutes)"
	@echo ""
	@echo "  [1/6] Formatting check..."
	@cargo fmt -- --check
	@echo "  [2/6] Full clippy (lib only, tests exempt from unwrap lint)..."
	@cargo clippy --lib --all-features --quiet -- -D warnings
	@echo "  [3/6] All tests..."
	@env PROPTEST_CASES=100 cargo test --all-features --quiet
	@echo "  [4/6] Property tests..."
	@PROPTEST_CASES=100 cargo test prop_ --all-features --quiet 2>/dev/null || true
	@echo "  [5/6] Doc tests..."
	@cargo test --doc --all-features --quiet
	@echo "  [6/6] SATD check..."
	@! grep -rn "TODO\|FIXME\|HACK" src/ 2>/dev/null || { echo "    ⚠️  SATD comments found (informational)"; }
	@echo ""
	@echo "✅ Tier 2 complete - Ready to commit!"

# ============================================================================
# TIER 3: ON-MERGE/NIGHTLY (Hours)
# ============================================================================

tier3: ## Tier 3: Coverage + Mutation testing (ON-MERGE/NIGHTLY)
	@echo "🧬 TIER 3: Test quality assurance (hours)"
	@echo ""
	@echo "  [1/6] Tier 2 gates..."
	@$(MAKE) --no-print-directory tier2
	@echo ""
	@echo "  [2/6] Coverage analysis (≥$(COVERAGE_THRESHOLD)% required)..."
	@$(MAKE) --no-print-directory coverage-check
	@echo ""
	@echo "  [3/6] Mutation testing sample..."
	@$(MAKE) --no-print-directory mutants-fast || echo "    ⚠️  Mutation testing sample complete"
	@echo ""
	@echo "  [4/6] Property tests (full)..."
	@PROPTEST_CASES=256 cargo test prop_ --all-features --quiet 2>/dev/null || true
	@echo ""
	@echo "  [5/6] Security audit..."
	@cargo audit 2>/dev/null || echo "    ⚠️  cargo-audit not installed"
	@echo ""
	@echo "  [6/6] PMAT analysis..."
	@pmat rust-project-score 2>/dev/null || echo "    ⚠️  pmat not available"
	@echo ""
	@echo "✅ Tier 3 complete - Ready to merge!"

# ============================================================================
# TIER 4: CI/CD (Full validation)
# ============================================================================

tier4: tier3 ## Tier 4: CI/CD validation (comprehensive)
	@echo "🏗️ TIER 4: CI/CD validation..."
	@echo ""
	@echo "  Running release tests..."
	@cargo test --release --all-features
	@echo ""
	@echo "  Full mutation testing..."
	@$(MAKE) --no-print-directory mutants || echo "⚠️  Full mutation testing takes time"
	@echo ""
	@echo "✅ Tier 4 complete!"

# ============================================================================
# COVERAGE TARGETS (Two-Phase Pattern - 95% REQUIRED)
# ============================================================================
# CRITICAL: mold linker breaks LLVM coverage instrumentation
# Solution: Temporarily move ~/.cargo/config.toml during coverage runs

coverage: ## Generate HTML coverage report (target: <5 min, ≥95% required)
	@echo "📊 Running coverage analysis (target: <5 min, ≥$(COVERAGE_THRESHOLD)% required)..."
	@echo "🔍 Checking for cargo-llvm-cov and cargo-nextest..."
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "📦 Installing cargo-llvm-cov..." && cargo install cargo-llvm-cov --locked)
	@which cargo-nextest > /dev/null 2>&1 || (echo "📦 Installing cargo-nextest..." && cargo install cargo-nextest --locked)
	@echo "⚙️  Temporarily disabling global cargo config (sccache/mold break coverage)..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@echo "🧹 Cleaning old coverage data..."
	@cargo llvm-cov clean --workspace
	@mkdir -p target/coverage
	@echo "🧪 Phase 1: Running tests with instrumentation (no report)..."
	@echo "   Using PROPTEST_CASES=100 for faster coverage"
	@env PROPTEST_CASES=100 cargo llvm-cov --no-report nextest --no-tests=warn --workspace --no-fail-fast --all-features 2>/dev/null || \
		env PROPTEST_CASES=100 cargo llvm-cov --no-report --all-features
	@echo "📊 Phase 2: Generating coverage reports..."
	@cargo llvm-cov report --html --output-dir target/coverage/html --ignore-filename-regex 'probar/|tsp_wasm_app\.rs|visualization/tui\.rs|visualization/web\.rs|edd/report\.rs|bin/.*_tui\.rs|main\.rs'
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info --ignore-filename-regex 'probar/|tsp_wasm_app\.rs|visualization/tui\.rs|visualization/web\.rs|edd/report\.rs|bin/.*_tui\.rs|main\.rs'
	@echo "⚙️  Restoring global cargo config..."
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "📊 Coverage Summary:"
	@echo "=================="
	@cargo llvm-cov report --summary-only --ignore-filename-regex 'probar/|tsp_wasm_app\.rs|visualization/tui\.rs|visualization/web\.rs|edd/report\.rs|bin/.*_tui\.rs|main\.rs'
	@echo ""
	@echo "💡 Reports:"
	@echo "- HTML: target/coverage/html/index.html"
	@echo "- LCOV: target/coverage/lcov.info"
	@echo ""
	@echo "🎯 Target: ≥$(COVERAGE_THRESHOLD)%"
	@echo ""

# Fast coverage alias (same as coverage, optimized by default)
coverage-fast: coverage

# Coverage check: Enforce threshold
coverage-check: ## Enforce coverage threshold (≥95%)
	@echo "🔒 Enforcing $(COVERAGE_THRESHOLD)% coverage threshold..."
	@which cargo-llvm-cov > /dev/null 2>&1 || cargo install cargo-llvm-cov --locked
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@cargo llvm-cov clean --workspace 2>/dev/null || true
	@env PROPTEST_CASES=100 cargo llvm-cov --no-report --all-features 2>/dev/null || true
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@COVERAGE=$$(cargo llvm-cov report --summary-only --ignore-filename-regex 'probar/|tsp_wasm_app\.rs|visualization/tui\.rs|visualization/web\.rs|edd/report\.rs|bin/.*_tui\.rs|main\.rs' 2>/dev/null | grep "TOTAL" | awk '{print $$NF}' | sed 's/%//'); \
	echo "Coverage: $${COVERAGE}%"; \
	if [ -n "$$COVERAGE" ]; then \
		THRESHOLD=$(COVERAGE_THRESHOLD); \
		if [ $$(echo "$$COVERAGE < $$THRESHOLD" | bc -l 2>/dev/null || echo 0) -eq 1 ]; then \
			echo "❌ FAIL: Coverage $${COVERAGE}% < $(COVERAGE_THRESHOLD)% threshold"; \
			exit 1; \
		else \
			echo "✅ Coverage threshold met: $${COVERAGE}% ≥ $(COVERAGE_THRESHOLD)%"; \
		fi; \
	else \
		echo "⚠️  Could not determine coverage"; \
	fi

# Full coverage: All features (for CI, slower)
coverage-full: ## Full coverage report (all features, comprehensive)
	@echo "📊 Running full coverage analysis (all features)..."
	@which cargo-llvm-cov > /dev/null 2>&1 || cargo install cargo-llvm-cov --locked
	@which cargo-nextest > /dev/null 2>&1 || cargo install cargo-nextest --locked
	@cargo llvm-cov clean --workspace
	@mkdir -p target/coverage
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@env PROPTEST_CASES=256 cargo llvm-cov --no-report nextest --no-tests=warn --workspace --all-features 2>/dev/null || \
		env PROPTEST_CASES=256 cargo llvm-cov --no-report --all-features
	@cargo llvm-cov report --html --output-dir target/coverage/html
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@cargo llvm-cov report --summary-only

# Open coverage report in browser
coverage-open: ## Open HTML coverage report in browser
	@if [ -f target/coverage/html/index.html ]; then \
		xdg-open target/coverage/html/index.html 2>/dev/null || \
		open target/coverage/html/index.html 2>/dev/null || \
		echo "Open: target/coverage/html/index.html"; \
	else \
		echo "❌ Run 'make coverage' first"; \
	fi

# ============================================================================
# MUTATION TESTING
# ============================================================================

mutants: ## Run full mutation testing (≥80% kill rate target)
	@echo "🧬 Running full mutation testing (target: ≥80% kill rate)..."
	@which cargo-mutants > /dev/null 2>&1 || (echo "📦 Installing cargo-mutants..." && cargo install cargo-mutants --locked)
	@cargo mutants --no-times --timeout 300 -- --all-features
	@echo "✅ Mutation testing complete"

mutants-fast: ## Run mutation testing sample (~5 min)
	@echo "⚡ Running mutation testing (fast sample)..."
	@which cargo-mutants > /dev/null 2>&1 || (echo "📦 Installing cargo-mutants..." && cargo install cargo-mutants --locked)
	@cargo mutants --no-times --timeout 120 --shard 1/10 -- --lib --all-features
	@echo "✅ Mutation sample complete"

mutants-check: ## Check mutation score (≥80% required)
	@echo "🔒 Checking mutation score..."
	@if [ -f mutants.out/mutants.json ]; then \
		echo "Mutation results found"; \
		cat mutants.out/mutants.json | head -20; \
	else \
		echo "Run 'make mutants' first to generate mutation results"; \
	fi

# ============================================================================
# KAIZEN: Continuous Improvement Cycle
# ============================================================================

kaizen: ## Kaizen: Continuous improvement analysis
	@echo "=== KAIZEN: Continuous Improvement Protocol for Simular ==="
	@echo "改善 - Change for the better through systematic analysis"
	@echo ""
	@echo "=== STEP 1: Static Analysis & Technical Debt ==="
	@mkdir -p /tmp/kaizen .kaizen
	@if command -v tokei >/dev/null 2>&1; then \
		tokei src --output json > /tmp/kaizen/loc-metrics.json; \
	else \
		echo '{"Rust":{"code":1000}}' > /tmp/kaizen/loc-metrics.json; \
	fi
	@echo "✅ Baseline metrics collected"
	@echo ""
	@echo "=== STEP 2: Test Coverage Analysis ==="
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@cargo llvm-cov report --summary-only 2>/dev/null | tee /tmp/kaizen/coverage.txt || echo "Coverage: Unknown" > /tmp/kaizen/coverage.txt
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "=== STEP 3: PMAT Analysis ==="
	@pmat rust-project-score 2>/dev/null | tee /tmp/kaizen/pmat.txt || echo "PMAT analysis requires pmat" > /tmp/kaizen/pmat.txt
	@echo ""
	@echo "=== STEP 4: Clippy Analysis ==="
	@cargo clippy --all-features --lib -- -W clippy::all 2>&1 | \
		grep -E "warning:|error:" | wc -l | \
		awk '{print "Clippy warnings/errors: " $$1}'
	@echo ""
	@echo "=== STEP 5: Test Count ==="
	@cargo test --all-features 2>&1 | grep -E "^test result" | tail -1
	@echo ""
	@echo "=== STEP 6: Continuous Improvement Log ==="
	@date '+%Y-%m-%d %H:%M:%S' > /tmp/kaizen/timestamp.txt
	@echo "Session: $$(cat /tmp/kaizen/timestamp.txt)" >> .kaizen/improvement.log
	@echo "Coverage: $$(grep -o '[0-9]*\.[0-9]*%' /tmp/kaizen/coverage.txt | head -1 || echo 'Unknown')" >> .kaizen/improvement.log
	@rm -rf /tmp/kaizen
	@echo ""
	@echo "✅ Kaizen cycle complete - 継続的改善"

# ============================================================================
# VALIDATION (Full Pipeline)
# ============================================================================

validate: tier2 coverage-check ## Full validation (tier2 + coverage check)
	@echo ""
	@echo "✅ Full validation passed!"
	@echo "   - All tests passing"
	@echo "   - Coverage ≥$(COVERAGE_THRESHOLD)%"
	@echo "   - Clippy clean"

quality-gates: lint fmt-check test-fast coverage-check ## Run all quality gates
	@echo ""
	@echo "✅ All quality gates passed!"

# ============================================================================
# OTHER TARGETS
# ============================================================================

clean: ## Clean build artifacts
	cargo clean
	rm -rf target/coverage mutants.out
	rm -f lcov.info

doc: ## Generate documentation
	cargo doc --no-deps --all-features --open

bench: ## Run benchmarks
	cargo bench --all-features --no-fail-fast

check: ## Quick check (compile only)
	cargo check --all-features

audit: ## Run security audit
	@echo "🔒 Running security audit..."
	@cargo audit || echo "⚠️  cargo-audit not installed or vulnerabilities found"

deps-validate: ## Validate dependencies (duplicates + security)
	@echo "🔍 Validating dependencies..."
	@cargo tree --duplicate | grep -v "^$$" || echo "✅ No duplicate dependencies"
	@cargo audit || echo "⚠️  Security issues found"

deny: ## Run cargo-deny checks (licenses, bans, advisories)
	@echo "🔒 Running cargo-deny checks..."
	@if command -v cargo-deny >/dev/null 2>&1; then \
		cargo deny check; \
	else \
		echo "❌ cargo-deny not installed. Install with: cargo install cargo-deny"; \
	fi

dev: tier1 ## Development mode

pre-push: tier3 ## Pre-push checks

ci: tier4 ## CI/CD checks

# ============================================================================
# PMAT INTEGRATION
# ============================================================================

pmat-score: ## Calculate Rust project quality score
	@echo "📊 Calculating Rust project quality score..."
	@pmat rust-project-score || echo "⚠️  pmat not found. Install with: cargo install pmat"

pmat-tdg: ## Run PMAT Technical Debt Grading
	@echo "📊 PMAT Technical Debt Grading..."
	@pmat analyze tdg || echo "⚠️  pmat not available"

pmat-gates: ## Run pmat quality gates
	@echo "🔍 Running pmat quality gates..."
	@pmat quality-gates --report || echo "⚠️  pmat not found or gates failed"

pmat-analyze: ## Run comprehensive PMAT analysis
	@echo "🔍 PMAT Comprehensive Analysis..."
	@pmat analyze complexity --path src/ 2>/dev/null || true
	@pmat analyze satd --path src/ 2>/dev/null || true
	@pmat analyze dead-code --path src/ 2>/dev/null || true

pmat-all: pmat-tdg pmat-analyze pmat-score ## Run all PMAT checks

quality-report: ## Generate comprehensive quality report
	@echo "📋 Generating comprehensive quality report..."
	@mkdir -p docs/quality-reports
	@echo "# Simular Quality Report" > docs/quality-reports/latest.md
	@echo "" >> docs/quality-reports/latest.md
	@echo "Generated: $$(date)" >> docs/quality-reports/latest.md
	@echo "" >> docs/quality-reports/latest.md
	@echo "## Test Results" >> docs/quality-reports/latest.md
	@cargo test --all-features 2>&1 | grep -E "^test result" >> docs/quality-reports/latest.md || true
	@echo "" >> docs/quality-reports/latest.md
	@echo "## Coverage" >> docs/quality-reports/latest.md
	@cargo llvm-cov report --summary-only 2>/dev/null >> docs/quality-reports/latest.md || echo "Run make coverage first" >> docs/quality-reports/latest.md
	@echo "" >> docs/quality-reports/latest.md
	@echo "## Rust Project Score" >> docs/quality-reports/latest.md
	@pmat rust-project-score >> docs/quality-reports/latest.md 2>&1 || echo "Error getting score" >> docs/quality-reports/latest.md
	@echo "✅ Report generated: docs/quality-reports/latest.md"

# ============================================================================
# INSTALL TOOLS
# ============================================================================

install-tools: ## Install required development tools
	@echo "📦 Installing development tools..."
	cargo install cargo-llvm-cov --locked || true
	cargo install cargo-nextest --locked || true
	cargo install cargo-watch || true
	cargo install cargo-mutants --locked || true
	cargo install cargo-audit || true
	@echo "✅ Tools installed"

# ============================================================================
# EXAMPLES
# ============================================================================

examples: ## Run all examples
	@echo "🎯 Running all examples..."
	@for example in examples/*.rs; do \
		if [ -f "$$example" ]; then \
			name=$$(basename "$$example" .rs); \
			echo "  Running $$name..."; \
			cargo run --example "$$name" --all-features --quiet 2>/dev/null || echo "    ⚠️  $$name failed"; \
		fi; \
	done
	@echo "✅ Examples complete"

# ============================================================================
# DEMO SERVERS
# ============================================================================

serve-tsp: ## Serve TSP WASM demo at http://localhost:8080/tsp.html
	@echo "🌐 Serving TSP demo at http://localhost:8080/tsp.html"
	@(sleep 2 && xdg-open http://localhost:8080/tsp.html 2>/dev/null || open http://localhost:8080/tsp.html 2>/dev/null || true) &
	@cd web && npx serve -p 8080

serve-orbit: ## Serve Orbit WASM demo at http://localhost:8080/index.html
	@echo "🌐 Serving Orbit demo at http://localhost:8080/index.html"
	@(sleep 2 && xdg-open http://localhost:8080/index.html 2>/dev/null || open http://localhost:8080/index.html 2>/dev/null || true) &
	@cd web && npx serve -p 8080

serve: serve-tsp ## Alias for serve-tsp

# ============================================================================
# RELEASE
# ============================================================================

release-check: ## Verify package can be published (dry-run)
	@echo "🔍 Checking release readiness..."
	cargo publish --dry-run --allow-dirty
	@echo "✅ Package ready for release"

release: ## Publish to crates.io (requires cargo login)
	@echo "🚀 Publishing simular to crates.io..."
	cargo publish
	@echo "✅ Published successfully"

release-tag: ## Create git tag for current version
	@VERSION=$$(cargo pkgid | cut -d# -f2) && \
	echo "🏷️  Creating tag v$$VERSION..." && \
	git tag -a "v$$VERSION" -m "Release v$$VERSION" && \
	git push origin "v$$VERSION" && \
	echo "✅ Tag v$$VERSION pushed"

.DEFAULT_GOAL := help