<?php
/**
 * AI Review Moderation metabox template.
 *
 * @package SAAI
 *
 * @var array  $result            The moderation result data.
 * @var string $moderation_status The moderation status (pending/approved/flagged/rejected).
 * @var string $moderation_date   The moderation date.
 * @var int    $confidence        The normalized confidence score (0-100).
 */

if ( ! defined( 'ABSPATH' ) ) {
    die( 'You are not allowed to call this page directly.' );
}

$decision = isset( $result['decision'] ) ? $result['decision'] : '';
$flags    = isset( $result['flags'] ) && is_array( $result['flags'] ) ? $result['flags'] : array();
$reasons  = isset( $result['reasons'] ) && is_array( $result['reasons'] ) ? $result['reasons'] : array();
$summary  = isset( $result['admin_summary'] ) ? $result['admin_summary'] : '';

use SAAI\AI_Agents\Review_Moderation_AI;

$status_labels = Review_Moderation_AI::get_status_labels();
$status_colors = Review_Moderation_AI::get_status_colors();

$status_label = isset( $status_labels[ $moderation_status ] ) ? $status_labels[ $moderation_status ] : $moderation_status;
$status_color = isset( $status_colors[ $moderation_status ] ) ? $status_colors[ $moderation_status ] : '#50575e';
?>

<div class="saai-review-moderation-metabox" style="padding: 4px 0;">

    <table class="form-table" style="margin: 0;">
        <tbody>
            <tr>
                <th scope="row" style="padding: 8px 10px 8px 0; width: 140px;">
                    <?php esc_html_e( 'Status', 'storeagent-ai-for-woocommerce' ); ?>
                </th>
                <td style="padding: 8px 10px;">
                    <span style="display: inline-block; padding: 2px 10px; border-radius: 3px; font-size: 12px; font-weight: 600; color: #fff; background-color: <?php echo esc_attr( $status_color ); ?>;">
                        <?php echo esc_html( $status_label ); ?>
                    </span>
                </td>
            </tr>
            <tr>
                <th scope="row" style="padding: 8px 10px 8px 0;">
                    <?php esc_html_e( 'AI Decision', 'storeagent-ai-for-woocommerce' ); ?>
                </th>
                <td style="padding: 8px 10px;">
                    <?php echo esc_html( ucfirst( $decision ) ); ?>
                </td>
            </tr>
            <tr>
                <th scope="row" style="padding: 8px 10px 8px 0;">
                    <?php esc_html_e( 'Confidence', 'storeagent-ai-for-woocommerce' ); ?>
                </th>
                <td style="padding: 8px 10px;">
                    <strong><?php echo esc_html( $confidence ); ?>%</strong>
                </td>
            </tr>
            <?php if ( $moderation_date ) : ?>
                <tr>
                    <th scope="row" style="padding: 8px 10px 8px 0;">
                        <?php esc_html_e( 'Moderated On', 'storeagent-ai-for-woocommerce' ); ?>
                    </th>
                    <td style="padding: 8px 10px;">
                        <?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $moderation_date ) ) ); ?>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if ( ! empty( $flags ) ) : ?>
                <tr>
                    <th scope="row" style="padding: 8px 10px 8px 0; vertical-align: top;">
                        <?php esc_html_e( 'Flags', 'storeagent-ai-for-woocommerce' ); ?>
                    </th>
                    <td style="padding: 8px 10px;">
                        <?php foreach ( $flags as $flag ) : ?>
                            <span style="display: inline-block; margin: 0 4px 4px 0; padding: 2px 8px; border-radius: 3px; font-size: 12px; background-color: #fce4e4; color: #8a1f1f;">
                                <?php echo esc_html( $flag ); ?>
                            </span>
                        <?php endforeach; ?>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if ( $summary ) : ?>
                <tr>
                    <th scope="row" style="padding: 8px 10px 8px 0; vertical-align: top;">
                        <?php esc_html_e( 'AI Summary', 'storeagent-ai-for-woocommerce' ); ?>
                    </th>
                    <td style="padding: 8px 10px;">
                        <p style="margin: 0; color: #50575e;"><?php echo esc_html( $summary ); ?></p>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if ( ! empty( $reasons ) ) : ?>
                <tr>
                    <th scope="row" style="padding: 8px 10px 8px 0; vertical-align: top;">
                        <?php esc_html_e( 'Reasons', 'storeagent-ai-for-woocommerce' ); ?>
                    </th>
                    <td style="padding: 8px 10px;">
                        <ul style="margin: 0; padding-left: 16px; list-style-type: disc;">
                            <?php foreach ( $reasons as $reason ) : ?>
                                <li style="margin-bottom: 4px;"><?php echo esc_html( $reason ); ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
