<?php
namespace WooNooW\Frontend;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

class AddressController {
    
    /**
     * Register REST API routes
     */
    public static function register_routes() {
        $namespace = 'woonoow/v1';
        
        // Register GET and POST together to avoid route conflicts
        register_rest_route($namespace, '/account/addresses', [
            [
                'methods'             => 'GET',
                'callback'            => [__CLASS__, 'get_addresses'],
                'permission_callback' => [__CLASS__, 'check_customer_permission'],
            ],
            [
                'methods'             => 'POST',
                'callback'            => [__CLASS__, 'create_address'],
                'permission_callback' => [__CLASS__, 'check_customer_permission'],
            ],
        ]);
        
        // Update address
        register_rest_route($namespace, '/account/addresses/(?P<id>\d+)', [
            'methods'             => 'PUT',
            'callback'            => [__CLASS__, 'update_address'],
            'permission_callback' => [__CLASS__, 'check_customer_permission'],
        ]);
        
        // Delete address
        register_rest_route($namespace, '/account/addresses/(?P<id>\d+)', [
            'methods'             => 'DELETE',
            'callback'            => [__CLASS__, 'delete_address'],
            'permission_callback' => [__CLASS__, 'check_customer_permission'],
        ]);
        
        // Set default address
        register_rest_route($namespace, '/account/addresses/(?P<id>\d+)/set-default', [
            'methods'             => 'POST',
            'callback'            => [__CLASS__, 'set_default_address'],
            'permission_callback' => [__CLASS__, 'check_customer_permission'],
        ]);
    }
    
    /**
     * Check if user is logged in
     */
    public static function check_customer_permission() {
        return is_user_logged_in();
    }
    
    /**
     * Get all addresses for current user
     */
    public static function get_addresses(WP_REST_Request $request) {
        $user_id = get_current_user_id();
        $addresses = get_user_meta($user_id, 'woonoow_addresses', true);
        
        if (!$addresses || !is_array($addresses)) {
            $addresses = [];
        }
        
        $addresses = array_values($addresses);
        
        return new WP_REST_Response($addresses, 200);
    }
    
    /**
     * Create new address
     */
    public static function create_address(WP_REST_Request $request) {
        $user_id = get_current_user_id();
        $addresses = get_user_meta($user_id, 'woonoow_addresses', true);
        
        if (!is_array($addresses)) {
            $addresses = [];
        }
        
        // Generate new ID
        $new_id = empty($addresses) ? 1 : max(array_column($addresses, 'id')) + 1;
        
        // Standard address fields
        $standard_fields = ['first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'email', 'phone'];
        $reserved_fields = ['id', 'label', 'type', 'is_default'];
        
        // Prepare address data with standard fields
        $address = [
            'id'           => $new_id,
            'label'        => sanitize_text_field($request->get_param('label')),
            'type'         => sanitize_text_field($request->get_param('type')), // 'billing', 'shipping', or 'both'
            'is_default'   => (bool) $request->get_param('is_default'),
        ];
        
        // Add standard fields
        foreach ($standard_fields as $field) {
            $value = $request->get_param($field);
            if ($field === 'email') {
                $address[$field] = sanitize_email($value);
            } else {
                $address[$field] = sanitize_text_field($value);
            }
        }
        
        // Add any custom fields (like destination_id from Rajaongkir)
        $all_params = $request->get_json_params();
        if (is_array($all_params)) {
            foreach ($all_params as $key => $value) {
                if (!in_array($key, $standard_fields) && !in_array($key, $reserved_fields)) {
                    // Store custom field
                    $address[$key] = is_string($value) ? sanitize_text_field($value) : $value;
                }
            }
        }
        
        // If this is set as default, unset other defaults of the same type
        if ($address['is_default']) {
            foreach ($addresses as &$addr) {
                if ($addr['type'] === $address['type'] || $addr['type'] === 'both' || $address['type'] === 'both') {
                    $addr['is_default'] = false;
                }
            }
        }
        
        $addresses[] = $address;
        
        update_user_meta($user_id, 'woonoow_addresses', $addresses);
        
        return new WP_REST_Response($address, 201);
    }
    
    /**
     * Update existing address
     */
    public static function update_address(WP_REST_Request $request) {
        $user_id = get_current_user_id();
        $address_id = (int) $request->get_param('id');
        $addresses = get_user_meta($user_id, 'woonoow_addresses', true);
        
        if (!is_array($addresses)) {
            return new WP_Error('no_addresses', 'No addresses found', ['status' => 404]);
        }
        
        $found = false;
        foreach ($addresses as &$addr) {
            if ($addr['id'] === $address_id) {
                $found = true;
                
                // Standard address fields
                $standard_fields = ['first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'email', 'phone'];
                $reserved_fields = ['id', 'label', 'type', 'is_default'];
                
                // Update standard meta fields
                $addr['label'] = sanitize_text_field($request->get_param('label'));
                $addr['type']  = sanitize_text_field($request->get_param('type'));
                $addr['is_default'] = (bool) $request->get_param('is_default');
                
                // Update standard fields
                foreach ($standard_fields as $field) {
                    $value = $request->get_param($field);
                    if ($field === 'email') {
                        $addr[$field] = sanitize_email($value);
                    } else {
                        $addr[$field] = sanitize_text_field($value);
                    }
                }
                
                // Update any custom fields (like destination_id from Rajaongkir)
                $all_params = $request->get_json_params();
                if (is_array($all_params)) {
                    foreach ($all_params as $key => $value) {
                        if (!in_array($key, $standard_fields) && !in_array($key, $reserved_fields)) {
                            // Store/update custom field
                            $addr[$key] = is_string($value) ? sanitize_text_field($value) : $value;
                        }
                    }
                }
                
                // If this is set as default, unset other defaults of the same type
                if ($addr['is_default']) {
                    foreach ($addresses as &$other_addr) {
                        if ($other_addr['id'] !== $address_id) {
                            if ($other_addr['type'] === $addr['type'] || $other_addr['type'] === 'both' || $addr['type'] === 'both') {
                                $other_addr['is_default'] = false;
                            }
                        }
                    }
                }
                
                break;
            }
        }
        
        if (!$found) {
            return new WP_Error('address_not_found', 'Address not found', ['status' => 404]);
        }
        
        update_user_meta($user_id, 'woonoow_addresses', $addresses);
        
        return new WP_REST_Response(['success' => true], 200);
    }
    
    /**
     * Delete address
     */
    public static function delete_address(WP_REST_Request $request) {
        $user_id = get_current_user_id();
        $address_id = (int) $request->get_param('id');
        $addresses = get_user_meta($user_id, 'woonoow_addresses', true);
        
        if (!is_array($addresses)) {
            return new WP_Error('no_addresses', 'No addresses found', ['status' => 404]);
        }
        
        $addresses = array_filter($addresses, function($addr) use ($address_id) {
            return $addr['id'] !== $address_id;
        });
        
        // Re-index array
        $addresses = array_values($addresses);
        
        update_user_meta($user_id, 'woonoow_addresses', $addresses);
        
        return new WP_REST_Response(['success' => true], 200);
    }
    
    /**
     * Set address as default
     */
    public static function set_default_address(WP_REST_Request $request) {
        $user_id = get_current_user_id();
        $address_id = (int) $request->get_param('id');
        $addresses = get_user_meta($user_id, 'woonoow_addresses', true);
        
        if (!is_array($addresses)) {
            return new WP_Error('no_addresses', 'No addresses found', ['status' => 404]);
        }
        
        $found = false;
        $address_type = null;
        
        foreach ($addresses as &$addr) {
            if ($addr['id'] === $address_id) {
                $found = true;
                $address_type = $addr['type'];
                $addr['is_default'] = true;
            } else {
                // Unset default for addresses of the same type
                if ($address_type && ($addr['type'] === $address_type || $addr['type'] === 'both' || $address_type === 'both')) {
                    $addr['is_default'] = false;
                }
            }
        }
        
        if (!$found) {
            return new WP_Error('address_not_found', 'Address not found', ['status' => 404]);
        }
        
        update_user_meta($user_id, 'woonoow_addresses', $addresses);
        
        return new WP_REST_Response(['success' => true], 200);
    }
}
