from odoo import http
from odoo.http import request
import json
import logging

_logger = logging.getLogger(__name__)

class UberWebhookController(http.Controller):

    @http.route('/uber/webhook/delivery', type='json', auth='none', methods=['POST'], csrf=False)
    def uber_delivery_webhook(self, **post):
        """Handle status updates from Uber Direct"""
        data = json.loads(request.httprequest.data)
        _logger.info("Uber Webhook Received: %s", json.dumps(data, indent=2))
        
        uber_delivery_id = data.get('delivery_id')
        status = data.get('status') # e.g., 'picked_up', 'delivered'
        
        if uber_delivery_id:
            order = request.env['pos.order'].sudo().search([('uber_delivery_id', '=', uber_delivery_id)], limit=1)
            if order:
                # Map Uber status to Odoo status
                status_map = {
                    'pickup': 'pickup',
                    'pickup_completed': 'delivering',
                    'dropoff_completed': 'delivered',
                    'cancelled': 'cancelled'
                }
                order.uber_status = status_map.get(status, order.uber_status)
                return {'status': 'success'}
                
        return {'status': 'ignored'}
    


class UberDeliveryController(http.Controller):

    @http.route('/shop/uber/quote', type='json', auth='public', website=True, csrf=False)
    def uber_quote(self, address_data, **post):
        """Get Uber quote for a website address with cleaned address formatting"""
        order = request.website.sale_get_order()
        if not order:
            return {'success': False, 'error': 'No active order'}

        config = request.env['uber.config'].sudo().search([('active', '=', True)], limit=1)
        if not config:
            return {'success': False, 'error': 'Uber not configured'}

        company = request.website.company_id
        
        # Build STRUCTURED pickup address (Object) mapping POS exactly
        pickup_address = {
            "street_address": [company.street or ""],
            "city": company.city or "",
            "state": company.state_id.code if company.state_id else "",
            "zip_code": company.zip or "",
            "country": company.country_id.code or "CA"
        }
        
        # User entered address fields
        street = (address_data.get('street') or '').strip()
        street2 = (address_data.get('street2') or '').strip()
        full_street = f"{street}, {street2}" if street2 else street
        
        state_input = (address_data.get('state') or '').split('(')[0].strip()
        state_record = request.env['res.country.state'].sudo().search([
            ('country_id.code', '=', 'CA'),
            '|', ('name', '=ilike', state_input), ('code', '=ilike', state_input)
        ], limit=1)
        state_code = state_record.code if state_record else "ON"

        # Build STRUCTURED dropoff address (Object)
        dropoff_address = {
            "street_address": [full_street],
            "city": address_data.get('city', '').strip(),
            "state": state_code,
            "zip_code": address_data.get('zip', '').strip(),
            "country": "CA"
        }

        # For logging, still create strings
        p_str = f"{pickup_address['street_address'][0]}, {pickup_address['city']} {pickup_address['state']}"
        d_str = f"{dropoff_address['street_address'][0]}, {dropoff_address['city']} {dropoff_address['state']}"
        _logger.info("WEBSITE UBER QUOTE (STRUCTURED) -\nPickup: [%s]\nDropoff: [%s]", p_str, d_str)

        # POS ENCODING: The POS sends these as JSON-encoded STRINGS
        result = config.get_uber_quote(json.dumps(pickup_address), json.dumps(dropoff_address))
        
        if result.get('success'):
            order.sudo()._add_uber_delivery_fee(result['fee_amount'])
            return {
                'success': True,
                'fee': result['fee_amount'],
                'eta': result.get('estimated_arrival'),
            }
        else:
            _logger.warning("Uber Quote Failed: %s", result.get('error'))
            return result
