<?php

namespace MovieEngine\PostTypes;

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

class Person {

    public function __construct() {
        add_action('init', [$this, 'register']);
        add_filter('manage_movie_engine_person_posts_columns', [$this, 'set_columns']);
        add_action('manage_movie_engine_person_posts_custom_column', [$this, 'render_columns'], 10, 2);
    }

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

        $args = [
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => ['slug' => 'person'],
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => 6,
            'menu_icon'          => 'dashicons-admin-users',
            'supports'           => ['title', 'editor', 'thumbnail', 'excerpt'],
            'show_in_rest'       => true,
        ];

        register_post_type('movie_engine_person', $args);
    }

    public function set_columns($columns) {
        $new_columns = [];
        $new_columns['cb'] = $columns['cb'];
        $new_columns['thumbnail'] = esc_html__('Photo', 'cine-movie-engine');
        $new_columns['title'] = $columns['title'];
        $new_columns['tmdb_id'] = esc_html__('TMDb ID', 'cine-movie-engine');
        $new_columns['date'] = $columns['date'];
        return $new_columns;
    }

    public function render_columns($column, $post_id) {
        switch ($column) {
            case 'thumbnail':
                if (has_post_thumbnail($post_id)) {
                    the_post_thumbnail([50, 50], ['style' => 'border-radius: 6px;']);
                } else {
                    echo '<span class="dashicons dashicons-admin-users" style="font-size: 40px; width: 40px; height: 40px; color: #cbd5e1;"></span>';
                }
                break;
            case 'tmdb_id':
                $tmdb_id = get_post_meta($post_id, '_movie_engine_tmdb_id', true);
                echo $tmdb_id ? esc_html($tmdb_id) : '-';
                break;
        }
    }
}
