use sqlx::PgPool;
use uuid::Uuid;

use crate::{error::Result, models::{Member, CreateMemberRequest}};

pub async fn list(pool: &PgPool) -> Result<Vec<Member>> {
    let members = sqlx::query_as!(
        Member,
        r#"
        SELECT 
            id, 
            first_name, 
            last_name, 
            email, 
            phone, 
            address, 
            date_of_birth, 
            membership_status, 
            join_date, 
            baptism_date, 
            notes, 
            emergency_contact_name, 
            emergency_contact_phone, 
            created_at, 
            updated_at 
        FROM members 
        ORDER BY last_name, first_name
        "#
    )
    .fetch_all(pool)
    .await?;

    Ok(members)
}


pub async fn list_active(pool: &PgPool) -> Result<Vec<Member>> {
    let members = sqlx::query_as!(
        Member,
        r#"
        SELECT 
            id, 
            first_name, 
            last_name, 
            email, 
            phone, 
            address, 
            date_of_birth, 
            membership_status, 
            join_date, 
            baptism_date, 
            notes, 
            emergency_contact_name, 
            emergency_contact_phone, 
            created_at, 
            updated_at 
        FROM members 
        WHERE membership_status = 'active'
        ORDER BY last_name, first_name
        "#
    )
    .fetch_all(pool)
    .await?;

    Ok(members)
}

pub async fn create(pool: &PgPool, req: CreateMemberRequest) -> Result<Member> {
    let member = sqlx::query_as!(
        Member,
        r#"
        INSERT INTO members (
            first_name, 
            last_name, 
            email, 
            phone, 
            address, 
            date_of_birth, 
            membership_status, 
            join_date, 
            baptism_date, 
            notes, 
            emergency_contact_name, 
            emergency_contact_phone
        ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
        RETURNING 
            id, 
            first_name, 
            last_name, 
            email, 
            phone, 
            address, 
            date_of_birth, 
            membership_status, 
            join_date, 
            baptism_date, 
            notes, 
            emergency_contact_name, 
            emergency_contact_phone, 
            created_at, 
            updated_at
        "#,
        req.first_name,
        req.last_name,
        req.email,
        req.phone,
        req.address,
        req.date_of_birth,
        req.membership_status.unwrap_or_else(|| "active".to_string()),
        req.join_date,
        req.baptism_date,
        req.notes,
        req.emergency_contact_name,
        req.emergency_contact_phone
    )
    .fetch_one(pool)
    .await?;

    Ok(member)
}

pub async fn delete(pool: &PgPool, id: &Uuid) -> Result<bool> {
    let result = sqlx::query!(
        "DELETE FROM members WHERE id = $1",
        id
    )
    .execute(pool)
    .await?;

    Ok(result.rows_affected() > 0)
}