<?php

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

trait Niwa_AI_Tools_Pages {

    /* ==================================================================
     *  PAGE MANAGEMENT — Shared helper
     * ================================================================== */

    /**
     * Format page data for tool output.
     *
     * @param WP_Post $post  The page post object.
     * @param bool    $full  Whether to include full content and extra fields.
     * @return array
     */
    private function format_page_data( $post, $full = false ) {
        $thumb_id  = get_post_thumbnail_id( $post->ID );
        $parent_id = intval( $post->post_parent );
        $template  = get_page_template_slug( $post->ID );

        $data = array(
            'id'         => $post->ID,
            'title'      => $post->post_title,
            'status'     => $post->post_status,
            'parent'     => $parent_id > 0 ? $parent_id : null,
            'template'   => '' !== $template ? $template : 'default',
            'date'       => $post->post_date,
            'modified'   => $post->post_modified,
            'slug'       => $post->post_name,
            'menu_order' => intval( $post->menu_order ),
            'link'       => get_permalink( $post->ID ),
        );

        if ( ! $full ) {
            /* Brief mode: excerpt trimmed to 200 chars. */
            $excerpt_source = '' !== $post->post_excerpt ? $post->post_excerpt : $post->post_content;
            $plain          = wp_strip_all_tags( $excerpt_source );
            $data['excerpt'] = mb_strlen( $plain ) > 200 ? mb_substr( $plain, 0, 200 ) . '...' : $plain;
            return $data;
        }

        /* Full mode: include all fields. */
        $data['content']            = $post->post_content;
        $data['excerpt']            = $post->post_excerpt;
        $data['featured_image_id']  = $thumb_id ? intval( $thumb_id ) : null;
        $data['featured_image_url'] = $thumb_id ? wp_get_attachment_url( $thumb_id ) : null;
        $data['comment_status']     = $post->comment_status;

        /* Parent title. */
        if ( $parent_id > 0 ) {
            $parent_post = get_post( $parent_id );
            $data['parent_title'] = $parent_post ? $parent_post->post_title : '';
        } else {
            $data['parent_title'] = '';
        }

        /* Author. */
        $author_id = intval( $post->post_author );
        $data['author'] = array(
            'id'           => $author_id,
            'display_name' => get_the_author_meta( 'display_name', $author_id ),
        );

        return $data;
    }

    /* ==================================================================
     *  PAGE MANAGEMENT — Read-only helpers
     * ================================================================== */

    /**
     * Search / list pages with optional filtering.
     */
    public function execute_search_pages( $args ) {
        $search   = isset( $args['search'] ) ? sanitize_text_field( trim( (string) $args['search'] ) ) : '';
        $status   = isset( $args['status'] ) ? (string) $args['status'] : 'any';
        $per_page = isset( $args['per_page'] ) ? max( 1, min( 100, intval( $args['per_page'] ) ) ) : 10;
        $page     = isset( $args['page'] ) ? max( 1, intval( $args['page'] ) ) : 1;
        $parent   = isset( $args['parent'] ) ? intval( $args['parent'] ) : null;
        $orderby  = isset( $args['orderby'] ) && in_array( (string) $args['orderby'], array( 'date', 'title', 'modified', 'menu_order', 'id' ), true )
            ? (string) $args['orderby']
            : 'date';
        $order = isset( $args['order'] ) && in_array( strtolower( (string) $args['order'] ), array( 'asc', 'desc' ), true )
            ? strtoupper( (string) $args['order'] )
            : 'DESC';

        $valid_statuses = array( 'publish', 'draft', 'pending', 'private', 'trash', 'any' );
        if ( ! in_array( $status, $valid_statuses, true ) ) {
            $status = 'any';
        }

        $query_args = array(
            'post_type'      => 'page',
            'post_status'    => 'any' === $status ? array( 'publish', 'draft', 'pending', 'private' ) : $status,
            'posts_per_page' => $per_page,
            'paged'          => $page,
            'orderby'        => 'id' === $orderby ? 'ID' : $orderby,
            'order'          => $order,
        );

        if ( '' !== $search ) {
            $query_args['s'] = $search;
        }

        if ( null !== $parent && $parent >= 0 ) {
            $query_args['post_parent'] = $parent;
        }

        $posts  = get_posts( $query_args );
        $result = array();

        foreach ( $posts as $post ) {
            $result[] = $this->format_page_data( $post, false );
        }

        return array(
            'ok'    => true,
            'count' => count( $result ),
            'page'  => $page,
            'pages' => $result,
        );
    }

    /**
     * Get full details of a single page.
     */
    public function execute_get_page( $args ) {
        $page_id = isset( $args['page_id'] ) ? intval( $args['page_id'] ) : 0;

        if ( $page_id <= 0 ) {
            return array( 'ok' => false, 'error' => 'page_id je obavezan.' );
        }

        $post = get_post( $page_id );
        if ( ! $post || 'page' !== $post->post_type ) {
            return array( 'ok' => false, 'error' => 'Stranica (ID: ' . $page_id . ') nije pronadjena.' );
        }

        return array(
            'ok'   => true,
            'page' => $this->format_page_data( $post, true ),
        );
    }

    /* ==================================================================
     *  PAGE MANAGEMENT — Preview / Validate / Execute pipelines
     * ================================================================== */

    /**
     * Build a rollback snapshot for an existing page.
     */
    private function build_page_restore_entry( $post ) {
        $thumb_id = get_post_thumbnail_id( $post->ID );
        $template = get_page_template_slug( $post->ID );

        return array(
            'type'                    => 'page_restore',
            'post_id'                 => $post->ID,
            'previous_title'          => $post->post_title,
            'previous_content'        => $post->post_content,
            'previous_excerpt'        => $post->post_excerpt,
            'previous_status'         => $post->post_status,
            'previous_slug'           => $post->post_name,
            'previous_parent'         => intval( $post->post_parent ),
            'previous_template'       => '' !== $template ? $template : '',
            'previous_menu_order'     => intval( $post->menu_order ),
            'previous_comment_status' => $post->comment_status,
            'previous_thumbnail_id'   => $thumb_id ? intval( $thumb_id ) : 0,
        );
    }

    /* ------------------------------------------------------------------
     *  create_page pipeline
     * ------------------------------------------------------------------ */

    private function preview_create_page( $args ) {
        $title = isset( $args['title'] ) ? sanitize_text_field( trim( (string) $args['title'] ) ) : '';
        if ( '' === $title ) {
            return array( 'success' => false, 'error' => 'Naslov stranice (title) je obavezan.' );
        }

        $content = isset( $args['content'] ) ? (string) $args['content'] : '';
        $excerpt = isset( $args['excerpt'] ) ? (string) $args['excerpt'] : '';

        $status = isset( $args['status'] ) && in_array( (string) $args['status'], array( 'draft', 'publish', 'pending', 'private' ), true )
            ? (string) $args['status']
            : 'draft';

        $parent = isset( $args['parent'] ) ? intval( $args['parent'] ) : 0;
        if ( $parent > 0 ) {
            $parent_post = get_post( $parent );
            if ( ! $parent_post || 'page' !== $parent_post->post_type ) {
                return array( 'success' => false, 'error' => 'Roditeljska stranica (ID: ' . $parent . ') nije pronadjena.' );
            }
        }

        $template = isset( $args['template'] ) ? sanitize_text_field( (string) $args['template'] ) : '';

        $menu_order = isset( $args['menu_order'] ) ? intval( $args['menu_order'] ) : 0;

        $featured_image_id = isset( $args['featured_image_id'] ) ? intval( $args['featured_image_id'] ) : 0;
        if ( $featured_image_id > 0 && ! wp_attachment_is_image( $featured_image_id ) ) {
            return array( 'success' => false, 'error' => 'Attachment ID ' . $featured_image_id . ' nije validna slika.' );
        }

        $comment_status = isset( $args['comment_status'] ) && in_array( (string) $args['comment_status'], array( 'open', 'closed' ), true )
            ? (string) $args['comment_status']
            : 'closed';

        $summary_parts   = array();
        $summary_parts[] = 'Kreiranje stranice: "' . $title . '"';
        $summary_parts[] = 'Status: ' . $status;
        if ( $parent > 0 ) {
            $summary_parts[] = 'Roditeljska stranica: ID ' . $parent;
        }
        if ( '' !== $template ) {
            $summary_parts[] = 'Template: ' . $template;
        }
        if ( $featured_image_id > 0 ) {
            $summary_parts[] = 'Featured image: ID ' . $featured_image_id;
        }
        if ( '' !== $content ) {
            $content_words   = str_word_count( wp_strip_all_tags( $content ) );
            $summary_parts[] = 'Sadrzaj: ~' . $content_words . ' reci';
        }

        $action_data = array(
            'type'             => 'create_page',
            'label'            => 'Kreiranje stranice "' . $title . '"',
            'summary'          => implode( ' | ', $summary_parts ),
            'rollback_plan'    => 'Brisanje kreirane stranice.',
            'rollback_support' => 'full',
            'targets'          => array(),
            'args'             => array(
                'title'             => $title,
                'content'           => $content,
                'excerpt'           => $excerpt,
                'status'            => $status,
                'parent'            => $parent,
                'template'          => $template,
                'menu_order'        => $menu_order,
                'featured_image_id' => $featured_image_id,
                'comment_status'    => $comment_status,
            ),
        );

        return array(
            'success' => true,
            'action'  => $action_data,
        );
    }

    private function validate_create_page_action_args( $args ) {
        if ( empty( $args['title'] ) || '' === trim( (string) $args['title'] ) ) {
            return array( 'success' => false, 'error' => 'Naslov stranice (title) nedostaje.' );
        }
        return array( 'success' => true );
    }

    private function execute_create_page_action( $action ) {
        $args = isset( $action['args'] ) && is_array( $action['args'] ) ? $action['args'] : array();

        $post_data = array(
            'post_type'       => 'page',
            'post_title'      => sanitize_text_field( (string) $args['title'] ),
            'post_content'    => wp_kses_post( isset( $args['content'] ) ? (string) $args['content'] : '' ),
            'post_status'     => isset( $args['status'] ) ? (string) $args['status'] : 'draft',
            'comment_status'  => isset( $args['comment_status'] ) ? (string) $args['comment_status'] : 'closed',
        );

        if ( ! empty( $args['excerpt'] ) ) {
            $post_data['post_excerpt'] = wp_kses_post( (string) $args['excerpt'] );
        }

        $parent = isset( $args['parent'] ) ? intval( $args['parent'] ) : 0;
        if ( $parent > 0 ) {
            $post_data['post_parent'] = $parent;
        }

        $menu_order = isset( $args['menu_order'] ) ? intval( $args['menu_order'] ) : 0;
        if ( 0 !== $menu_order ) {
            $post_data['menu_order'] = $menu_order;
        }

        $page_id = wp_insert_post( $post_data, true );
        if ( is_wp_error( $page_id ) ) {
            return array( 'success' => false, 'error' => 'Stranica nije kreirana: ' . $page_id->get_error_message() );
        }

        /* Page template. */
        $template = isset( $args['template'] ) ? sanitize_text_field( (string) $args['template'] ) : '';
        if ( '' !== $template ) {
            update_post_meta( $page_id, '_wp_page_template', $template );
        }

        /* Featured image. */
        $featured_image_id = isset( $args['featured_image_id'] ) ? intval( $args['featured_image_id'] ) : 0;
        if ( $featured_image_id > 0 ) {
            set_post_thumbnail( $page_id, $featured_image_id );
        }

        $status = get_post_status( $page_id );

        return array(
            'success'          => true,
            'summary'          => 'Kreirana stranica "' . get_the_title( $page_id ) . '" (ID: ' . $page_id . ', status: ' . $status . '). Link: ' . get_permalink( $page_id ),
            'executed_actions' => array(
                array(
                    'type'    => 'create_page',
                    'page_id' => $page_id,
                ),
            ),
            'rollback_entries' => array(
                array(
                    'type'    => 'page_delete',
                    'page_id' => $page_id,
                ),
            ),
        );
    }

    /* ------------------------------------------------------------------
     *  update_page pipeline
     * ------------------------------------------------------------------ */

    private function preview_update_page( $args ) {
        $page_id = isset( $args['page_id'] ) ? intval( $args['page_id'] ) : 0;
        if ( $page_id <= 0 ) {
            return array( 'success' => false, 'error' => 'page_id je obavezan za update.' );
        }

        $post = get_post( $page_id );
        if ( ! $post || 'page' !== $post->post_type ) {
            return array( 'success' => false, 'error' => 'Stranica (ID: ' . $page_id . ') nije pronadjena.' );
        }

        /* At least one field must be provided. */
        $updatable_fields = array( 'title', 'content', 'excerpt', 'status', 'parent', 'template', 'menu_order', 'featured_image_id', 'comment_status', 'slug' );
        $has_change = false;
        foreach ( $updatable_fields as $field ) {
            if ( isset( $args[ $field ] ) ) {
                $has_change = true;
                break;
            }
        }
        if ( ! $has_change ) {
            return array( 'success' => false, 'error' => 'Najmanje jedno polje mora biti navedeno za update.' );
        }

        /* Validate parent if provided. */
        if ( isset( $args['parent'] ) ) {
            $new_parent = intval( $args['parent'] );
            if ( $new_parent > 0 ) {
                if ( $new_parent === $page_id ) {
                    return array( 'success' => false, 'error' => 'Stranica ne moze biti svoj sopstveni roditelj.' );
                }
                $parent_post = get_post( $new_parent );
                if ( ! $parent_post || 'page' !== $parent_post->post_type ) {
                    return array( 'success' => false, 'error' => 'Roditeljska stranica (ID: ' . $new_parent . ') nije pronadjena.' );
                }
            }
        }

        /* Validate featured image if provided. */
        if ( isset( $args['featured_image_id'] ) && intval( $args['featured_image_id'] ) > 0 ) {
            if ( ! wp_attachment_is_image( intval( $args['featured_image_id'] ) ) ) {
                return array( 'success' => false, 'error' => 'Attachment ID ' . intval( $args['featured_image_id'] ) . ' nije validna slika.' );
            }
        }

        /* Build rollback snapshot. */
        $rollback_snapshot = $this->build_page_restore_entry( $post );

        /* Describe changes. */
        $summary_parts   = array();
        $summary_parts[] = 'Azuriranje stranice: "' . $post->post_title . '" (ID: ' . $page_id . ')';
        $changes         = array();
        if ( isset( $args['title'] ) )             { $changes[] = 'naslov'; }
        if ( isset( $args['content'] ) )           { $changes[] = 'sadrzaj'; }
        if ( isset( $args['excerpt'] ) )           { $changes[] = 'excerpt'; }
        if ( isset( $args['status'] ) )            { $changes[] = 'status → ' . (string) $args['status']; }
        if ( isset( $args['slug'] ) )              { $changes[] = 'slug'; }
        if ( isset( $args['parent'] ) )            { $changes[] = 'roditeljska stranica'; }
        if ( isset( $args['template'] ) )          { $changes[] = 'template'; }
        if ( isset( $args['menu_order'] ) )        { $changes[] = 'menu order'; }
        if ( isset( $args['featured_image_id'] ) ) { $changes[] = 'featured image'; }
        if ( isset( $args['comment_status'] ) )    { $changes[] = 'komentari'; }
        $summary_parts[] = 'Promene: ' . ( ! empty( $changes ) ? implode( ', ', $changes ) : 'nema' );

        $action_args = array(
            'page_id'           => $page_id,
            'rollback_snapshot' => $rollback_snapshot,
        );

        if ( isset( $args['title'] ) )             { $action_args['title']             = sanitize_text_field( (string) $args['title'] ); }
        if ( isset( $args['content'] ) )           { $action_args['content']           = (string) $args['content']; }
        if ( isset( $args['excerpt'] ) )           { $action_args['excerpt']           = (string) $args['excerpt']; }
        if ( isset( $args['status'] ) )            { $action_args['status']            = (string) $args['status']; }
        if ( isset( $args['slug'] ) )              { $action_args['slug']              = sanitize_title( (string) $args['slug'] ); }
        if ( isset( $args['parent'] ) )            { $action_args['parent']            = intval( $args['parent'] ); }
        if ( isset( $args['template'] ) )          { $action_args['template']          = sanitize_text_field( (string) $args['template'] ); }
        if ( isset( $args['menu_order'] ) )        { $action_args['menu_order']        = intval( $args['menu_order'] ); }
        if ( isset( $args['featured_image_id'] ) ) { $action_args['featured_image_id'] = intval( $args['featured_image_id'] ); }
        if ( isset( $args['comment_status'] ) )    { $action_args['comment_status']    = (string) $args['comment_status']; }

        return array(
            'success' => true,
            'action'  => array(
                'type'             => 'update_page',
                'label'            => 'Azuriranje stranice "' . $post->post_title . '"',
                'summary'          => implode( ' | ', $summary_parts ),
                'rollback_plan'    => 'Vracanje svih polja na prethodne vrednosti.',
                'rollback_support' => 'full',
                'targets'          => array(
                    array( 'type' => 'page', 'id' => $page_id, 'label' => $post->post_title ),
                ),
                'args'             => $action_args,
            ),
        );
    }

    private function validate_update_page_action_args( $args ) {
        if ( empty( $args['page_id'] ) || intval( $args['page_id'] ) <= 0 ) {
            return array( 'success' => false, 'error' => 'page_id nedostaje za update.' );
        }
        if ( isset( $args['status'] ) && ! in_array( (string) $args['status'], array( 'draft', 'publish', 'pending', 'private' ), true ) ) {
            return array( 'success' => false, 'error' => 'Nevalidan status: ' . (string) $args['status'] );
        }
        if ( isset( $args['comment_status'] ) && ! in_array( (string) $args['comment_status'], array( 'open', 'closed' ), true ) ) {
            return array( 'success' => false, 'error' => 'Nevalidan comment_status: ' . (string) $args['comment_status'] );
        }
        return array( 'success' => true );
    }

    private function execute_update_page_action( $action ) {
        $args    = isset( $action['args'] ) && is_array( $action['args'] ) ? $action['args'] : array();
        $page_id = intval( $args['page_id'] );

        $post = get_post( $page_id );
        if ( ! $post || 'page' !== $post->post_type ) {
            return array( 'success' => false, 'error' => 'Stranica (ID: ' . $page_id . ') ne postoji.' );
        }

        $update_data = array( 'ID' => $page_id );
        if ( isset( $args['title'] ) )          { $update_data['post_title']    = sanitize_text_field( (string) $args['title'] ); }
        if ( isset( $args['content'] ) )        { $update_data['post_content']  = wp_kses_post( (string) $args['content'] ); }
        if ( isset( $args['excerpt'] ) )        { $update_data['post_excerpt']  = wp_kses_post( (string) $args['excerpt'] ); }
        if ( isset( $args['status'] ) )         { $update_data['post_status']   = (string) $args['status']; }
        if ( isset( $args['slug'] ) )           { $update_data['post_name']     = sanitize_title( (string) $args['slug'] ); }
        if ( isset( $args['parent'] ) )         { $update_data['post_parent']   = intval( $args['parent'] ); }
        if ( isset( $args['menu_order'] ) )     { $update_data['menu_order']    = intval( $args['menu_order'] ); }
        if ( isset( $args['comment_status'] ) ) { $update_data['comment_status'] = (string) $args['comment_status']; }

        $updated = wp_update_post( $update_data, true );
        if ( is_wp_error( $updated ) ) {
            return array( 'success' => false, 'error' => 'Azuriranje stranice nije uspelo: ' . $updated->get_error_message() );
        }

        /* Page template. */
        if ( isset( $args['template'] ) ) {
            $new_template = sanitize_text_field( (string) $args['template'] );
            if ( '' !== $new_template && 'default' !== $new_template ) {
                update_post_meta( $page_id, '_wp_page_template', $new_template );
            } else {
                delete_post_meta( $page_id, '_wp_page_template' );
            }
        }

        /* Featured image. */
        if ( array_key_exists( 'featured_image_id', $args ) ) {
            $new_thumb = intval( $args['featured_image_id'] );
            if ( $new_thumb > 0 ) {
                set_post_thumbnail( $page_id, $new_thumb );
            } else {
                delete_post_thumbnail( $page_id );
            }
        }

        $rollback_entries = array();
        if ( ! empty( $args['rollback_snapshot'] ) && is_array( $args['rollback_snapshot'] ) ) {
            $rollback_entries[] = $args['rollback_snapshot'];
        }

        return array(
            'success'          => true,
            'summary'          => 'Azurirana stranica "' . get_the_title( $page_id ) . '" (ID: ' . $page_id . '). Link: ' . get_permalink( $page_id ),
            'executed_actions' => array(
                array(
                    'type'    => 'update_page',
                    'page_id' => $page_id,
                ),
            ),
            'rollback_entries' => $rollback_entries,
        );
    }

    /* ------------------------------------------------------------------
     *  delete_page pipeline
     * ------------------------------------------------------------------ */

    private function preview_delete_page( $args ) {
        $page_id = isset( $args['page_id'] ) ? intval( $args['page_id'] ) : 0;
        if ( $page_id <= 0 ) {
            return array( 'success' => false, 'error' => 'page_id je obavezan za brisanje.' );
        }

        $post = get_post( $page_id );
        if ( ! $post || 'page' !== $post->post_type ) {
            return array( 'success' => false, 'error' => 'Stranica (ID: ' . $page_id . ') nije pronadjena.' );
        }

        /* Safety: cannot delete the front page. */
        $front_page_id = intval( get_option( 'page_on_front', 0 ) );
        if ( $front_page_id > 0 && $front_page_id === $page_id ) {
            return array( 'success' => false, 'error' => 'Stranica (ID: ' . $page_id . ') je front page sajta i ne moze se obrisati.' );
        }

        $force = ! empty( $args['force'] );

        if ( $force ) {
            return array(
                'success' => true,
                'action'  => array(
                    'type'             => 'delete_page',
                    'label'            => 'Trajno brisanje stranice "' . $post->post_title . '"',
                    'summary'          => 'Trajno brisanje stranice: "' . $post->post_title . '" (ID: ' . $page_id . '). Ova akcija je NEPOVRATNA.',
                    'rollback_plan'    => 'Nema mogucnosti rollback-a za trajno brisanje.',
                    'rollback_support' => 'none',
                    'targets'          => array(
                        array( 'type' => 'page', 'id' => $page_id, 'label' => $post->post_title ),
                    ),
                    'args'             => array(
                        'page_id' => $page_id,
                        'force'   => true,
                    ),
                ),
            );
        }

        return array(
            'success' => true,
            'action'  => array(
                'type'             => 'delete_page',
                'label'            => 'Premestanje stranice "' . $post->post_title . '" u korpu',
                'summary'          => 'Premestanje stranice u korpu: "' . $post->post_title . '" (ID: ' . $page_id . ', status: ' . $post->post_status . ' → trash)',
                'rollback_plan'    => 'Vracanje stranice na prethodni status.',
                'rollback_support' => 'full',
                'targets'          => array(
                    array( 'type' => 'page', 'id' => $page_id, 'label' => $post->post_title ),
                ),
                'args'             => array(
                    'page_id'         => $page_id,
                    'force'           => false,
                    'previous_status' => $post->post_status,
                ),
            ),
        );
    }

    private function validate_delete_page_action_args( $args ) {
        if ( empty( $args['page_id'] ) || intval( $args['page_id'] ) <= 0 ) {
            return array( 'success' => false, 'error' => 'page_id nedostaje za brisanje.' );
        }

        /* Re-check front page safety. */
        $page_id       = intval( $args['page_id'] );
        $front_page_id = intval( get_option( 'page_on_front', 0 ) );
        if ( $front_page_id > 0 && $front_page_id === $page_id ) {
            return array( 'success' => false, 'error' => 'Front page sajta ne moze biti obrisana.' );
        }

        return array( 'success' => true );
    }

    private function execute_delete_page_action( $action ) {
        $args    = isset( $action['args'] ) && is_array( $action['args'] ) ? $action['args'] : array();
        $page_id = intval( $args['page_id'] );
        $force   = ! empty( $args['force'] );

        $post = get_post( $page_id );
        if ( ! $post || 'page' !== $post->post_type ) {
            return array( 'success' => false, 'error' => 'Stranica (ID: ' . $page_id . ') ne postoji.' );
        }

        /* Safety: re-check front page. */
        $front_page_id = intval( get_option( 'page_on_front', 0 ) );
        if ( $front_page_id > 0 && $front_page_id === $page_id ) {
            return array( 'success' => false, 'error' => 'Front page sajta ne moze biti obrisana.' );
        }

        if ( $force ) {
            $deleted = wp_delete_post( $page_id, true );
            if ( false === $deleted || null === $deleted ) {
                return array( 'success' => false, 'error' => 'Trajno brisanje stranice (ID: ' . $page_id . ') nije uspelo.' );
            }

            return array(
                'success'          => true,
                'summary'          => 'Trajno obrisana stranica "' . $post->post_title . '" (ID: ' . $page_id . ').',
                'executed_actions' => array(
                    array(
                        'type'    => 'delete_page',
                        'page_id' => $page_id,
                        'force'   => true,
                    ),
                ),
                'rollback_entries' => array(),
            );
        }

        /* Soft delete: move to trash. */
        $previous_status = isset( $args['previous_status'] ) ? (string) $args['previous_status'] : $post->post_status;
        $trashed         = wp_trash_post( $page_id );

        if ( false === $trashed ) {
            return array( 'success' => false, 'error' => 'Premestanje stranice (ID: ' . $page_id . ') u korpu nije uspelo.' );
        }

        return array(
            'success'          => true,
            'summary'          => 'Stranica "' . $post->post_title . '" (ID: ' . $page_id . ') premestena u korpu.',
            'executed_actions' => array(
                array(
                    'type'    => 'delete_page',
                    'page_id' => $page_id,
                    'force'   => false,
                ),
            ),
            'rollback_entries' => array(
                array(
                    'type'            => 'post_status_restore',
                    'post_id'         => $page_id,
                    'previous_status' => $previous_status,
                ),
            ),
        );
    }
}
