#!/bin/bash
# scripts/ci/lint.sh - Проверка качества кода

set -e

echo "🔍 Running code quality checks..."

# Проверка Python кода (Backend)
echo "📦 Checking Python code quality..."
if [ -d "backend" ]; then
    echo "  • Running flake8 for Python linting..."
    docker run --rm -v "$(pwd)/backend:/app" -w /app python:3.11-slim bash -c "
        pip install flake8 black isort > /dev/null 2>&1
        echo '    - flake8 check:'
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics || echo '      ⚠️  flake8 issues found'
        echo '    - black format check:'
        black --check . || echo '      ⚠️  black formatting issues found'
        echo '    - isort import check:'
        isort --check-only . || echo '      ⚠️  import sorting issues found'
    "
fi

# Проверка TypeScript/JavaScript кода (Frontend)
echo "🌐 Checking TypeScript/JavaScript code quality..."
if [ -d "frontend/linktree-frontend" ]; then
    echo "  • Running ESLint for TypeScript/JavaScript..."
    docker run --rm -v "$(pwd)/frontend/linktree-frontend:/app" -w /app node:20-alpine sh -c "
        npm install --silent > /dev/null 2>&1
        echo '    - ESLint check:'
        npm run lint || echo '      ⚠️  ESLint issues found'
        echo '    - TypeScript check:'
        npm run type-check || echo '      ⚠️  TypeScript issues found'
    "
fi

# Проверка Docker файлов
echo "🐳 Checking Docker files..."
if command -v hadolint > /dev/null 2>&1; then
    echo "  • Running hadolint for Dockerfile..."
    find . -name "Dockerfile*" -exec hadolint {} \; || echo "      ⚠️  Dockerfile issues found"
else
    echo "  • Hadolint not available, skipping Dockerfile check"
fi

# Проверка YAML файлов
echo "📄 Checking YAML files..."
if command -v yamllint > /dev/null 2>&1; then
    echo "  • Running yamllint..."
    find . -name "*.yml" -o -name "*.yaml" | xargs yamllint || echo "      ⚠️  YAML issues found"
else
    echo "  • yamllint not available, skipping YAML check"
fi

# Проверка Markdown файлов
echo "📝 Checking Markdown files..."
if [ -f "README.md" ]; then
    echo "  • Checking README.md structure..."
    if grep -q "# " README.md; then
        echo "    ✅ README.md has proper headers"
    else
        echo "    ⚠️  README.md missing proper headers"
    fi
fi

# Проверка безопасности зависимостей
echo "🔐 Checking dependencies security..."
if [ -f "frontend/linktree-frontend/package.json" ]; then
    echo "  • Running npm audit..."
    docker run --rm -v "$(pwd)/frontend/linktree-frontend:/app" -w /app node:20-alpine sh -c "
        npm install --silent > /dev/null 2>&1
        npm audit --audit-level moderate || echo '      ⚠️  npm security issues found'
    "
fi

if [ -f "backend/requirements.txt" ]; then
    echo "  • Running safety check for Python..."
    docker run --rm -v "$(pwd)/backend:/app" -w /app python:3.11-slim bash -c "
        pip install safety > /dev/null 2>&1
        safety check -r requirements.txt || echo '      ⚠️  Python security issues found'
    " || echo "      ⚠️  Safety check failed"
fi

echo "✅ Code quality checks completed!"