<?php

namespace MovieEngine\PostTypes;

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

class Movie {

    public function __construct() {
        add_action('init', [$this, 'register']);
    }

    public function register() {
        $labels = [
            'name'               => esc_html_x('Movies', 'post type general name', 'cine-movie-engine'),
            'singular_name'      => esc_html_x('Movie', 'post type singular name', 'cine-movie-engine'),
            'menu_name'          => esc_html_x('Movies', 'admin menu', 'cine-movie-engine'),
            'name_admin_bar'     => esc_html_x('Movie', 'add new on admin bar', 'cine-movie-engine'),
            'add_new'            => esc_html_x('Add New', 'movie', 'cine-movie-engine'),
            'add_new_item'       => esc_html__('Add New Movie', 'cine-movie-engine'),
            'new_item'           => esc_html__('New Movie', 'cine-movie-engine'),
            'edit_item'          => esc_html__('Edit Movie', 'cine-movie-engine'),
            'view_item'          => esc_html__('View Movie', 'cine-movie-engine'),
            'all_items'          => esc_html__('All Movies', 'cine-movie-engine'),
            'search_items'       => esc_html__('Search Movies', 'cine-movie-engine'),
            'parent_item_colon'  => esc_html__('Parent Movies:', 'cine-movie-engine'),
            'not_found'          => esc_html__('No movies found.', 'cine-movie-engine'),
            'not_found_in_trash' => esc_html__('No movies found in Trash.', 'cine-movie-engine'),
        ];

        $design_settings = get_option('movie_engine_design_settings', []);
        $archive_page_id = isset($design_settings['movie_archive_page']) ? intval($design_settings['movie_archive_page']) : 0;
        $has_custom_page = $archive_page_id && get_post_status($archive_page_id) === 'publish';

        $has_archive = !$has_custom_page;

        $current_state = $has_archive ? 'archive' : 'page';
        $last_state = get_option('movie_engine_movie_archive_state', '');

        if ($current_state !== $last_state) {
            update_option('movie_engine_movie_archive_state', $current_state);
            add_action('shutdown', 'flush_rewrite_rules');
        }

        $args = [
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => ['slug' => 'movies'],
            'capability_type'    => 'post',
            'has_archive'        => $has_archive,
            'hierarchical'       => false,
            'menu_position'      => 5,
            'menu_icon'          => 'dashicons-video-alt2',
            'supports'           => ['title', 'editor', 'thumbnail', 'excerpt', 'comments'],
            'show_in_rest'       => true,
        ];

        register_post_type('movie_engine_movie', $args);
    }
}
