<?php
namespace TSProduct;

use TSProduct\Helper;
use TSProduct\Common;

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

class ProductShowcase {

	public static function init() {
		$self = new self();
		// Team Showcase Ajax
		add_action( 'wp_ajax_tsproduct/product_showcase/fetch', array( $self, 'get_showcase' ) );

		add_action( 'wp_ajax_tsproduct/product_showcase/fetch/single', array( $self, 'get_showcase_by_id' ) );
		add_action( 'wp_ajax_nopriv_tsproduct/product_showcase/fetch/single', array( $self, 'get_showcase_by_id' ) );
		
		add_action( 'wp_ajax_tsproduct/product_showcase/create', array( $self, 'create_showcase' ) );
		add_action( 'wp_ajax_tsproduct/product_showcase/update', array( $self, 'update_showcase' ) );
		add_action( 'wp_ajax_tsproduct/product_showcase/update/settings', array( $self, 'update_showcase_settings' ) );
		add_action( 'wp_ajax_tsproduct/product_showcase/duplicate', array( $self, 'duplicate_showcase' ) );
		add_action( 'wp_ajax_tsproduct/product_showcase/delete', array( $self, 'delete_showcase' ) );
	}

	// Team Showcase Ajax

	public function get_showcase() {
		check_ajax_referer( 'tsproduct_nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$args = array(
			'post_type' => 'ts-product-showcase',
			'posts_per_page' => -1,
            'orderby' => 'date',
            'order' => 'DESC'
		);

		$query = new \WP_Query( $args );

		if ( ! $query->have_posts() ) {
			wp_send_json_error( array( 'message' => 'No showcases found' ) );
		}

		$showcases = array();

		while ( $query->have_posts() ) {
			$query->the_post();

			$showcases[] = array(
				'post_id'   => get_the_ID(),
				'title'     => get_the_title(),
				'shortcode' => '[ts_product_showcase id="' . get_the_ID() . '"]',
				'snippet'  => '<?php echo do_shortcode(\'[ts_product_showcase id="' . get_the_ID() . '"]\'); ?>',
			);
		}

		wp_reset_postdata();

		wp_send_json_success( $showcases );
	}

	public function get_showcase_by_id() {
		check_ajax_referer( 'tsproduct_nonce' );

		$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;

		$args = array(
			'post_type' => 'ts-product-showcase',
			'p'         => $post_id,
		);

		$query = new \WP_Query( $args );

		if ( ! $query->have_posts() ) {
			wp_send_json_error( array( 'message' => 'Showcase not found' ) );
			return;
		}

		$query->the_post();
		$post_id = get_the_ID();

		// Get the team member IDs stored as meta data
        $product_ids   = get_post_meta( $post_id, 'tsproduct_products', true );
		$showcase_settings = get_post_meta( $post_id, 'tsproduct_showcase_settings', true );

		if ( ! empty( $product_ids ) && is_array( $product_ids ) ) {
			$products_result = Helper::get_products_by_ids( $product_ids );

			if ( $products_result['error'] ) {
				wp_send_json_error( array( 'message' => $products_result['message'] ) );
				return;
			}

			$products = $products_result['products'];
		} else {
			$products = array();
		}

		$showcase = array(
			'post_id'   => $post_id,
			'title'     => get_the_title( $post_id ),
			'content'   => get_the_content(),
			'meta_data' => array(
				'products'      => $products,
				'showcase_settings' => ! empty( $showcase_settings ) ? $showcase_settings : Common::get_default_showcase_settings(),
			),
		);

		wp_reset_postdata();
		wp_send_json_success( $showcase );
	}

	public function create_showcase() {
		check_ajax_referer( 'tsproduct_nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$showcase_title    = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
		$products      = isset( $_POST['products'] ) ? array_map( 'intval', (array) $_POST['products'] ) : array();
		$showcase_settings = isset($_POST['data']) ? array_map(function($item) { return is_array($item) ? $item : sanitize_text_field($item); }, wp_unslash($_POST['data'])) : array();

		$args = array(
			'post_title'  => $showcase_title,
			'post_status' => 'publish',
			'post_author' => get_current_user_id(),
			'post_type'   => 'ts-product-showcase',
		);

		$is_post = wp_insert_post( $args );

		if ( is_wp_error( $is_post ) ) {
			wp_send_json_error( array( 'message' => 'Failed to create showcase' ) );
			return;
		}

		update_post_meta( $is_post, 'tsproduct_products', $products );
		update_post_meta( $is_post, 'tsproduct_showcase_settings', $showcase_settings );
		wp_send_json_success( array( 'post_id' => $is_post ) );
	}

	public function update_showcase() {
		check_ajax_referer( 'tsproduct_nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$post_id = isset( $_POST['data']['post_id'] ) ? absint( $_POST['data']['post_id'] ) : 0;

		if ( ! $post_id ) {
			wp_send_json_error( array( 'message' => 'Invalid ID' ) );
		}

		$showcase_title = ( isset( $_POST['data']['title'] ) ? sanitize_text_field( wp_unslash( $_POST['data']['title'] ) ) : '' );
		$products   = isset( $_POST['data']['products'] ) ? array_map( 'intval', (array) $_POST['data']['products'] ) : array();

		$args = array(
		    'ID'         => $post_id,
        	'post_title'  => $showcase_title,
        	'post_type'   => 'ts-product-showcase',
        	'meta_input' => array(
            	'tsproduct_products' => $products,
            ),
        );

		$is_post = wp_update_post( $args );
		wp_send_json_success( array( 'post_id' => $is_post ) );
	}

	public function update_showcase_settings() {
		check_ajax_referer( 'tsproduct_nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$post_id           = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
		$showcase_settings = isset($_POST['data']) ? array_map(function($item) { return is_array($item) ? $item : sanitize_text_field($item); }, wp_unslash($_POST['data'])) : array();

		$args = array(
			'ID'        => $post_id,
        	'post_type'   => 'ts-product-showcase',
		);

		$is_post = wp_update_post( $args, true );

		// Check for errors in the update
		if ( is_wp_error( $is_post ) ) {
			wp_send_json_error( array( 'message' => 'Failed to update showcase' ) );
			return;
		}

		update_post_meta( $post_id, 'tsproduct_showcase_settings', $showcase_settings );
		wp_send_json_success( array( 'post_id' => $post_id ) );
	}

    public function duplicate_showcase() {
        check_ajax_referer( 'tsproduct_nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die();
        }

        try {
            $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;

            if ( ! $post_id ) {
                wp_send_json_error( array( 'message' => 'Invalid ID' ) );
                return;
            }

            // Get original post data
            $post = get_post( $post_id );

            if ( ! $post || $post->post_type !== 'ts-product-showcase' ) {
                wp_send_json_error( array( 'message' => 'Showcase not found' ) );
                return;
            }

            // Get the team member IDs and showcase settings stored as meta data
            $product_ids = get_post_meta( $post_id, 'tsproduct_products', true );
            $showcase_settings = get_post_meta( $post_id, 'tsproduct_showcase_settings', true );

            // Create new post args
            $args = array(
                'post_title'   => $post->post_title . ' (Copy)',
                'post_content' => $post->post_content,
                'post_status'  => 'publish',
                'post_author'  => get_current_user_id(),
                'post_type'    => 'ts-product-showcase',
            );

            // Insert the new post
            $new_post_id = wp_insert_post( $args );

            if ( is_wp_error( $new_post_id ) ) {
                wp_send_json_error( array( 'message' => 'Failed to duplicate showcase: ' . $new_post_id->get_error_message() ) );
                return;
            }

            // Copy over the meta data
            update_post_meta( $new_post_id, 'tsproduct_products', $product_ids );
            update_post_meta( $new_post_id, 'tsproduct_showcase_settings', $showcase_settings );

            wp_send_json_success(
                array(
                    'message' => 'Showcase duplicated successfully',
                    'post_id' => $new_post_id
                )
            );
        } catch (Exception $e) {
            wp_send_json_error( array( 'message' => 'Exception occurred: ' . $e->getMessage() ) );
        }
    }

	public function delete_showcase() {
		check_ajax_referer( 'tsproduct_nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;

		if ( ! $post_id ) {
			wp_send_json_error( array( 'message' => 'Invalid showcase ID' ) );
		}

		$deleted = wp_delete_post( $post_id, true );

		if ( $deleted ) {
			wp_send_json_success(
				array(
					'message' => 'Showcase deleted successfully',
					'post_id' => $post_id,
				)
			);
		} else {
			wp_send_json_error( array( 'message' => 'Failed to delete showcase' ) );
		}
	}
}
