<?php
/**
 * Odysseo Core — Custom Post Types & Taxonomies.
 *
 * Registers Destinations (+ States taxonomy), Fleet, Tour Packages,
 * Gallery (+ Gallery Categories) and Testimonials. Lives in the plugin so the
 * content survives a theme switch (wordpress.org plugin-territory guideline).
 *
 * @package    OdysseoCore
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

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

/**
 * Register `destination` CPT (cities and places).
 */
function odysseo_register_destination_cpt() {
    $labels = array(
        'name'               => __( 'Destinations', 'odysseo-core' ),
        'singular_name'      => __( 'Destination', 'odysseo-core' ),
        'menu_name'          => __( 'Destinations', 'odysseo-core' ),
        'add_new'            => __( 'Add New Destination', 'odysseo-core' ),
        'add_new_item'       => __( 'Add New Destination', 'odysseo-core' ),
        'edit_item'          => __( 'Edit Destination', 'odysseo-core' ),
        'new_item'           => __( 'New Destination', 'odysseo-core' ),
        'view_item'          => __( 'View Destination', 'odysseo-core' ),
        'search_items'       => __( 'Search Destinations', 'odysseo-core' ),
        'all_items'          => __( 'All Destinations', 'odysseo-core' ),
    );

    register_post_type( 'destination', array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-location-alt',
        'menu_position'      => 4,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'       => true,
        'rewrite'            => array( 'slug' => 'destinations' ),
    ) );
}
add_action( 'init', 'odysseo_register_destination_cpt' );

/**
 * Register `dest_state` taxonomy (regions / states).
 */
function odysseo_register_state_taxonomy() {
    register_taxonomy( 'dest_state', 'destination', array(
        'labels' => array(
            'name'              => __( 'States', 'odysseo-core' ),
            'singular_name'     => __( 'State', 'odysseo-core' ),
            'menu_name'         => __( 'States', 'odysseo-core' ),
            'all_items'         => __( 'All States', 'odysseo-core' ),
            'edit_item'         => __( 'Edit State', 'odysseo-core' ),
            'add_new_item'      => __( 'Add New State', 'odysseo-core' ),
        ),
        'hierarchical'      => true,
        'show_admin_column' => true,
        'show_in_rest'      => true,
        'rewrite'           => array( 'slug' => 'destinations/state', 'with_front' => false ),
    ) );
}
add_action( 'init', 'odysseo_register_state_taxonomy' );

/**
 * Flush rewrite rules once after the state taxonomy slug change.
 * Bump the option flag (v2) any time the taxonomy slug changes again.
 */
function odysseo_flush_after_taxonomy_change() {
    if ( get_option( 'odysseo_state_slug_flushed_v2' ) ) return;
    flush_rewrite_rules( false );
    update_option( 'odysseo_state_slug_flushed_v2', 1 );
}
add_action( 'init', 'odysseo_flush_after_taxonomy_change', 100 );

/**
 * Custom Post Type: Fleets
 */
function odysseo_register_fleet_cpt() {
    $labels = array(
        'name'               => __( 'Fleets', 'odysseo-core' ),
        'singular_name'      => __( 'Fleet', 'odysseo-core' ),
        'menu_name'          => __( 'Fleets', 'odysseo-core' ),
        'add_new'            => __( 'Add New Fleet', 'odysseo-core' ),
        'add_new_item'       => __( 'Add New Fleet', 'odysseo-core' ),
        'edit_item'          => __( 'Edit Fleet', 'odysseo-core' ),
        'new_item'           => __( 'New Fleet', 'odysseo-core' ),
        'view_item'          => __( 'View Fleet', 'odysseo-core' ),
        'search_items'       => __( 'Search Fleets', 'odysseo-core' ),
        'not_found'          => __( 'No fleets found', 'odysseo-core' ),
        'not_found_in_trash' => __( 'No fleets found in Trash', 'odysseo-core' ),
    );

    register_post_type( 'fleet', array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-car',
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'       => true,
        'rewrite'            => array( 'slug' => 'fleets' ),
    ) );
}
add_action( 'init', 'odysseo_register_fleet_cpt' );

/**
 * Custom Post Type: Tours / Packages
 */
function odysseo_register_tour_cpt() {
    $labels = array(
        'name'               => __( 'Tour Packages', 'odysseo-core' ),
        'singular_name'      => __( 'Tour Package', 'odysseo-core' ),
        'menu_name'          => __( 'Tour Packages', 'odysseo-core' ),
        'add_new'            => __( 'Add New Tour', 'odysseo-core' ),
        'add_new_item'       => __( 'Add New Tour', 'odysseo-core' ),
        'edit_item'          => __( 'Edit Tour', 'odysseo-core' ),
        'new_item'           => __( 'New Tour', 'odysseo-core' ),
    );

    register_post_type( 'tour_package', array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-palmtree',
        'menu_position'      => 6,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'       => true,
        'rewrite'            => array( 'slug' => 'tour-packages' ),
    ) );
}
add_action( 'init', 'odysseo_register_tour_cpt' );

/**
 * Custom Post Type: Gallery
 */
function odysseo_register_gallery_cpt() {
    $labels = array(
        'name'               => __( 'Gallery', 'odysseo-core' ),
        'singular_name'      => __( 'Gallery Item', 'odysseo-core' ),
        'menu_name'          => __( 'Gallery', 'odysseo-core' ),
        'add_new'            => __( 'Add New Image', 'odysseo-core' ),
        'add_new_item'       => __( 'Add New Gallery Item', 'odysseo-core' ),
    );

    register_post_type( 'gallery_item', array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => false,
        'menu_icon'          => 'dashicons-format-gallery',
        'menu_position'      => 7,
        'supports'           => array( 'title', 'thumbnail' ),
        'show_in_rest'       => true,
    ) );

    register_taxonomy( 'gallery_category', 'gallery_item', array(
        'labels' => array(
            'name'          => __( 'Gallery Categories', 'odysseo-core' ),
            'singular_name' => __( 'Gallery Category', 'odysseo-core' ),
        ),
        'hierarchical' => true,
        'show_in_rest' => true,
        'rewrite'      => array( 'slug' => 'gallery-category' ),
    ) );
}
add_action( 'init', 'odysseo_register_gallery_cpt' );

/**
 * Custom Post Type: Testimonials
 */
function odysseo_register_testimonial_cpt() {
    $labels = array(
        'name'          => __( 'Testimonials', 'odysseo-core' ),
        'singular_name' => __( 'Testimonial', 'odysseo-core' ),
        'menu_name'     => __( 'Testimonials', 'odysseo-core' ),
        'add_new'       => __( 'Add New', 'odysseo-core' ),
        'add_new_item'  => __( 'Add New Testimonial', 'odysseo-core' ),
    );

    register_post_type( 'testimonial', array(
        'labels'        => $labels,
        'public'        => true,
        'has_archive'   => false,
        'menu_icon'     => 'dashicons-format-quote',
        'menu_position' => 8,
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'show_in_rest'  => true,
    ) );
}
add_action( 'init', 'odysseo_register_testimonial_cpt' );
