use axum::Extension;
use axum::extract::{FromRef, Json, Path, State};
use axum::response::Json as JsonResponse;
use validator::Validate;

use erp_core::error::AppError;
use erp_core::rbac::require_permission;
use erp_core::types::{ApiResponse, TenantContext};
use uuid::Uuid;

use crate::config_state::ConfigState;
use crate::dto::{BatchSaveMenusReq, CreateMenuReq, MenuResp, UpdateMenuReq};
use crate::service::menu_service::MenuService;

#[utoipa::path(
    get,
    path = "/api/v1/config/menus",
    responses(
        (status = 200, description = "成功", body = ApiResponse<Vec<MenuResp>>),
        (status = 401, description = "未授权"),
        (status = 403, description = "权限不足"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// GET /api/v1/config/menus
///
/// 获取当前租户下当前用户角色可见的菜单树。
pub async fn get_menus<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
) -> Result<JsonResponse<ApiResponse<Vec<MenuResp>>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    require_permission(&ctx, "menu.list")?;

    let menus = MenuService::get_menu_tree(ctx.tenant_id, &ctx.roles, &state.db).await?;

    Ok(JsonResponse(ApiResponse::ok(menus)))
}

#[utoipa::path(
    post,
    path = "/api/v1/config/menus",
    request_body = CreateMenuReq,
    responses(
        (status = 200, description = "成功", body = ApiResponse<MenuResp>),
        (status = 401, description = "未授权"),
        (status = 403, description = "权限不足"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// POST /api/v1/config/menus
///
/// 创建单个菜单项。
pub async fn create_menu<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
    Json(req): Json<CreateMenuReq>,
) -> Result<JsonResponse<ApiResponse<MenuResp>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    require_permission(&ctx, "menu.update")?;

    req.validate()
        .map_err(|e| AppError::Validation(e.to_string()))?;

    let resp = MenuService::create(
        ctx.tenant_id,
        ctx.user_id,
        &req,
        &state.db,
        &state.event_bus,
    )
    .await?;

    Ok(JsonResponse(ApiResponse::ok(resp)))
}

#[utoipa::path(
    put,
    path = "/api/v1/config/menus/{id}",
    params(("id" = Uuid, Path, description = "菜单ID")),
    request_body = UpdateMenuReq,
    responses(
        (status = 200, description = "成功", body = ApiResponse<MenuResp>),
        (status = 401, description = "未授权"),
        (status = 403, description = "权限不足"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// PUT /api/v1/config/menus/{id}
///
/// 更新单个菜单项。
pub async fn update_menu<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
    Path(id): Path<Uuid>,
    Json(req): Json<UpdateMenuReq>,
) -> Result<JsonResponse<ApiResponse<MenuResp>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    require_permission(&ctx, "menu.update")?;

    let resp = MenuService::update(id, ctx.tenant_id, ctx.user_id, &req, &state.db).await?;
    Ok(JsonResponse(ApiResponse::ok(resp)))
}

#[utoipa::path(
    delete,
    path = "/api/v1/config/menus/{id}",
    params(("id" = Uuid, Path, description = "菜单ID")),
    request_body = DeleteMenuVersionReq,
    responses(
        (status = 200, description = "成功"),
        (status = 401, description = "未授权"),
        (status = 403, description = "权限不足"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// DELETE /api/v1/config/menus/{id}
///
/// 软删除单个菜单项。需要请求体包含 version 字段用于乐观锁校验。
pub async fn delete_menu<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
    Path(id): Path<Uuid>,
    Json(req): Json<DeleteMenuVersionReq>,
) -> Result<JsonResponse<ApiResponse<()>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    require_permission(&ctx, "menu.update")?;

    MenuService::delete(
        id,
        ctx.tenant_id,
        ctx.user_id,
        req.version,
        &state.db,
        &state.event_bus,
    )
    .await?;
    Ok(JsonResponse(ApiResponse::ok(())))
}

#[utoipa::path(
    put,
    path = "/api/v1/config/menus/batch",
    request_body = BatchSaveMenusReq,
    responses(
        (status = 200, description = "成功"),
        (status = 401, description = "未授权"),
        (status = 403, description = "权限不足"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// PUT /api/v1/config/menus/batch
///
/// 批量保存菜单列表。
pub async fn batch_save_menus<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
    Json(req): Json<BatchSaveMenusReq>,
) -> Result<JsonResponse<ApiResponse<()>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    require_permission(&ctx, "menu.update")?;

    req.validate()
        .map_err(|e| AppError::Validation(e.to_string()))?;

    for item in &req.menus {
        match item.id {
            Some(id) => {
                let version = item.version.unwrap_or(0);
                let update_req = UpdateMenuReq {
                    title: Some(item.title.clone()),
                    path: item.path.clone(),
                    icon: item.icon.clone(),
                    sort_order: item.sort_order,
                    visible: item.visible,
                    permission: item.permission.clone(),
                    role_ids: item.role_ids.clone(),
                    version,
                };
                MenuService::update(id, ctx.tenant_id, ctx.user_id, &update_req, &state.db).await?;
            }
            None => {
                let create_req = CreateMenuReq {
                    parent_id: item.parent_id,
                    title: item.title.clone(),
                    path: item.path.clone(),
                    icon: item.icon.clone(),
                    sort_order: item.sort_order,
                    visible: item.visible,
                    menu_type: item.menu_type.clone(),
                    permission: item.permission.clone(),
                    role_ids: item.role_ids.clone(),
                };
                MenuService::create(
                    ctx.tenant_id,
                    ctx.user_id,
                    &create_req,
                    &state.db,
                    &state.event_bus,
                )
                .await?;
            }
        }
    }

    Ok(JsonResponse(ApiResponse {
        success: true,
        data: None,
        message: Some("菜单批量保存成功".to_string()),
    }))
}

#[utoipa::path(
    get,
    path = "/api/v1/menus/user",
    responses(
        (status = 200, description = "成功", body = ApiResponse<Vec<MenuResp>>),
        (status = 401, description = "未授权"),
    ),
    security(("bearer_auth" = [])),
    tag = "菜单管理"
)]
/// GET /api/v1/menus/user
///
/// 获取当前用户可见的菜单树（无需 menu.list 权限，仅需登录）。
pub async fn get_user_menus<S>(
    State(state): State<ConfigState>,
    Extension(ctx): Extension<TenantContext>,
) -> Result<JsonResponse<ApiResponse<Vec<MenuResp>>>, AppError>
where
    ConfigState: FromRef<S>,
    S: Clone + Send + Sync + 'static,
{
    let menus = MenuService::get_menu_tree(ctx.tenant_id, &ctx.roles, &state.db).await?;

    Ok(JsonResponse(ApiResponse::ok(menus)))
}

/// 删除菜单的乐观锁版本号请求体。
#[derive(Debug, serde::Deserialize, utoipa::ToSchema)]
pub struct DeleteMenuVersionReq {
    pub version: i32,
}
