<?php
/**
 * Post Types
 */

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

define('ASKAGENTO_POST_TYPE', 'askagento_filter');

/**
 * Register filter post type
 */
function askagento_register_post_type() {
    $labels = [
        'name'               => __('Auto Filter', 'askagento'),
        'singular_name'      => __('Filter', 'askagento'),
        'add_new'            => __('New Auto Filter', 'askagento'),
        'add_new_item'       => __('Auto Filter', 'askagento'),
        'edit_item'          => __('Edit Filter', 'askagento'),
        'new_item'           => __('Auto Filter', 'askagento'),
        'view_item'          => __('View Filter', 'askagento'),
        'search_items'       => __('Search Filters', 'askagento'),
        'not_found'          => __('No filters found', 'askagento'),
        'not_found_in_trash' => __('No filters found in Trash', 'askagento'),
        'all_items'          => __('All Filters', 'askagento'),
        'menu_name'          => __('AskAgento', 'askagento'),
    ];
    
    $args = [
        'labels'             => $labels,
        'public'             => false,
        'publicly_queryable' => false,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'show_in_rest'       => true,
        'query_var'          => false,
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => 30,
        'menu_icon'          => 'dashicons-filter',
        'supports'           => ['title'],
        'rewrite'            => false,
    ];
    
    register_post_type(ASKAGENTO_POST_TYPE, $args);
}

/**
 * Get filter settings
 */
function askagento_get_settings($filter_id) {
    $settings = get_post_meta($filter_id, '_askagento_settings', true);
    
    if (!is_array($settings)) {
        $settings = [];
    }
    
    $defaults = [
        'post_types'       => ['product'],
        'display_on'       => 'archive',
        'results_selector' => '.products',
        'ajax_enabled'     => true,
        'show_count'       => true,
        'hide_empty'       => true,
    ];
    
    return wp_parse_args($settings, $defaults);
}

/**
 * Get filter fields
 */
function askagento_get_fields($filter_id) {
    $fields = get_post_meta($filter_id, '_askagento_fields', true);
    
    if (!is_array($fields)) {
        $fields = [];
    }
    
    return $fields;
}

/**
 * Save filter settings
 */
function askagento_save_settings($filter_id, $settings) {
    update_post_meta($filter_id, '_askagento_settings', $settings);
    if (function_exists('askagento_schedule_filter_cache_rebuild')) {
        askagento_schedule_filter_cache_rebuild((int) $filter_id, true);
    }
}

/**
 * Save filter fields
 */
function askagento_save_fields($filter_id, $fields) {
    update_post_meta($filter_id, '_askagento_fields', $fields);
    if (function_exists('askagento_schedule_filter_cache_rebuild')) {
        askagento_schedule_filter_cache_rebuild((int) $filter_id, true);
    }
}
