#!/bin/bash
# Local Code Review using Roslynator
# No external server required - uses installed analyzers

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${GREEN}╔════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║   CODEX Code Review - Local Analysis  ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════╝${NC}\n"

# Restore tools
echo -e "${YELLOW}→ Restoring tools...${NC}"
dotnet tool restore > /dev/null 2>&1

# Clean
echo -e "${YELLOW}→ Cleaning build artifacts...${NC}"
dotnet clean > /dev/null 2>&1

# Analyze with Roslynator
echo -e "\n${BLUE}═══════════════════════════════════════${NC}"
echo -e "${BLUE}   Running Roslynator Analysis${NC}"
echo -e "${BLUE}═══════════════════════════════════════${NC}\n"

dotnet roslynator analyze \
  --severity-level info \
  --output code-review-results.xml \
  Codex.sln

echo -e "\n${BLUE}═══════════════════════════════════════${NC}"
echo -e "${BLUE}   Code Formatting Check${NC}"
echo -e "${BLUE}═══════════════════════════════════════${NC}\n"

dotnet format --verify-no-changes --verbosity diagnostic || echo -e "${YELLOW}⚠ Formatting issues detected. Run 'dotnet format' to fix.${NC}"

echo -e "\n${GREEN}═══════════════════════════════════════${NC}"
echo -e "${GREEN}   Code Review Complete!${NC}"
echo -e "${GREEN}═══════════════════════════════════════${NC}\n"

if [ -f "code-review-results.xml" ]; then
  echo -e "${BLUE}📊 Results saved to: code-review-results.xml${NC}"
fi

echo -e "\n${YELLOW}Quick Commands:${NC}"
echo -e "  ${BLUE}dotnet format${NC}               - Auto-fix formatting"
echo -e "  ${BLUE}dotnet roslynator fix${NC}       - Auto-fix code issues"
echo -e "  ${BLUE}dotnet build${NC}                - Standard build\n"
