<?php

namespace MovieEngine\PostTypes;

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

class Taxonomies {

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

    public function register_taxonomies() {

        register_taxonomy('movie_engine_genre', ['movie_engine_movie', 'movie_engine_series', 'movie_engine_season', 'movie_engine_episode'], [
            'labels'            => [
                'name'          => esc_html__('Genres', 'cine-movie-engine'),
                'singular_name' => esc_html__('Genre', 'cine-movie-engine'),
                'add_new_item'  => esc_html__('Add New Genre', 'cine-movie-engine'),
            ],
            'hierarchical'      => true,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'genre'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_year', ['movie_engine_movie', 'movie_engine_series', 'movie_engine_season', 'movie_engine_episode'], [
            'labels'            => [
                'name'          => esc_html__('Year', 'cine-movie-engine'),
                'singular_name' => esc_html__('Year', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'year'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_quality', ['movie_engine_movie'], [
            'labels'            => [
                'name'          => esc_html__('Quality', 'cine-movie-engine'),
                'singular_name' => esc_html__('Quality', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'movie-quality'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_tv_quality', ['movie_engine_series', 'movie_engine_season', 'movie_engine_episode'], [
            'labels'            => [
                'name'          => esc_html__('Quality', 'cine-movie-engine'),
                'singular_name' => esc_html__('Quality', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'tv-quality'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_cast', ['movie_engine_movie', 'movie_engine_series', 'movie_engine_season', 'movie_engine_episode'], [
            'labels'            => [
                'name'          => esc_html__('Cast', 'cine-movie-engine'),
                'singular_name' => esc_html__('Actor', 'cine-movie-engine'),
                'add_new_item'  => esc_html__('Add New Actor', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'cast'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_director', 'movie_engine_movie', [
            'labels'            => [
                'name'          => esc_html__('Director', 'cine-movie-engine'),
                'singular_name' => esc_html__('Director', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'director'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_creator', 'movie_engine_series', [
            'labels'            => [
                'name'          => esc_html__('Creator', 'cine-movie-engine'),
                'singular_name' => esc_html__('Creator', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'creator'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_country', ['movie_engine_movie', 'movie_engine_series'], [
            'labels'            => [
                'name'          => esc_html__('Country', 'cine-movie-engine'),
                'singular_name' => esc_html__('Country', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'country'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_language', ['movie_engine_movie', 'movie_engine_series'], [
            'labels'            => [
                'name'          => esc_html__('Language', 'cine-movie-engine'),
                'singular_name' => esc_html__('Language', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => false,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'language'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_studio', ['movie_engine_movie', 'movie_engine_series'], [
            'labels'            => [
                'name'          => esc_html__('Studio', 'cine-movie-engine'),
                'singular_name' => esc_html__('Studio', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => false,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'studio'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_network', 'movie_engine_series', [
            'labels'            => [
                'name'          => esc_html__('Networks', 'cine-movie-engine'),
                'singular_name' => esc_html__('Network', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'network'],
            'show_in_rest'      => true,
        ]);

        register_taxonomy('movie_engine_tag', ['movie_engine_movie', 'movie_engine_series'], [
            'labels'            => [
                'name'          => esc_html__('Tags', 'cine-movie-engine'),
                'singular_name' => esc_html__('Tag', 'cine-movie-engine'),
            ],
            'hierarchical'      => false,
            'show_ui'           => true,
            'show_admin_column' => false,
            'query_var'         => true,
            'rewrite'           => ['slug' => 'tag'],
            'show_in_rest'      => true,
        ]);

        $this->seed_quality_terms();
    }


    private function seed_quality_terms() {
        if (get_option('movie_engine_tv_quality_v1_seeded')) {
            return;
        }
        $qualities = [
            '4K UHD',
            '1080p Full HD',
            '720p HD',
            'HDR',
            'Blu-ray',
            'WEB-DL',
            'DVDRip',
            'CAM',
            'TS',
            'HD-TC'
        ];

        foreach ($qualities as $quality) {
            if (!term_exists($quality, 'movie_engine_quality')) {
                wp_insert_term($quality, 'movie_engine_quality');
            }
            if (!term_exists($quality, 'movie_engine_tv_quality')) {
                wp_insert_term($quality, 'movie_engine_tv_quality');
            }
        }
        update_option('movie_engine_tv_quality_v1_seeded', time());
        $this->migrate_to_quality();
    }

    private function migrate_to_quality() {
        if (get_option('movie_engine_tv_quality_v1_migrated')) {
            return;
        }
        $movies = get_posts(['post_type' => 'movie_engine_movie', 'posts_per_page' => -1, 'fields' => 'ids']);
        foreach ($movies as $id) {
            wp_set_object_terms($id, '1080p Full HD', 'movie_engine_quality');
        }
        $tv_posts = get_posts(['post_type' => ['movie_engine_series', 'movie_engine_episode'], 'posts_per_page' => -1, 'fields' => 'ids']);
        foreach ($tv_posts as $id) {
            wp_set_object_terms($id, '1080p Full HD', 'movie_engine_tv_quality');
        }
        update_option('movie_engine_tv_quality_v1_migrated', time());
    }
}
