<?php
namespace EasyContentManager\Api;

use WP_REST_Controller;
use WP_REST_Server;
use WP_Query;
use WP_REST_Response;
use WP_Error;

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

class PostPriority extends WP_REST_Controller {

	public function __construct() {
		add_action( 'rest_api_init', [ $this, 'register_routes' ] );
	}

	public function register_routes() {

		register_rest_route(
			'easy-content-manager/v1',
			'/posts-priority',
			[
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_posts' ],
					'permission_callback' => [ $this, 'permissions_check' ],
					'args'                => [
						'post_type' => [
							'type'     => 'string',
							'required' => true,
						],
						'page' => [
							'type'    => 'integer',
							'default' => 1,
						],
						'per_page' => [
							'type'    => 'integer',
							'default' => 20,
						],
						'search' => [
							'type'    => 'string',
							'default' => '',
						],
					],
				],

				[
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => [ $this, 'update_priorities' ],
					'permission_callback' => [ $this, 'permissions_check' ],
					'args'                => [
						'priorities' => [
							'type'        => 'object',
							'required'    => true,
							'description' => __( 'Map of post_id => priority.', 'easy-content-manager' ),
						],
					],
				],
			]
		);
	}

	public function permissions_check() {
		return current_user_can( 'edit_posts' );
	}

	public function get_posts( $request ) {
		$post_type = sanitize_key( $request->get_param( 'post_type' ) );
		$page      = max( 1, (int) $request->get_param( 'page' ) );
		$per_page  = min( 100, max( 1, (int) $request->get_param( 'per_page' ) ) );
		$search    = $request->get_param( 'search' );
		
		if ( ! post_type_exists( $post_type ) ) {
			return new WP_Error(
				'invalid_post_type',
				__( 'Invalid post type.', 'easy-content-manager' ),
				[ 'status' => 400 ]
			);
		}
		
		global $wpdb;
		
		// Check if any posts have a menu_order set (greater than 0)
		$has_priority = (bool) $wpdb->get_var( $wpdb->prepare(
			"SELECT ID FROM {$wpdb->posts} 
			WHERE post_type = %s 
			AND post_status = 'publish' 
			AND menu_order > 0 
			LIMIT 1",
			$post_type
		) );
		
		$query_args = [
			'post_type'              => $post_type,
			'post_status'            => 'publish',
			'posts_per_page'         => $per_page,
			'paged'                  => $page,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
			'no_found_rows'          => false,
			's'                      => $search,
		];
		
		if ( $has_priority ) {
			$query_args['orderby'] = [
				'menu_order' => 'DESC',
				'date'       => 'DESC',
			];
			
			add_filter( 'posts_orderby', function( $orderby, $query ) use ( $wpdb ) {
				if ( $query->get( 'ecm_custom_order' ) ) {
					// Order by: menu_order = 0 last, then menu_order ASC, then date DESC
					$orderby = "CASE WHEN {$wpdb->posts}.menu_order = 0 THEN 1 ELSE 0 END ASC, 
								{$wpdb->posts}.menu_order ASC, 
								{$wpdb->posts}.post_date DESC";
				}
				return $orderby;
			}, 10, 2 );
			
			$query_args['ecm_custom_order'] = true;
		} else {
			$query_args['orderby'] = [ 'date' => 'DESC' ];
		}
		
		$query = new WP_Query( $query_args );
		
		$items = [];
		foreach ( $query->posts as $post ) {
			$items[] = [
				'id'         => (int) $post->ID,
				'title'      => get_the_title( $post ),
				'slug'       => $post->post_name,
				'created_at' => $post->post_date,
				'updated_at' => $post->post_modified,
				'priority'   => (int) $post->menu_order,
			];
		}
		
		$response = new WP_REST_Response( $items );
		$response->header( 'X-WP-Total', (int) $query->found_posts );
		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
		
		return $response;
	}

	public function update_priorities( $request ) {

		$priorities = $request->get_param( 'priorities' );

		if ( ! is_array( $priorities ) ) {
			return new WP_Error(
				'invalid_payload',
				__( 'Priorities must be an object of post_id => priority.', 'easy-content-manager' ),
				[ 'status' => 400 ]
			);
		}

		$updated = [];

		foreach ( $priorities as $post_id => $priority ) {

			$post_id  = (int) $post_id;
			$priority = max( 0, (int) $priority );
			$post = get_post( $post_id );

			if ( ! $post ) {
				continue;
			}

			if ( ! current_user_can( 'edit_post', $post_id ) ) {
				continue;
			}

			// Update the menu_order directly
			wp_update_post( [
				'ID'         => $post_id,
				'menu_order' => $priority,
			] );

			$updated[] = [
				'id'       => $post_id,
				'title'    => get_the_title( $post ),
				'slug'     => $post->post_name,
				'priority' => $priority,
			];
		}//end foreach

		return new WP_REST_Response( $updated );
	}
}
