#![allow(unused_imports)]
use super::*;

#[test]
fn test_owner_has_full_permissions() {
    let custom = HashMap::new();

    assert!(has_permission(
        "owner",
        ORGANIZATION_RESOURCE,
        UPDATE_ACTION,
        &custom,
        &[]
    ));
    assert!(has_permission(
        "owner",
        ORGANIZATION_RESOURCE,
        DELETE_ACTION,
        &custom,
        &[]
    ));
    assert!(has_permission(
        "owner",
        MEMBER_RESOURCE,
        CREATE_ACTION,
        &custom,
        &[]
    ));
    assert!(has_permission(
        "owner",
        INVITATION_RESOURCE,
        CANCEL_ACTION,
        &custom,
        &[]
    ));
}

#[test]
fn test_admin_cannot_delete_organization() {
    let custom = HashMap::new();

    assert!(has_permission(
        "admin",
        ORGANIZATION_RESOURCE,
        UPDATE_ACTION,
        &custom,
        &[]
    ));
    assert!(!has_permission(
        "admin",
        ORGANIZATION_RESOURCE,
        DELETE_ACTION,
        &custom,
        &[]
    ));
}

#[test]
fn test_member_has_no_permissions() {
    let custom = HashMap::new();

    assert!(!has_permission(
        "member",
        ORGANIZATION_RESOURCE,
        UPDATE_ACTION,
        &custom,
        &[]
    ));
    assert!(!has_permission(
        "member",
        MEMBER_RESOURCE,
        CREATE_ACTION,
        &custom,
        &[]
    ));
}

#[test]
fn test_composite_roles() {
    let custom = HashMap::new();

    assert!(has_permission_any(
        "member,admin",
        ORGANIZATION_RESOURCE,
        UPDATE_ACTION,
        &custom,
        &[]
    ));

    assert!(!has_permission_any(
        "member",
        ORGANIZATION_RESOURCE,
        UPDATE_ACTION,
        &custom,
        &[]
    ));
}

#[test]
fn test_custom_grants_support_billing_events() {
    let mut custom = HashMap::new();
    custom.insert(
        "billing-bot".to_string(),
        crate::plugins::organization::RolePermissions {
            grants: HashMap::from([(
                "payment-link".to_string(),
                vec!["paid".to_string(), "failed".to_string()],
            )]),
            ..Default::default()
        },
    );

    assert!(has_permission(
        "billing-bot",
        "payment-link",
        "paid",
        &custom,
        &[]
    ));
    assert!(!has_permission(
        "billing-bot",
        "payment-link",
        "refunded",
        &custom,
        &[]
    ));
}

#[test]
fn test_custom_grant_keys_support_billing_events() {
    let mut custom = HashMap::new();
    custom.insert(
        "billing-webhook".to_string(),
        crate::plugins::organization::RolePermissions {
            grant_keys: vec!["payment-link.paid".to_string()],
            ..Default::default()
        },
    );

    let catalog = vec![crate::plugins::grants::GrantDescriptor {
        key: "payment-link.paid".to_string(),
        resource: "payment-link".to_string(),
        action: "paid".to_string(),
        description: "Grant access when a payment-link payment has succeeded.".to_string(),
    }];

    assert!(has_permission(
        "billing-webhook",
        "payment-link",
        "paid",
        &custom,
        &catalog
    ));
    assert!(!has_permission(
        "billing-webhook",
        "payment-link",
        "failed",
        &custom,
        &catalog
    ));
}

#[test]
fn test_custom_right_modules_support_billing_events() {
    let mut custom = HashMap::new();
    custom.insert(
        "billing-webhook".to_string(),
        crate::plugins::organization::RolePermissions {
            right_modules: vec![athena_auth_core::NativeRightModuleInput {
                key: "billing".to_string(),
                right_keys: vec!["payment-link.paid".to_string()],
            }],
            ..Default::default()
        },
    );

    let catalog = vec![crate::plugins::grants::GrantDescriptor {
        key: "payment-link.paid".to_string(),
        resource: "payment-link".to_string(),
        action: "paid".to_string(),
        description: "Grant access when a payment-link payment has succeeded.".to_string(),
    }];

    assert!(has_permission(
        "billing-webhook",
        "payment-link",
        "paid",
        &custom,
        &catalog
    ));
    assert!(!has_permission(
        "billing-webhook",
        "payment-link",
        "failed",
        &custom,
        &catalog
    ));
}
