<?php

/**
 * Page Setup Class
 *
 * Handles automatic creation of required pages on plugin activation.
 *
 * @package MovieEngine
 */

namespace MovieEngine\Core;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class PageSetup {

    public static function get_pages() {
        return [
            'dashboard_page' => [
                'name'    => esc_html_x('movie-engine-dashboard', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Dashboard', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_dashboard]',
                'template' => 'page-dashboard.php',
            ],
            'movie_archive_page' => [
                'name'    => esc_html_x('movies', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Movies Archive', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_library post_type="movie_engine_movie" show_search="true" show_filters="true" filter_layout="sidebar" columns="5" limit="20"]',
                'template' => 'template-container-fluid.php',
            ],
            'series_archive_page' => [
                'name'    => esc_html_x('tv-series', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Series Archive', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_library post_type="movie_engine_series" show_search="true" show_filters="true" filter_layout="sidebar" columns="4" limit="12"]',
                'template' => 'template-container-fluid.php',
            ],
            'browse_page' => [
                'name'    => esc_html_x('movie-engine-movies', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Browse Movies', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_library post_type="movie_engine_movie" show_search="true" show_filters="false" columns="5" limit="20"]',
                'template' => 'template-container-fluid.php',
            ],
            'tv_series_page' => [
                'name'    => esc_html_x('movie-engine-tv-series', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('TV Series', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_library post_type="movie_engine_series" show_search="true" show_filters="false" columns="4" limit="12"]',
                'template' => 'template-container-fluid.php',
            ],
            'search_page' => [
                'name'    => esc_html_x('movie-engine-search', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Search Results', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_search]',
                'template' => 'template-container-fluid.php',
            ],
            'login_page' => [
                'name'    => esc_html_x('movie-engine-login', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Sign In', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_login]',
                'template' => 'template-container-fluid.php',
            ],
            'register_page' => [
                'name'    => esc_html_x('movie-engine-register', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Register', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_login action="register"]',
                'template' => 'template-container-fluid.php',
            ],
            'person_archive_page' => [
                'name'    => esc_html_x('persons', 'Page slug', 'cine-movie-engine'),
                'title'   => esc_html_x('Person Archive', 'Page title', 'cine-movie-engine'),
                'content' => '[movie_engine_person_list show_filters="true" columns="5" limit="20"]',
                'template' => 'template-container-fluid.php',
            ],
        ];
    }

    /**
     * Create required pages.
     */
    public static function create_pages() {
        $pages = self::get_pages();
        $design_settings = get_option('movie_engine_design_settings', []);

        foreach ($pages as $key => $page) {
            $page_id = isset($design_settings[$key]) ? absint($design_settings[$key]) : 0;
            $page_object = $page_id ? get_post($page_id) : null;

            if (!$page_object || $page_object->post_status === 'trash') {
                $existing_page = get_page_by_path($page['name']);

                if ($existing_page) {
                    $design_settings[$key] = $existing_page->ID;
                } else {
                    $new_page_id = wp_insert_post([
                        'post_title'     => $page['title'],
                        'post_name'      => $page['name'],
                        'post_content'   => $page['content'],
                        'post_status'    => 'publish',
                        'post_type'      => 'page',
                        'comment_status' => 'closed',
                    ]);

                    if ($new_page_id && !is_wp_error($new_page_id)) {
                        $design_settings[$key] = $new_page_id;
                        if (!empty($page['template'])) {
                            update_post_meta($new_page_id, '_wp_page_template', $page['template']);
                        }
                    }
                }
            }
        }
        update_option('movie_engine_design_settings', $design_settings);
    }
}
