#!/bin/bash

# Add BibleVerse model to models.rs
cat >> src/models.rs << 'EOF'

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BibleVerse {
    pub id: Uuid,
    pub reference: String,
    pub text: String,
    pub is_active: bool,
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}
EOF

# Create handlers/bible_verses.rs
cat > src/handlers/bible_verses.rs << 'EOF'
use crate::{db, error::Result, models::{ApiResponse, BibleVerse}, AppState};
use axum::{extract::State, Json};

pub async fn random(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<BibleVerse>>> {
    let verse = db::bible_verses::get_random(&state.pool).await?;
    Ok(Json(ApiResponse {
        success: true,
        data: verse,
        message: None,
    }))
}

pub async fn list(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<Vec<BibleVerse>>>> {
    let verses = db::bible_verses::list(&state.pool).await?;
    Ok(Json(ApiResponse {
        success: true,
        data: Some(verses),
        message: None,
    }))
}
EOF

# Create db/bible_verses.rs
cat > src/db/bible_verses.rs << 'EOF'
use sqlx::PgPool;
use uuid::Uuid;
use crate::{error::Result, models::BibleVerse};

pub async fn get_random(pool: &PgPool) -> Result<Option<BibleVerse>> {
    let verse = sqlx::query_as!(
        BibleVerse,
        "SELECT * FROM bible_verses WHERE is_active = true ORDER BY RANDOM() LIMIT 1"
    )
    .fetch_optional(pool)
    .await?;

    Ok(verse)
}

pub async fn list(pool: &PgPool) -> Result<Vec<BibleVerse>> {
    let verses = sqlx::query_as!(
        BibleVerse,
        "SELECT * FROM bible_verses WHERE is_active = true ORDER BY reference"
    )
    .fetch_all(pool)
    .await?;

    Ok(verses)
}
EOF

# Add module to handlers/mod.rs
echo "pub mod bible_verses;" >> src/handlers/mod.rs

# Add module to db/mod.rs  
echo "pub mod bible_verses;" >> src/db/mod.rs

echo "✅ Bible verses files created!"
echo "Don't forget to add the routes to main.rs:"
echo '.route("/api/bible_verses/random", get(handlers::bible_verses::random))'
echo '.route("/api/bible_verses", get(handlers::bible_verses::list))'
