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

_logger = logging.getLogger(__name__)


class Dine360OrderChannelsController(http.Controller):

    @http.route('/dine360/order_channels/partners', type='json', auth='user', methods=['POST'])
    def search_partners(self, query='', limit=10):
        """Search for partners (for delivery address lookup from POS)"""
        domain = [('name', 'ilike', query)]
        partners = request.env['res.partner'].search(domain, limit=limit)
        return [{
            'id': p.id,
            'name': p.name,
            'phone': p.phone or p.mobile or '',
            'street': p.street or '',
            'city': p.city or '',
            'zip': p.zip or '',
            'display_name': p.display_name,
        } for p in partners]

    @http.route('/dine360/order_channels/create_partner', type='json', auth='user', methods=['POST'])
    def create_partner(self, name, phone='', street='', city='', zip_code=''):
        """Quick create a delivery partner from POS"""
        partner = request.env['res.partner'].create({
            'name': name,
            'phone': phone,
            'street': street,
            'city': city,
            'zip': zip_code,
            'type': 'delivery',
        })
        return {
            'id': partner.id,
            'name': partner.name,
            'phone': partner.phone or '',
            'street': partner.street or '',
            'city': partner.city or '',
            'zip': partner.zip or '',
        }
