<?php
/**
 * ACF Field Groups
 *
 * Registers custom fields for properties using ACF
 *
 * @package HomeProz
 */

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

/**
 * Register Property Details field group
 */
function homeproz_register_acf_fields() {
    if (!function_exists('acf_add_local_field_group')) {
        return;
    }

    // Property Details Field Group
    acf_add_local_field_group(array(
        'key' => 'group_property_details',
        'title' => 'Property Details',
        'fields' => array(
            // Pricing Tab
            array(
                'key' => 'field_property_tab_pricing',
                'label' => 'Pricing & Status',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_price',
                'label' => 'Price',
                'name' => 'property_price',
                'type' => 'number',
                'instructions' => 'Enter the listing price (numbers only, no commas or $)',
                'required' => 1,
                'prepend' => '$',
                'min' => 0,
            ),
            array(
                'key' => 'field_property_mls_number',
                'label' => 'MLS Number',
                'name' => 'mls_number',
                'type' => 'text',
                'instructions' => 'Enter the MLS listing number',
            ),

            // Address Tab
            array(
                'key' => 'field_property_tab_address',
                'label' => 'Address',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_street_address',
                'label' => 'Street Address',
                'name' => 'street_address',
                'type' => 'text',
                'required' => 1,
            ),
            array(
                'key' => 'field_property_city',
                'label' => 'City',
                'name' => 'city',
                'type' => 'text',
                'required' => 1,
                'default_value' => 'Albert Lea',
            ),
            array(
                'key' => 'field_property_state',
                'label' => 'State',
                'name' => 'state',
                'type' => 'text',
                'required' => 1,
                'default_value' => 'MN',
            ),
            array(
                'key' => 'field_property_zip',
                'label' => 'ZIP Code',
                'name' => 'zip_code',
                'type' => 'text',
                'required' => 1,
            ),

            // Property Details Tab
            array(
                'key' => 'field_property_tab_details',
                'label' => 'Property Details',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_bedrooms',
                'label' => 'Bedrooms',
                'name' => 'bedrooms',
                'type' => 'number',
                'min' => 0,
                'max' => 20,
            ),
            array(
                'key' => 'field_property_bathrooms',
                'label' => 'Bathrooms',
                'name' => 'bathrooms',
                'type' => 'number',
                'min' => 0,
                'max' => 20,
                'step' => 0.5,
            ),
            array(
                'key' => 'field_property_square_feet',
                'label' => 'Square Feet',
                'name' => 'square_feet',
                'type' => 'number',
                'min' => 0,
            ),
            array(
                'key' => 'field_property_lot_size',
                'label' => 'Lot Size',
                'name' => 'lot_size',
                'type' => 'text',
                'instructions' => 'e.g., "0.25 acres" or "10,890 sq ft"',
            ),
            array(
                'key' => 'field_property_year_built',
                'label' => 'Year Built',
                'name' => 'year_built',
                'type' => 'number',
                'min' => 1800,
                'max' => 2100,
            ),
            array(
                'key' => 'field_property_garage',
                'label' => 'Garage',
                'name' => 'garage',
                'type' => 'select',
                'choices' => array(
                    '' => 'None',
                    '1' => '1 Car',
                    '2' => '2 Car',
                    '3' => '3 Car',
                    '4+' => '4+ Car',
                ),
            ),

            // Features Tab
            array(
                'key' => 'field_property_tab_features',
                'label' => 'Features',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_short_description',
                'label' => 'Short Description',
                'name' => 'short_description',
                'type' => 'textarea',
                'instructions' => 'Brief description for property cards (1-2 sentences)',
                'rows' => 3,
                'maxlength' => 250,
            ),
            array(
                'key' => 'field_property_features',
                'label' => 'Property Features',
                'name' => 'property_features',
                'type' => 'checkbox',
                'instructions' => 'Select all features that apply',
                'choices' => array(
                    'central_air' => 'Central Air',
                    'central_heat' => 'Central Heat',
                    'fireplace' => 'Fireplace',
                    'hardwood_floors' => 'Hardwood Floors',
                    'updated_kitchen' => 'Updated Kitchen',
                    'updated_bathrooms' => 'Updated Bathrooms',
                    'basement' => 'Basement',
                    'finished_basement' => 'Finished Basement',
                    'deck_patio' => 'Deck/Patio',
                    'pool' => 'Pool',
                    'fenced_yard' => 'Fenced Yard',
                    'sprinkler_system' => 'Sprinkler System',
                    'smart_home' => 'Smart Home Features',
                    'solar_panels' => 'Solar Panels',
                    'new_roof' => 'New Roof',
                    'new_windows' => 'New Windows',
                    'waterfront' => 'Waterfront',
                    'lake_access' => 'Lake Access',
                ),
                'layout' => 'horizontal',
            ),

            // Gallery Tab - Using relationship to media for free ACF
            array(
                'key' => 'field_property_tab_gallery',
                'label' => 'Photo Gallery',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_gallery',
                'label' => 'Property Gallery',
                'name' => 'property_gallery',
                'type' => 'relationship',
                'instructions' => 'Select images from the media library. First image will be the main photo if no featured image is set.',
                'post_type' => array('attachment'),
                'post_status' => array('inherit'),
                'filters' => array('search'),
                'min' => 0,
                'max' => 30,
                'return_format' => 'id',
                'mime_types' => 'jpg, jpeg, png, webp',
            ),

            // Agent Tab
            array(
                'key' => 'field_property_tab_agent',
                'label' => 'Listing Agent',
                'name' => '',
                'type' => 'tab',
                'placement' => 'top',
            ),
            array(
                'key' => 'field_property_listing_agent',
                'label' => 'Listing Agent',
                'name' => 'listing_agent',
                'type' => 'user',
                'instructions' => 'Select the listing agent for this property',
                'role' => '',
                'allow_null' => 1,
                'multiple' => 0,
                'return_format' => 'array',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'property',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'active' => true,
    ));

    // Theme Options Field Group (for global settings)
    acf_add_local_field_group(array(
        'key' => 'group_theme_options',
        'title' => 'Theme Options',
        'fields' => array(
            // Contact Information
            array(
                'key' => 'field_theme_tab_contact',
                'label' => 'Contact Information',
                'name' => '',
                'type' => 'tab',
                'placement' => 'left',
            ),
            array(
                'key' => 'field_theme_phone',
                'label' => 'Phone Number',
                'name' => 'theme_phone',
                'type' => 'text',
                'default_value' => '507-516-4870',
            ),
            array(
                'key' => 'field_theme_email',
                'label' => 'Email Address',
                'name' => 'theme_email',
                'type' => 'email',
                'default_value' => 'info@homeprozrealestate.com',
            ),
            array(
                'key' => 'field_theme_address',
                'label' => 'Office Address',
                'name' => 'theme_address',
                'type' => 'textarea',
                'rows' => 2,
                'default_value' => '111 E Clark St, Albert Lea, MN 56007',
            ),

            // Social Media
            array(
                'key' => 'field_theme_tab_social',
                'label' => 'Social Media',
                'name' => '',
                'type' => 'tab',
                'placement' => 'left',
            ),
            array(
                'key' => 'field_theme_facebook',
                'label' => 'Facebook URL',
                'name' => 'theme_facebook',
                'type' => 'url',
            ),
            array(
                'key' => 'field_theme_tiktok',
                'label' => 'TikTok URL',
                'name' => 'theme_tiktok',
                'type' => 'url',
            ),
            array(
                'key' => 'field_theme_instagram',
                'label' => 'Instagram URL',
                'name' => 'theme_instagram',
                'type' => 'url',
            ),
            array(
                'key' => 'field_theme_youtube',
                'label' => 'YouTube URL',
                'name' => 'theme_youtube',
                'type' => 'url',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'options_page',
                    'operator' => '==',
                    'value' => 'theme-options',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'active' => true,
    ));
}
add_action('acf/init', 'homeproz_register_acf_fields');

/**
 * Register ACF Options Page
 */
function homeproz_register_acf_options_page() {
    if (!function_exists('acf_add_options_page')) {
        return;
    }

    acf_add_options_page(array(
        'page_title'    => 'Theme Options',
        'menu_title'    => 'Theme Options',
        'menu_slug'     => 'theme-options',
        'capability'    => 'edit_posts',
        'redirect'      => false,
        'icon_url'      => 'dashicons-admin-customizer',
        'position'      => 80,
    ));
}
add_action('acf/init', 'homeproz_register_acf_options_page');

/**
 * Update homeproz_get_option to use ACF if available
 */
function homeproz_get_acf_option($key, $default = '') {
    // ACF field mapping
    $field_map = array(
        'phone' => 'theme_phone',
        'email' => 'theme_email',
        'address' => 'theme_address',
        'facebook' => 'theme_facebook',
        'tiktok' => 'theme_tiktok',
        'instagram' => 'theme_instagram',
        'youtube' => 'theme_youtube',
    );

    // If ACF is active and we have an options page
    if (function_exists('get_field') && isset($field_map[$key])) {
        $value = get_field($field_map[$key], 'option');
        if ($value) {
            return $value;
        }
    }

    // Fallback defaults
    $defaults = array(
        'phone' => '507-516-4870',
        'email' => 'info@homeprozrealestate.com',
        'address' => '111 E Clark St, Albert Lea, MN 56007',
        'facebook' => 'https://www.facebook.com/profile.php?id=61578834743321',
        'tiktok' => 'https://www.tiktok.com/@homeproz.real.est',
    );

    return isset($defaults[$key]) ? $defaults[$key] : $default;
}
