<?php

/**
 * Admin view for the Work Log page.
 *
 * @package MediaTasker
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Ensure the Work Log class is available.
if ( !class_exists( 'Mediat_Work_Log' ) ) {
    require_once MEDIAT_PLUGIN_DIR . 'includes/class-mediat_work-log.php';
}
$mediat_args = array(
    'limit'  => 50,
    'offset' => 0,
);
$mediat_logs = Mediat_Work_Log::get_logs( $mediat_args );
$mediat_total = Mediat_Work_Log::get_total_logs( $mediat_args );
// Quick helper to format action types visually.
function mediat_format_action_type(  $type  ) {
    switch ( $type ) {
        case 'upload':
            return '<span class="dashicons dashicons-upload" style="color:#2271b1;" title="Upload"></span> Upload';
        case 'status_change':
            return '<span class="dashicons dashicons-update" style="color:#f56e28;" title="Status Change"></span> Status Change';
        case 'comment_added':
            return '<span class="dashicons dashicons-admin-comments" style="color:#00a0d2;" title="Comment"></span> Comment Added';
        case 'comment_edited':
            return '<span class="dashicons dashicons-admin-comments" style="color:#00a0d2;" title="Comment Edited"></span> Comment Edited';
        case 'comment_deleted':
            return '<span class="dashicons dashicons-trash" style="color:#d63638;" title="Comment Deleted"></span> Comment Deleted';
        case 'media_deleted':
            return '<span class="dashicons dashicons-trash" style="color:#d63638;" title="Media Deleted"></span> Media Deleted';
        default:
            return esc_html( ucwords( str_replace( '_', ' ', $type ) ) );
    }
}

?>
<div class="wrap mediat_admin-wrap">
    <h1 class="wp-heading-inline">
        <?php 
esc_html_e( 'MediaTasker Work Log', 'mediatasker' );
?>
    </h1>
    <p>
        <?php 
esc_html_e( 'A chronological list of all logged events across the task hub.', 'mediatasker' );
?>
    </p>

    <div id="mediat_admin-notice-container" class="notice notice-error is-dismissible"
        style="display:none; margin-bottom: 20px;">
        <p></p>
        <button type="button" class="notice-dismiss">
            <span class="screen-reader-text"><?php 
esc_html_e( 'Dismiss this notice.', 'mediatasker' );
?></span>
        </button>
    </div>

    <!-- Filters Section -->
    <div class="tablenav top mediat_work-log-filters">
        <div class="alignleft actions">
            <select name="mediat_log_action_type" id="mediat_log_action_type">
                <option value="">
                    <?php 
esc_html_e( 'All Actions', 'mediatasker' );
?>
                </option>
                <option value="upload">
                    <?php 
esc_html_e( 'Uploads', 'mediatasker' );
?>
                </option>
                <option value="status_change">
                    <?php 
esc_html_e( 'Status Changes', 'mediatasker' );
?>
                </option>
                <option value="comment_added">
                    <?php 
esc_html_e( 'Comments Added', 'mediatasker' );
?>
                </option>
                <option value="comment_edited">
                    <?php 
esc_html_e( 'Comments Edited', 'mediatasker' );
?>
                </option>
                <option value="comment_deleted">
                    <?php 
esc_html_e( 'Comments Deleted', 'mediatasker' );
?>
                </option>
                <option value="media_deleted">
                    <?php 
esc_html_e( 'Media Deleted', 'mediatasker' );
?>
                </option>
            </select>

            <?php 
?>

            <input type="date" id="mediat_log_date_from" placeholder="From Date" />
            <input type="date" id="mediat_log_date_to" placeholder="To Date" />

            <button type="button" class="button button-secondary" id="mediat_log_filter_btn">
                <?php 
esc_html_e( 'Filter', 'mediatasker' );
?>
            </button>
        </div>

        <div class="alignright">
            <?php 
do_action( 'mediat_work_log_export_buttons' );
?>

            <?php 
?>
        </div>
        <div class="clear"></div>
    </div>

    <!-- The Log Table -->
    <table class="wp-list-table widefat fixed striped mediat_log-table">
        <thead>
            <tr>
                <th scope="col" class="manage-column column-date">
                    <?php 
esc_html_e( 'Date/Time', 'mediatasker' );
?>
                </th>
                <th scope="col" class="manage-column column-user">
                    <?php 
esc_html_e( 'User', 'mediatasker' );
?>
                </th>
                <th scope="col" class="manage-column column-action">
                    <?php 
esc_html_e( 'Action Type', 'mediatasker' );
?>
                </th>
                <th scope="col" class="manage-column column-task">
                    <?php 
esc_html_e( 'Task', 'mediatasker' );
?>
                </th>
                <th scope="col" class="manage-column column-details">
                    <?php 
esc_html_e( 'Details', 'mediatasker' );
?>
                </th>
            </tr>
        </thead>
        <tbody id="mediat_log_table_body">
            <?php 
if ( empty( $mediat_logs ) ) {
    ?>
                <tr>
                    <td colspan="5">
                        <?php 
    esc_html_e( 'No events logged yet.', 'mediatasker' );
    ?>
                    </td>
                </tr>
            <?php 
} else {
    ?>
                <?php 
    foreach ( $mediat_logs as $mediat_log ) {
        ?>
                    <?php 
        $mediat_user_name = 'Guest/System';
        if ( $mediat_log->user_id ) {
            $mediat_user = get_userdata( $mediat_log->user_id );
            if ( $mediat_user ) {
                $mediat_user_name = $mediat_user->display_name;
            }
        }
        $mediat_task_name = 'Deleted/Unknown';
        if ( $mediat_log->task_id && get_post_type( $mediat_log->task_id ) === 'mediat_task' ) {
            $mediat_task_name = get_the_title( $mediat_log->task_id );
        } else {
            $mediat_details = json_decode( $mediat_log->details, true );
            if ( isset( $mediat_details['file_name'] ) ) {
                $mediat_task_name = $mediat_details['file_name'];
            }
        }
        $mediat_details_str = '';
        if ( $mediat_log->details ) {
            $mediat_details = json_decode( $mediat_log->details, true );
            if ( isset( $mediat_details['new_status'] ) ) {
                $mediat_details_str = 'Status: ' . $mediat_details['new_status'];
            } elseif ( isset( $mediat_details['comment_id'] ) ) {
                $mediat_details_str = 'Comment ID: ' . $mediat_details['comment_id'];
            }
        }
        ?>
                    <tr>
                        <td>
                            <?php 
        echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $mediat_log->created_at ) ) );
        ?>
                        </td>
                        <td>
                            <?php 
        echo esc_html( $mediat_user_name );
        ?>
                        </td>
                        <td>
                            <?php 
        echo wp_kses( mediat_format_action_type( $mediat_log->action_type ), array(
            'span' => array(
                'class' => array(),
                'style' => array(),
                'title' => array(),
            ),
        ) );
        ?>
                        </td>
                        <td>
                            <?php 
        if ( get_post_type( $mediat_log->task_id ) === 'mediat_task' ) {
            ?>
                                <a href="<?php 
            echo esc_url( get_edit_post_link( $mediat_log->task_id ) );
            ?>">
                                    <?php 
            echo esc_html( $mediat_task_name );
            ?>
                                </a>
                            <?php 
        } else {
            ?>
                                <?php 
            echo esc_html( $mediat_task_name );
            ?>
                            <?php 
        }
        ?>
                        </td>
                        <td>
                            <?php 
        echo esc_html( $mediat_details_str );
        ?>
                        </td>
                    </tr>
                <?php 
    }
    ?>
            <?php 
}
?>
        </tbody>
    </table>

    <div class="tablenav bottom">
        <div class="alignleft actions">
            <button type="button" class="button" id="mediat_log_load_more"
                style="<?php 
echo ( $mediat_total <= 50 ? 'display:none;' : '' );
?>">
                <?php 
esc_html_e( 'Load More Logs', 'mediatasker' );
?>
            </button>
            <span class="spinner mediat_log-spinner"></span>
        </div>
        <div class="clear"></div>
    </div>
</div>
