use axum::{extract::{Path, State}, Json};
use uuid::Uuid;

use crate::{
    error::Result,
    models::{Member, ApiResponse, CreateMemberRequest},
    db::members,
    utils::response::success_response,
    AppState,
};

pub async fn list(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<Vec<Member>>>> {
    let members_list = members::list(&state.pool).await?;
    
    Ok(success_response(members_list))
}

pub async fn list_active(
    State(state): State<AppState>,
) -> Result<Json<ApiResponse<Vec<Member>>>> {
    let members_list = members::list_active(&state.pool).await?;
    
    Ok(success_response(members_list))
}

pub async fn create(
    State(state): State<AppState>,
    Json(req): Json<CreateMemberRequest>,
) -> Result<Json<ApiResponse<Member>>> {
    let member = members::create(&state.pool, req).await?;
    
    Ok(Json(ApiResponse {
        success: true,
        data: Some(member),
        message: Some("Member created successfully".to_string()),
    }))
}

pub async fn delete(
    State(state): State<AppState>,
    Path(id): Path<Uuid>,
) -> Result<Json<ApiResponse<bool>>> {
    let deleted = members::delete(&state.pool, &id).await?;
    
    if deleted {
        Ok(Json(ApiResponse {
            success: true,
            data: Some(true),
            message: Some("Member deleted successfully".to_string()),
        }))
    } else {
        Ok(Json(ApiResponse {
            success: false,
            data: Some(false),
            message: Some("Member not found".to_string()),
        }))
    }
}