<?php if ( is_singular( AITD_POST_TYPE ) ) : get_header(); endif; ?>
<?php
$tool = isset( $tool ) && $tool instanceof WP_Post ? $tool : aitd_get_current_tool();

if ( ! $tool ) {
    if ( is_singular( AITD_POST_TYPE ) ) {
        get_footer();
    }
    return;
}

$website_url = aitd_get_tool_meta( $tool->ID, '_aitd_website_url' );
$logo_url    = aitd_get_tool_logo_source( $tool->ID );
$pricing     = aitd_get_tool_meta( $tool->ID, '_aitd_pricing', 'Free' );
$featured    = (int) aitd_get_tool_meta( $tool->ID, '_aitd_featured', 0 );
$categories  = get_the_terms( $tool, 'category' );
$tags        = get_the_terms( $tool, 'post_tag' );
$website_domain = aitd_get_tool_domain( $website_url );
$primary_category = ( $categories && ! is_wp_error( $categories ) ) ? $categories[0]->name : '';
$use_cases = $tags && ! is_wp_error( $tags ) ? array_slice( wp_list_pluck( $tags, 'name' ), 0, 4 ) : [];
?>
<div class="aitd-single-wrap" data-tool-name="<?php echo esc_attr( get_the_title( $tool ) ); ?>">
    <div class="aitd-single-nav">
        <a href="<?php echo esc_url( aitd_get_directory_url() ); ?>" class="aitd-back-btn">Back to Directory</a>
    </div>

    <div class="aitd-single-hero">
        <div class="aitd-single-logo">
            <?php if ( $logo_url ) : ?>
                <img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr( $tool->post_title ); ?>" crossorigin="anonymous">
            <?php else : ?>
                <span><?php echo esc_html( strtoupper( substr( $tool->post_title, 0, 2 ) ) ); ?></span>
            <?php endif; ?>
        </div>

        <div class="aitd-single-info">
            <div class="aitd-single-meta">
                <?php if ( $categories && ! is_wp_error( $categories ) ) : ?>
                    <span class="aitd-category-pill"><?php echo esc_html( $categories[0]->name ); ?></span>
                <?php endif; ?>
                <span class="aitd-pricing-badge <?php echo esc_attr( sanitize_html_class( strtolower( str_replace( ' ', '-', $pricing ) ) ) ); ?>"><?php echo esc_html( $pricing ); ?></span>
                <?php if ( $featured ) : ?>
                    <span class="aitd-featured-label">Featured</span>
                <?php endif; ?>
            </div>

            <h1 class="aitd-single-title"><?php echo esc_html( get_the_title( $tool ) ); ?></h1>
            <p class="aitd-single-short-desc"><?php echo esc_html( get_the_excerpt( $tool ) ); ?></p>

            <div class="aitd-single-actions">
                <a href="<?php echo esc_url( $website_url ?: get_permalink( $tool ) ); ?>" target="_blank" rel="noopener sponsored" class="aitd-cta-btn">Visit <?php echo esc_html( get_the_title( $tool ) ); ?></a>
            </div>
        </div>
    </div>

    <div class="aitd-single-facts">
        <div class="aitd-fact-card">
            <span class="aitd-fact-label">Best for</span>
            <strong><?php echo esc_html( $primary_category ?: 'General AI workflows' ); ?></strong>
        </div>
        <div class="aitd-fact-card">
            <span class="aitd-fact-label">Pricing</span>
            <strong><?php echo esc_html( $pricing ); ?></strong>
        </div>
        <div class="aitd-fact-card">
            <span class="aitd-fact-label">Website</span>
            <strong><?php echo esc_html( $website_domain ?: 'Official tool website' ); ?></strong>
        </div>
    </div>

    <?php if ( $tool->post_content ) : ?>
        <div class="aitd-single-body-content">
            <div class="aitd-single-desc-card">
                <h2>About <?php echo esc_html( get_the_title( $tool ) ); ?></h2>
                <div class="aitd-single-desc"><?php echo wpautop( wp_kses_post( $tool->post_content ) ); ?></div>
            </div>
        </div>
    <?php endif; ?>

    <?php if ( ! empty( $use_cases ) ) : ?>
        <div class="aitd-single-use-cases">
            <h2>Popular Use Cases</h2>
            <div class="aitd-card-highlights">
                <?php foreach ( $use_cases as $use_case ) : ?>
                    <span class="aitd-highlight-pill"><?php echo esc_html( $use_case ); ?></span>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <?php if ( $tags && ! is_wp_error( $tags ) ) : ?>
        <div class="aitd-single-tags-wrap">
            <h3>Tags</h3>
            <div class="aitd-tags">
                <?php foreach ( $tags as $tag ) : ?>
                    <span class="aitd-tag"><?php echo esc_html( $tag->name ); ?></span>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <?php
    $related_query = new WP_Query(
        [
            'post_type'      => AITD_POST_TYPE,
            'post_status'    => 'publish',
            'posts_per_page' => 3,
            'post__not_in'   => [ $tool->ID ],
            'tax_query'      => ! empty( $categories ) && ! is_wp_error( $categories ) ? [
                [
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => wp_list_pluck( $categories, 'term_id' ),
                ],
            ] : [],
        ]
    );

    if ( $related_query->have_posts() ) :
    ?>
        <div class="aitd-related">
            <h2>More AI Tools</h2>
            <div class="aitd-grid aitd-related-grid">
                <?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
                    <?php aitd_render_card( get_post() ); ?>
                <?php endwhile; wp_reset_postdata(); ?>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php if ( is_singular( AITD_POST_TYPE ) ) : get_footer(); endif; ?>
