<?php
/**
 * Custom Post Types
 *
 * @package HomeProz
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register Property Custom Post Type
 */
function homeproz_register_property_cpt() {
    $labels = array(
        'name'                  => _x('Properties', 'Post type general name', 'homeproz'),
        'singular_name'         => _x('Property', 'Post type singular name', 'homeproz'),
        'menu_name'             => _x('Properties', 'Admin Menu text', 'homeproz'),
        'name_admin_bar'        => _x('Property', 'Add New on Toolbar', 'homeproz'),
        'add_new'               => __('Add New', 'homeproz'),
        'add_new_item'          => __('Add New Property', 'homeproz'),
        'new_item'              => __('New Property', 'homeproz'),
        'edit_item'             => __('Edit Property', 'homeproz'),
        'view_item'             => __('View Property', 'homeproz'),
        'all_items'             => __('All Properties', 'homeproz'),
        'search_items'          => __('Search Properties', 'homeproz'),
        'parent_item_colon'     => __('Parent Properties:', 'homeproz'),
        'not_found'             => __('No properties found.', 'homeproz'),
        'not_found_in_trash'    => __('No properties found in Trash.', 'homeproz'),
        'featured_image'        => _x('Property Featured Image', 'Overrides the "Featured Image" phrase', 'homeproz'),
        'set_featured_image'    => _x('Set featured image', 'Overrides the "Set featured image" phrase', 'homeproz'),
        'remove_featured_image' => _x('Remove featured image', 'Overrides the "Remove featured image" phrase', 'homeproz'),
        'use_featured_image'    => _x('Use as featured image', 'Overrides the "Use as featured image" phrase', 'homeproz'),
        'archives'              => _x('Property archives', 'The post type archive label used in nav menus', 'homeproz'),
        'insert_into_item'      => _x('Insert into property', 'Overrides the "Insert into post" phrase', 'homeproz'),
        'uploaded_to_this_item' => _x('Uploaded to this property', 'Overrides the "Uploaded to this post" phrase', 'homeproz'),
        'filter_items_list'     => _x('Filter properties list', 'Screen reader text', 'homeproz'),
        'items_list_navigation' => _x('Properties list navigation', 'Screen reader text', 'homeproz'),
        'items_list'            => _x('Properties list', 'Screen reader text', 'homeproz'),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'properties', 'with_front' => false),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'menu_icon'          => 'dashicons-building',
        'supports'           => array('title', 'editor', 'thumbnail', 'excerpt'),
        'show_in_rest'       => true,
    );

    register_post_type('property', $args);
}
add_action('init', 'homeproz_register_property_cpt');

/**
 * Register Property Taxonomies
 */
function homeproz_register_property_taxonomies() {
    // Property Type (Residential, Commercial, Land, Multi-Family)
    $type_labels = array(
        'name'                       => _x('Property Types', 'Taxonomy general name', 'homeproz'),
        'singular_name'              => _x('Property Type', 'Taxonomy singular name', 'homeproz'),
        'search_items'               => __('Search Property Types', 'homeproz'),
        'popular_items'              => __('Popular Property Types', 'homeproz'),
        'all_items'                  => __('All Property Types', 'homeproz'),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __('Edit Property Type', 'homeproz'),
        'update_item'                => __('Update Property Type', 'homeproz'),
        'add_new_item'               => __('Add New Property Type', 'homeproz'),
        'new_item_name'              => __('New Property Type Name', 'homeproz'),
        'separate_items_with_commas' => __('Separate property types with commas', 'homeproz'),
        'add_or_remove_items'        => __('Add or remove property types', 'homeproz'),
        'choose_from_most_used'      => __('Choose from the most used property types', 'homeproz'),
        'not_found'                  => __('No property types found.', 'homeproz'),
        'menu_name'                  => __('Property Types', 'homeproz'),
    );

    register_taxonomy('property_type', array('property'), array(
        'hierarchical'          => true,
        'labels'                => $type_labels,
        'show_ui'               => true,
        'show_in_rest'          => true,
        'show_admin_column'     => true,
        'query_var'             => true,
        'rewrite'               => array('slug' => 'property-type'),
    ));

    // Property Status (Active, Pending, Sold)
    $status_labels = array(
        'name'                       => _x('Property Status', 'Taxonomy general name', 'homeproz'),
        'singular_name'              => _x('Property Status', 'Taxonomy singular name', 'homeproz'),
        'search_items'               => __('Search Property Statuses', 'homeproz'),
        'popular_items'              => __('Popular Property Statuses', 'homeproz'),
        'all_items'                  => __('All Property Statuses', 'homeproz'),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __('Edit Property Status', 'homeproz'),
        'update_item'                => __('Update Property Status', 'homeproz'),
        'add_new_item'               => __('Add New Property Status', 'homeproz'),
        'new_item_name'              => __('New Property Status Name', 'homeproz'),
        'separate_items_with_commas' => __('Separate statuses with commas', 'homeproz'),
        'add_or_remove_items'        => __('Add or remove statuses', 'homeproz'),
        'choose_from_most_used'      => __('Choose from the most used statuses', 'homeproz'),
        'not_found'                  => __('No statuses found.', 'homeproz'),
        'menu_name'                  => __('Status', 'homeproz'),
    );

    register_taxonomy('property_status', array('property'), array(
        'hierarchical'          => true,
        'labels'                => $status_labels,
        'show_ui'               => true,
        'show_in_rest'          => true,
        'show_admin_column'     => true,
        'query_var'             => true,
        'rewrite'               => array('slug' => 'property-status'),
    ));

    // Property Location (Cities/Areas)
    $location_labels = array(
        'name'                       => _x('Locations', 'Taxonomy general name', 'homeproz'),
        'singular_name'              => _x('Location', 'Taxonomy singular name', 'homeproz'),
        'search_items'               => __('Search Locations', 'homeproz'),
        'popular_items'              => __('Popular Locations', 'homeproz'),
        'all_items'                  => __('All Locations', 'homeproz'),
        'parent_item'                => __('Parent Location', 'homeproz'),
        'parent_item_colon'          => __('Parent Location:', 'homeproz'),
        'edit_item'                  => __('Edit Location', 'homeproz'),
        'update_item'                => __('Update Location', 'homeproz'),
        'add_new_item'               => __('Add New Location', 'homeproz'),
        'new_item_name'              => __('New Location Name', 'homeproz'),
        'separate_items_with_commas' => __('Separate locations with commas', 'homeproz'),
        'add_or_remove_items'        => __('Add or remove locations', 'homeproz'),
        'choose_from_most_used'      => __('Choose from the most used locations', 'homeproz'),
        'not_found'                  => __('No locations found.', 'homeproz'),
        'menu_name'                  => __('Locations', 'homeproz'),
    );

    register_taxonomy('property_location', array('property'), array(
        'hierarchical'          => true,
        'labels'                => $location_labels,
        'show_ui'               => true,
        'show_in_rest'          => true,
        'show_admin_column'     => true,
        'query_var'             => true,
        'rewrite'               => array('slug' => 'location'),
    ));
}
add_action('init', 'homeproz_register_property_taxonomies');

/**
 * Add default taxonomy terms on theme activation
 */
function homeproz_add_default_terms() {
    // Default Property Types
    $property_types = array('Residential', 'Commercial', 'Land', 'Multi-Family');
    foreach ($property_types as $type) {
        if (!term_exists($type, 'property_type')) {
            wp_insert_term($type, 'property_type');
        }
    }

    // Default Property Statuses
    $property_statuses = array('Active', 'Pending', 'Sold');
    foreach ($property_statuses as $status) {
        if (!term_exists($status, 'property_status')) {
            wp_insert_term($status, 'property_status');
        }
    }

    // Default Locations (Minnesota cities near Albert Lea)
    $locations = array('Albert Lea', 'Austin', 'Owatonna', 'Faribault', 'Mankato', 'Rochester');
    foreach ($locations as $location) {
        if (!term_exists($location, 'property_location')) {
            wp_insert_term($location, 'property_location');
        }
    }
}
add_action('after_switch_theme', 'homeproz_add_default_terms');

/**
 * Flush rewrite rules on theme activation
 */
function homeproz_rewrite_flush() {
    homeproz_register_property_cpt();
    homeproz_register_property_taxonomies();
    flush_rewrite_rules();
}
add_action('after_switch_theme', 'homeproz_rewrite_flush');
