<?php
/**
 * Author: Rymera Web Co.
 *
 * @package SAAI\Templates\Admin
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$attachment_id = $attachment_id ?? 0;
$image_url     = $image_url ?? '';
$current_alt   = $current_alt ?? '';

$attachment = null;
if ( $attachment_id ) {
    $attachment = get_post( $attachment_id );
    if ( ! $attachment || 'attachment' !== $attachment->post_type || ! wp_attachment_is_image( $attachment ) ) {
        return;
    }
}

// Get default fields from settings.
$default_fields = array( 'alt' ); // Default fallback.
if ( class_exists( '\SAAI\AI_Agents\Image_Alt_Tags_AI' ) ) {
    $image_alt_tags_ai = \SAAI\AI_Agents\Image_Alt_Tags_AI::instance();
    $settings          = $image_alt_tags_ai->get_settings();
    if ( isset( $settings['default_fields'] ) && is_array( $settings['default_fields'] ) ) {
        $default_fields = $settings['default_fields'];
    }
}

?>
<div class="saai-image-alt-tags-modal" id="saai-image-alt-tags-modal" style="display: none;">
    <div class="saai-modal-backdrop" data-action="close"></div>
    <div class="saai-modal-content">
        <div class="saai-modal-header">
            <h2><?php esc_html_e( 'Generate Image Alt Tags with AI', 'storeagent-ai-for-woocommerce' ); ?></h2>
            <button type="button" class="saai-modal-close" data-action="close" aria-label="<?php esc_attr_e( 'Close modal', 'storeagent-ai-for-woocommerce' ); ?>">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="saai-modal-body">
            <div class="saai-form-section">
                <h3><?php esc_html_e( 'What would you like to generate?', 'storeagent-ai-for-woocommerce' ); ?></h3>
                <div class="saai-checkbox-group">
                    <label class="saai-checkbox-label saai-checkbox-label-check-all">
                        <input type="checkbox" id="saai-check-all" value="1" />
                        <span class="saai-checkbox-text" style="font-weight: 600;"><?php esc_html_e( 'Check All', 'storeagent-ai-for-woocommerce' ); ?></span>
                    </label>
                    <div style="border-top: 1px solid #ddd; margin: 8px 0;"></div>
                    <label class="saai-checkbox-label">
                        <input type="checkbox" name="generate_alt" class="saai-generate-field" value="1" <?php checked( in_array( 'alt', $default_fields, true ) ); ?> />
                        <span class="saai-checkbox-text"><?php esc_html_e( 'Alt Text (for accessibility)', 'storeagent-ai-for-woocommerce' ); ?></span>
                    </label>
                    <label class="saai-checkbox-label">
                        <input type="checkbox" name="generate_caption" class="saai-generate-field" value="1" <?php checked( in_array( 'caption', $default_fields, true ) ); ?> />
                        <span class="saai-checkbox-text"><?php esc_html_e( 'Caption', 'storeagent-ai-for-woocommerce' ); ?></span>
                    </label>
                    <label class="saai-checkbox-label">
                        <input type="checkbox" name="generate_title" class="saai-generate-field" value="1" <?php checked( in_array( 'title', $default_fields, true ) ); ?> />
                        <span class="saai-checkbox-text"><?php esc_html_e( 'Title', 'storeagent-ai-for-woocommerce' ); ?></span>
                    </label>
                    <label class="saai-checkbox-label">
                        <input type="checkbox" name="generate_description" class="saai-generate-field" value="1" <?php checked( in_array( 'description', $default_fields, true ) ); ?> />
                        <span class="saai-checkbox-text"><?php esc_html_e( 'Description', 'storeagent-ai-for-woocommerce' ); ?></span>
                    </label>
                </div>
            </div>

            <div class="saai-current-values">
                <h3><?php esc_html_e( 'Current Values', 'storeagent-ai-for-woocommerce' ); ?></h3>
                <div class="saai-current-values-empty"><?php esc_html_e( 'Select fields above to see their current values.', 'storeagent-ai-for-woocommerce' ); ?></div>
                <div class="saai-current-field saai-current-field-alt" data-field="alt">
                    <label><?php esc_html_e( 'Alt Text:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-current-value"><?php echo esc_html( $current_alt ? $current_alt : __( 'None', 'storeagent-ai-for-woocommerce' ) ); ?></div>
                </div>
                <div class="saai-current-field saai-current-field-caption" data-field="caption">
                    <label><?php esc_html_e( 'Caption:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-current-value"><?php echo esc_html( ( $attachment && $attachment->post_excerpt ) ? $attachment->post_excerpt : __( 'None', 'storeagent-ai-for-woocommerce' ) ); ?></div>
                </div>
                <div class="saai-current-field saai-current-field-title" data-field="title">
                    <label><?php esc_html_e( 'Title:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-current-value"><?php echo esc_html( ( $attachment && $attachment->post_title ) ? $attachment->post_title : __( 'None', 'storeagent-ai-for-woocommerce' ) ); ?></div>
                </div>
                <div class="saai-current-field saai-current-field-description" data-field="description">
                    <label><?php esc_html_e( 'Description:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-current-value"><?php echo esc_html( ( $attachment && $attachment->post_content ) ? $attachment->post_content : __( 'None', 'storeagent-ai-for-woocommerce' ) ); ?></div>
                </div>
            </div>

            <div class="saai-loading" style="display: none;">
                <div class="saai-spinner"></div>
                <p><?php esc_html_e( 'Generating content with AI...', 'storeagent-ai-for-woocommerce' ); ?></p>
            </div>

            <div class="saai-results" style="display: none;">
                <h3><?php esc_html_e( 'Generated Content', 'storeagent-ai-for-woocommerce' ); ?></h3>
                <div class="saai-result-field" data-field="alt">
                    <label><?php esc_html_e( 'Alt Text:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-result-value saai-alt-text"></div>
                </div>
                <div class="saai-result-field" data-field="caption">
                    <label><?php esc_html_e( 'Caption:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-result-value saai-caption"></div>
                </div>
                <div class="saai-result-field" data-field="title">
                    <label><?php esc_html_e( 'Title:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-result-value saai-title"></div>
                </div>
                <div class="saai-result-field" data-field="description">
                    <label><?php esc_html_e( 'Description:', 'storeagent-ai-for-woocommerce' ); ?></label>
                    <div class="saai-result-value saai-description"></div>
                </div>
            </div>

            <div class="saai-error" style="display: none;"></div>
        </div>
        <div class="saai-modal-footer">
            <button type="button" class="button button-secondary" data-action="close">
                <?php esc_html_e( 'Cancel', 'storeagent-ai-for-woocommerce' ); ?>
            </button>
            <button type="button" class="button button-primary saai-generate-button" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>">
                <?php esc_html_e( 'Generate', 'storeagent-ai-for-woocommerce' ); ?>
            </button>
            <button type="button" class="button button-primary saai-save-button" style="display: none;">
                <?php esc_html_e( 'Save Changes', 'storeagent-ai-for-woocommerce' ); ?>
            </button>
        </div>
    </div>
</div>
