# Next Steps for Service Layer Migration

## Immediate Actions Required

### 1. Clean Up Current EventService Import
```bash
# Remove unused import from events service
# File: src/services/events.rs line 10
# Remove: db_operations::EventOperations,
```

### 2. Migrate Remaining Modules (In Priority Order)

#### A. Bulletins Service (HIGH PRIORITY)
**Files to create:**
```rust
// src/services/bulletins.rs
pub struct BulletinService;
impl BulletinService {
    pub async fn create_v1(pool: &PgPool, req: CreateBulletinRequest, url_builder: &UrlBuilder) -> Result<Bulletin> {
        let bulletin = db::bulletins::create(pool, req).await?;
        convert_bulletin_to_v1(bulletin, url_builder)
    }
    
    pub async fn update_v1(pool: &PgPool, id: &Uuid, req: UpdateBulletinRequest, url_builder: &UrlBuilder) -> Result<Bulletin> {
        let bulletin = db::bulletins::update(pool, id, req).await?;
        convert_bulletin_to_v1(bulletin, url_builder)
    }
    
    // Add V2 methods with timezone flexibility
}
```

**Files to modify:**
- `src/handlers/bulletins.rs` - Replace direct db calls with BulletinService calls
- `src/handlers/v2/bulletins.rs` - Replace direct db calls with BulletinService calls  
- `src/services/mod.rs` - Add `pub mod bulletins;` and `pub use bulletins::BulletinService;`

#### B. Users/Auth Service (HIGH PRIORITY)
**Files to create:**
```rust
// src/services/auth.rs
pub struct AuthService;
impl AuthService {
    pub async fn authenticate_user(pool: &PgPool, username: &str, password: &str) -> Result<User> {
        let user = db::users::get_by_username(pool, username).await?
            .ok_or_else(|| ApiError::Unauthorized("Invalid credentials".to_string()))?;
        
        let password_hash = db::users::get_password_hash(pool, &user.id).await?;
        
        // Verify password logic here
        // Return user with V1 timezone conversion if needed
    }
    
    pub async fn get_user_by_id(pool: &PgPool, id: &Uuid) -> Result<Option<User>> {
        db::users::get_by_id(pool, id).await
    }
}
```

**Files to modify:**
- `src/handlers/auth.rs` - Replace direct db calls with AuthService calls

#### C. Bible Verses Service
**Files to create:**
```rust
// src/services/bible_verses.rs
pub struct BibleVerseService;
impl BibleVerseService {
    pub async fn get_random_v1(pool: &PgPool) -> Result<Option<BibleVerse>> {
        let verse = BibleVerseOperations::get_random(pool).await?;
        // Apply V1 timezone conversion if needed
    }
    
    pub async fn search_v1(pool: &PgPool, query: &str, limit: i64) -> Result<Vec<BibleVerse>> {
        let verses = BibleVerseOperations::search(pool, query, limit).await?;
        // Apply V1 timezone conversion if needed
    }
}
```

#### D. Schedule Service  
**Files to create:**
```rust
// src/services/schedule.rs
pub struct ScheduleService;
impl ScheduleService {
    pub async fn get_by_date_v1(pool: &PgPool, date: NaiveDate) -> Result<Option<Schedule>> {
        ScheduleOperations::get_by_date(pool, date).await
    }
    
    pub async fn get_for_range_v1(pool: &PgPool, start: NaiveDate, end: NaiveDate) -> Result<Vec<Schedule>> {
        ScheduleOperations::get_for_range(pool, start, end).await
    }
}
```

#### E. Config Service (LOW PRIORITY)
**Files to create:**
```rust  
// src/services/config.rs
pub struct ConfigService;
impl ConfigService {
    pub async fn update_config(pool: &PgPool, config: ChurchConfig) -> Result<ChurchConfig> {
        // Add business logic validation here
        db::config::update_config(pool, config).await
    }
}
```

## Migration Checklist Template

For each module, follow this checklist:

### Service Creation
- [ ] Create `src/services/{module}.rs`
- [ ] Implement `{Module}Service` struct  
- [ ] Add V1 methods that call `db::{module}::*` functions
- [ ] Add V2 methods with timezone flexibility
- [ ] Apply proper timezone conversions and URL building

### Handler Migration  
- [ ] Update imports to use service instead of direct db calls
- [ ] Replace `db::{module}::*` calls with `{Module}Service::*` calls
- [ ] Ensure handlers stay thin (no business logic)
- [ ] Test that all endpoints still work

### Module Registration
- [ ] Add `pub mod {module};` to `src/services/mod.rs`
- [ ] Add `pub use {module}::{Module}Service;` to `src/services/mod.rs`

### Verification
- [ ] Run `cargo build` and confirm specific "unused" warnings eliminated
- [ ] Test API endpoints to ensure functionality preserved
- [ ] Verify timezone conversion working correctly

## Expected Results After Full Migration

### Warning Reduction
- **Current**: 64 warnings
- **Target**: ~45-50 warnings
- **Eliminated**: ~15-20 legitimate "unused function" warnings

### Architecture Achieved
- **Thin handlers** - HTTP concerns only
- **Service layer** - All business logic centralized  
- **Database layer** - Data access properly abstracted
- **Dumb frontend** - No logic, just displays backend data

### Maintainability Gains
- Business logic changes only require service layer updates
- Easy to add caching, validation, authorization at service level
- Clear separation of concerns
- Better testability

## Files That Will Remain "Unused" (Legitimate)
These are utility functions for future features and can be ignored:
- `src/utils/response.rs` helper functions
- `src/utils/database.rs` generic utilities  
- `src/utils/datetime.rs` display formatting functions
- `src/utils/validation.rs` optional validation methods
- `src/utils/handlers.rs` generic handler utilities
- Model structs for future API versions

## Timeline Estimate
- **Bulletins**: 30 minutes
- **Users/Auth**: 45 minutes  
- **Bible Verses**: 20 minutes
- **Schedule**: 20 minutes
- **Config**: 15 minutes
- **Total**: ~2.5 hours

## Success Criteria
1. All database functions showing "unused" warnings are eliminated
2. Application builds and runs without breaking changes
3. API endpoints continue to work exactly as before
4. Service layer properly centralizes business logic
5. Handlers are thin and focused on HTTP concerns only