<?php
if (!defined('ABSPATH')) exit;

class VIMAKAVA_GBC_Quotes {

    public function __construct() {
        add_action('init', array($this, 'register_cpt'));
        add_action('admin_menu', array($this, 'adjust_menu'), 99);
        add_filter('manage_vimakava_gbc_quote_posts_columns', array($this, 'set_custom_edit_vimakava_gbc_quote_columns'));
        add_action('manage_vimakava_gbc_quote_posts_custom_column', array($this, 'custom_vimakava_gbc_quote_column'), 10, 2);
    }

    public function register_cpt() {
        $args = array(
            'labels' => array(
                'name' => 'Quotes',
                'singular_name' => 'Quote',
                'search_items' => 'Search Quotes by ID',
                'not_found' => 'No quotes found.',
            ),
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => false, // Do not let WP hijack the menu slug
            'supports' => array('title'),
            'capabilities' => array(
                'create_posts' => 'do_not_allow', 
            ),
            'map_meta_cap' => true,
        );
        register_post_type('vimakava_gbc_quote', $args);
    }

    public function adjust_menu() {
        // Explicitly inject the CPT list under the parent settings menu
        add_submenu_page(
            'vimakava-gbc-settings', 
            'Quotes', 
            'Quotes', 
            'manage_options', 
            'edit.php?post_type=vimakava_gbc_quote'
        );
    }

    public function set_custom_edit_vimakava_gbc_quote_columns($columns) {
        unset($columns['date']); // Remove default date to reposition it
        $columns['vimakava_gbc_email'] = 'Customer Email';
        $columns['vimakava_gbc_weight'] = 'Weight';
        $columns['vimakava_gbc_purity'] = 'Purity';
        $columns['vimakava_gbc_price'] = 'Quoted Price';
        $columns['date'] = 'Date';
        return $columns;
    }

    public function custom_vimakava_gbc_quote_column($column, $post_id) {
        switch ($column) {
            case 'vimakava_gbc_email':
                echo esc_html(get_post_meta($post_id, '_vimakava_gbc_email', true));
                break;
            case 'vimakava_gbc_weight':
                echo esc_html(get_post_meta($post_id, '_vimakava_gbc_weight', true)) . 'g';
                break;
            case 'vimakava_gbc_purity':
                echo esc_html(get_post_meta($post_id, '_vimakava_gbc_karat', true));
                break;
            case 'vimakava_gbc_price':
                echo '<strong>' . esc_html(get_post_meta($post_id, '_vimakava_gbc_price', true)) . '</strong>';
                break;
        }
    }
}

new VIMAKAVA_GBC_Quotes();
