# Filament Article Management Features

This document describes the new features added to the Filament admin panel for article management.

## 🎯 New Features

### 1. Enhanced Article Filters
The article table now includes comprehensive filters above the table:

- **Processing State**: Filter by article processing state (fetched, pending_summary, summarized, etc.)
- **Keywords**: Filter by article keywords
- **Article Type**: Filter by article type
- **Journal**: Filter by journal name
- **Score Range**: Filter by overall validation score (min/max)
- **Publication Date**: Filter by publication date range
- **Created Date**: Filter by when the article was created in the system

### 2. User Access Control
- **Admin Users**: Can see all articles in all states
- **Non-Admin Users**: Can only see articles with state 'summarized'
- **User Management**: New UserResource for managing users and admin status

### 3. Enhanced Article Table
- **Score Column**: Shows validation score with color coding
  - Green: ≥85% (passing)
  - Yellow: 70-84% (warning)
  - Red: <70% (failing)
- **State Colors**: Updated state badges with appropriate colors
- **Better Organization**: Filters are organized and searchable

## 🗄️ Database Changes

### New Migration Required
```bash
php artisan migrate
```

This adds:
- `is_admin` field to `users` table
- `state`, `last_processed_at`, `processing_notes` fields to `articles` table

## 👥 User Management

### Creating Admin Users

#### Option 1: Command Line
```bash
php artisan user:create-admin admin@example.com "Admin User" password123
```

#### Option 2: Filament Admin Panel
1. Go to Administration → Users
2. Click "Create User"
3. Fill in the form and toggle "Admin Access" to ON
4. Save the user

### User Permissions

| User Type | Can See | Can Edit | Can Manage Users |
|-----------|---------|----------|------------------|
| Admin | All articles | Yes | Yes |
| Non-Admin | Only summarized articles | No | No |

## 🎨 Filter Usage

### Using Filters
1. **State Filter**: Click the "Processing State" filter to select one or more states
2. **Score Filter**: Use the "Score" filter to set minimum and maximum score ranges
3. **Date Filters**: Use date pickers to filter by publication or creation dates
4. **Text Filters**: Use searchable dropdowns for keywords, article types, and journals

### Filter Combinations
- You can combine multiple filters
- All filters work together (AND logic)
- Clear individual filters or use "Clear all filters"

## 🔧 Configuration

### Admin Check Logic
The system checks for admin access using the `is_admin` field in the users table. You can modify this logic in `app/Filament/Resources/ArticleResource.php`:

```php
public static function modifyQueryForUser(Builder $query): Builder
{
    $user = Auth::user();
    
    // If user is admin, they see all articles
    if ($user && $user->isAdmin()) {
        return $query;
    }
    
    // Non-admin users only see summarized articles
    return $query->where('state', 'summarized');
}
```

### Customizing Admin Logic
If you want to use a different method to determine admin status:

1. **Role-based**: Add a `role` field to users table
2. **Email-based**: Check specific email addresses
3. **Permission-based**: Use Laravel's permission system

Example with roles:
```php
if ($user && $user->role === 'admin') {
    return $query;
}
```

## 📊 Article States

The system now tracks articles through these states:

| State | Description | Color |
|-------|-------------|-------|
| `fetched` | Article fetched from PMC | Gray |
| `pending_summary` | Queued for summarization | Yellow |
| `summarized` | Summarization completed | Green |
| `pending_validation` | Queued for validation | Blue |
| `validated` | Validation completed | Purple |
| `active` | Article activated (score ≥85%) | Green |
| `failed` | Processing failed | Red |

## 🚀 Getting Started

### 1. Run Migrations
```bash
php artisan migrate
```

### 2. Create Admin User
```bash
php artisan user:create-admin admin@example.com "Admin User" yourpassword
```

### 3. Access Admin Panel
- Go to `/admin`
- Login with your admin credentials
- Navigate to Articles to see the enhanced table

### 4. Create Regular Users
- Go to Administration → Users
- Create users without admin access
- These users will only see summarized articles

## 🔍 Monitoring

### Check User Access
```bash
# See what articles a user can access
php artisan tinker
>>> $user = App\Models\User::where('email', 'user@example.com')->first();
>>> $user->isAdmin() ? 'Admin - sees all' : 'Non-admin - sees summarized only';
```

### Article Distribution
```bash
# Check article state distribution
php artisan articles:monitor
```

## 🛠️ Troubleshooting

### Users Can't See Articles
1. Check if user has `is_admin = true` in database
2. Verify articles have `state = 'summarized'` for non-admin users
3. Check user authentication status

### Filters Not Working
1. Clear browser cache
2. Check if database fields exist
3. Verify filter options are populated

### Admin Access Issues
1. Run `php artisan user:create-admin` to create admin user
2. Check database for `is_admin` field
3. Verify user model has `isAdmin()` method

## 📝 Notes

- Non-admin users will only see articles in the 'summarized' state
- Admin users can see and manage all articles regardless of state
- The filtering system is optimized for performance with large datasets
- All filters support multiple selections where appropriate
- Score filtering works with decimal values (e.g., 85.5%) 
