<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Trendly_AI_Post_Duplicate {
    public function title_exists( string $title ) : bool {
        $q = new WP_Query( [
            'post_type' => 'post',
            'post_status' => [ 'publish','draft','pending','future' ],
            'title' => $title,
            'posts_per_page' => 1,
            'no_found_rows' => true,
            'fields' => 'ids',
            'cache_results' => true,
        ] );
        return ( $q->have_posts() );
    }

    public function duplicate_post( int $post_id ) : int {
        $post = get_post( $post_id );
        if ( ! $post ) return 0;
        $new_post = [
            'post_title'   => $post->post_title . ' (Copy)',
            'post_content' => $post->post_content,
            'post_status'  => 'draft',
            'post_author'  => $post->post_author,
            'post_category'=> wp_get_post_categories( $post_id ),
        ];
        return intval( wp_insert_post( wp_slash( $new_post ) ) );
    }
}
