<?php
/**
 * User Files Block Template
 *
 * @since 4.2.0
 *
 * @var WP_User $user User object
 * @var array $attributes Block attributes
 * @var string $content Block content
 * @var WP_Block $block Block instance
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! $user ) {
    return;
}

$meta_field_key = ! empty( $attributes['metaFieldKey'] ) ? $attributes['metaFieldKey'] : '';
$field_label = ! empty( $attributes['fieldLabel'] ) ? $attributes['fieldLabel'] : '';
$show_label = isset( $attributes['showLabel'] ) ? (bool) $attributes['showLabel'] : true;

// Don't show anything if no field key
if ( empty( $meta_field_key ) ) {
    return;
}

// Get the custom field value
$field_value = get_user_meta( $user->ID, $meta_field_key, true );

// Don't show anything if no value
if ( empty( $field_value ) ) {
    return;
}

// Use field key as label if no label provided
if ( empty( $field_label ) ) {
    $field_label = ucwords( str_replace( [ '_', '-' ], ' ', $meta_field_key ) );
}

// Use the FileHelper to convert file IDs to file data
$files = \WPUF\UserDirectory\FileHelper::convert_file_ids_to_data( $field_value );

// Don't show anything if no files
if ( empty( $files ) ) {
    return;
}

// Role-based visibility: show only if the profile user has one of the selected roles
$selected_roles = $attributes['profileUserRoles'] ?? [ 'all' ];

if ( ! is_array( $selected_roles ) || empty( $selected_roles ) ) {
    $selected_roles = [ 'all' ];
}
if ( ! in_array( 'all', $selected_roles, true ) ) {
    $user_roles = is_array( $user->roles ?? null ) ? $user->roles : [];
    if ( empty( array_intersect( $user_roles, $selected_roles ) ) ) {
        return;
    }
}

// Viewer access check using shared function
$field_config = [
    'viewer_roles'              => $attributes['viewerRoles'] ?? [ 'all' ],
    'viewer_subscription_packs' => $attributes['viewerSubscriptionPacks'] ?? [ 'all' ],
    'viewer_access_logic'       => $attributes['viewerAccessLogic'] ?? 'and',
    'field_view_type'           => $attributes['fieldViewType'] ?? 'hide',
    'custom_message'            => $attributes['customMessage'] ?? '',
];

$access_result = wpuf_ud_check_field_viewer_access( $field_config );

if ( ! $access_result['has_access'] ) {
    if ( ! empty( $access_result['custom_message'] ) ) {
        $wrapper_attributes = wpuf_get_block_wrapper_attributes( $attributes, 'files', [
            'class' => 'wpuf-user-files wpuf-restricted-message',
            'data-user-id'   => $user->ID,
            'data-field-key' => $meta_field_key,
        ] );
        echo '<div ' . $wrapper_attributes . '><span class="wpuf-field-message">' . wp_kses_post( $access_result['custom_message'] ) . '</span></div>';

        return;
    }

    return;
}

// Get block wrapper attributes (includes styling from block editor)
$wrapper_attributes = wpuf_get_block_wrapper_attributes( $attributes, 'files', [
    'class' => 'wpuf-user-files',
    'data-user-id' => $user->ID,
    'data-field-key' => $meta_field_key,
] );


?>
<div <?php echo $wrapper_attributes; ?>>
    <div class="wpuf-files-content">
        <?php if ( $show_label && ! empty( $field_label ) ) : ?>
            <div class="wpuf-files-label">
                <?php echo esc_html( $field_label ); ?>
            </div>
        <?php endif; ?>

        <?php echo \WPUF\UserDirectory\FileHelper::render_file_grid( $files ); ?>
    </div>
</div>
