# Contributing to NAFNet Image Restoration API

First off, thank you for considering contributing to NAFNet! It's people like you that make NAFNet such a great project.

## Code of Conduct

This project and everyone participating in it is governed by our [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.

---

## How Can I Contribute?

### Reporting Bugs
Before creating bug reports, please check the issue list as you might find out that you don't need to create one. When you are creating a bug report, please include as many details as possible:

**Description of the bug**
- What you were doing when the bug occurred
- Steps to reproduce the problem
- Expected behavior
- Actual behavior
- Relevant logs or error messages

**System information**
- OS (Linux, macOS, Windows)
- Python version
- Browser (if related to web interface)
- Flask version

### Suggesting Enhancements
Enhancement suggestions are tracked as GitHub issues. When creating an enhancement suggestion, please include:

- Use case for the enhancement
- Why you think this enhancement is useful
- Examples of how this enhancement could be implemented

### Pull Requests

**Before starting work:**
1. Fork the repository
2. Create a new branch for your feature
3. Make sure your code follows PEP 8
4. Write tests for new features
5. Update documentation

**Process:**
```bash
# 1. Fork and clone
git clone https://github.com/YOUR-USERNAME/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API

# 2. Create feature branch
git checkout -b feature/your-feature-name

# 3. Make changes
# ... edit files ...

# 4. Test your changes
python app.py
# Test in browser: http://localhost:5000

# 5. Commit changes
git add .
git commit -m "Add feature: description of your feature"

# 6. Push to your fork
git push origin feature/your-feature-name

# 7. Open Pull Request on GitHub
```

---

## Development Setup

### Prerequisites
- Python 3.8+
- Git
- Virtual environment

### Initial Setup
```bash
# Clone repository
git clone https://github.com/Gtajisan/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Install development tools
pip install pytest pytest-cov black flake8 isort
```

### Running the Application
```bash
python app.py
```

Access at `http://localhost:5000`

---

## Code Style

### PEP 8 Compliance
This project follows [PEP 8](https://www.python.org/dev/peps/pep-0008/) guidelines.

**Check code style:**
```bash
flake8 app.py
```

**Auto-format code:**
```bash
black app.py
isort app.py
```

### Naming Conventions
- **Functions/Variables**: `snake_case`
- **Classes**: `PascalCase`
- **Constants**: `UPPER_SNAKE_CASE`
- **Modules**: `snake_case`

### Docstring Format
```python
def enhance_image(input_file):
    """
    Restore and enhance image using image processing techniques.
    
    Applies a series of transformations to improve image quality:
    - Median filtering for denoising
    - Gaussian blur for smoothing
    - Contrast, sharpness, and color enhancement
    
    Args:
        input_file (file-like): Image file object or path
        
    Returns:
        PIL.Image: Enhanced image in RGB format
        
    Raises:
        IOError: If file cannot be opened
        ValueError: If image format is not supported
        
    Example:
        >>> from PIL import Image
        >>> img = Image.open('input.jpg')
        >>> restored = enhance_image(img)
        >>> restored.save('output.png')
    """
    pass
```

### Comments
- Use comments for WHY, not WHAT
- Keep comments concise and clear
- Update comments when code changes

```python
# Good
# Median filter preserves edges while removing noise
denoised[:,:,i] = ndimage.median_filter(img_np[:,:,i], size=3)

# Avoid
# Apply median filter
denoised[:,:,i] = ndimage.median_filter(img_np[:,:,i], size=3)
```

---

## Testing

### Running Tests
```bash
# Run all tests
pytest

# Run with coverage
pytest --cov=nafnet --cov-report=html

# Run specific test file
pytest tests/test_api.py

# Run with verbose output
pytest -v
```

### Writing Tests
```python
# tests/test_api.py
import pytest
from app import app

@pytest.fixture
def client():
    """Create test client."""
    app.config['TESTING'] = True
    return app.test_client()

def test_api_info(client):
    """Test API info endpoint returns correct data."""
    response = client.get('/api/info')
    assert response.status_code == 200
    assert 'name' in response.json
    assert 'version' in response.json

def test_image_upload(client):
    """Test image upload and processing."""
    # Create test image
    from PIL import Image
    img = Image.new('RGB', (100, 100), color='red')
    img.save('/tmp/test.jpg')
    
    with open('/tmp/test.jpg', 'rb') as f:
        response = client.post('/api/fix', data={'file': f})
    
    assert response.status_code == 200
    assert response.content_type == 'image/png'
```

---

## Commit Guidelines

### Commit Message Format
```
<type>(<scope>): <subject>

<body>

<footer>
```

### Types
- `feat`: A new feature
- `fix`: A bug fix
- `docs`: Documentation only changes
- `style`: Changes that don't affect code meaning (formatting, etc.)
- `refactor`: Code change that doesn't fix bugs or add features
- `perf`: Code change that improves performance
- `test`: Adding missing tests
- `chore`: Changes to build process or dependencies

### Examples
```bash
# Good commit messages
git commit -m "feat(api): add batch processing endpoint"
git commit -m "fix(image): handle RGBA to RGB conversion correctly"
git commit -m "docs(readme): update installation instructions"
git commit -m "refactor(processing): optimize image enhancement pipeline"
git commit -m "test(api): add comprehensive endpoint tests"

# Avoid
git commit -m "fix bug"
git commit -m "updates"
git commit -m "WIP"
```

---

## Pull Request Process

1. **Before submitting:**
   - [ ] Fork and create branch from main
   - [ ] Update code following style guidelines
   - [ ] Write or update tests
   - [ ] Update documentation if needed
   - [ ] Run linting: `flake8 .`
   - [ ] Run tests: `pytest`
   - [ ] Format code: `black .` and `isort .`

2. **When submitting:**
   - [ ] Use a clear, descriptive title
   - [ ] Describe changes in detail
   - [ ] Reference related issues (e.g., "Fixes #123")
   - [ ] Include before/after screenshots if applicable
   - [ ] Link to any relevant documentation

3. **PR Title Format:**
   ```
   [TYPE] Brief description of changes
   
   Examples:
   [FEATURE] Add batch processing API
   [FIX] Handle RGBA images correctly
   [DOCS] Update installation guide
   [REFACTOR] Optimize image pipeline
   ```

4. **Example PR Description:**
   ```markdown
   ## Description
   This PR adds support for batch image processing via the API.
   
   ## Changes
   - Add `/api/batch` endpoint
   - Support multiple file uploads
   - Return processing results as JSON array
   
   ## Testing
   - Added comprehensive tests in `test_batch_api.py`
   - Tested with 100+ images
   - Performance: ~500 images/minute
   
   ## Checklist
   - [x] Tests pass
   - [x] Code follows PEP 8
   - [x] Documentation updated
   - [x] No breaking changes
   
   Fixes #45
   ```

5. **Review process:**
   - Maintainers will review your PR
   - Feedback will be provided if changes needed
   - Once approved, your PR will be merged

---

## Recognition

Contributors will be recognized in:
- [README.md](README.md) - Contributors section
- GitHub contributors page
- Release notes (if applicable)

---

## Questions?

Feel free to reach out:
- **Email**: ffjisan804@gmail.com
- **GitHub Issues**: [Ask a question](https://github.com/Gtajisan/NAFNet-Image-Restoration-API/issues)
- **Discussions**: GitHub Discussions (coming soon)

---

## Additional Resources

- [PEP 8 Style Guide](https://www.python.org/dev/peps/pep-0008/)
- [Flask Documentation](https://flask.palletsprojects.com/)
- [Pytest Documentation](https://docs.pytest.org/)
- [Git Documentation](https://git-scm.com/doc)
- [GitHub Guides](https://guides.github.com/)

---

## License

By contributing to NAFNet, you agree that your contributions will be licensed under its MIT License.

---

<div align="center">

**Thank you for contributing! Happy coding! 🚀**

[← Back to README](README.md)

</div>
