/** @odoo-module */

import { Component, useState } from "@odoo/owl";
import { usePos } from "@point_of_sale/app/store/pos_hook";
import { jsonrpc } from "@web/core/network/rpc_service";

/**
 * ChannelPanel - shown in the POS OrderScreen.
 * Allows staff to select:
 *   - Order Source (Walk-in / Phone / WhatsApp / Social / Online)
 *   - Fulfilment Type (Dine-In / Pickup / Delivery)
 *   - Delivery address (when Delivery is selected)
 */
export class ChannelPanel extends Component {
    static template = "dine360_order_channels.ChannelPanel";

    setup() {
        this.pos = usePos();

        this.SOURCE_LABELS = {
            walk_in: 'Walk-In',
            phone: 'Phone',
            whatsapp: 'WhatsApp',
            social_media: 'Social',
            online: 'Online',
            kiosk: 'Kiosk',
            qr: 'QR Code',
            platform: 'Platform',
        };

        this.FULFILMENT_LABELS = {
            dine_in: 'Dine-In',
            pickup: 'Pickup',
            delivery: 'Delivery',
        };

        this.state = useState({
            showDelivery: false,
            showDetails: false, // For manual fields toggle
            isCollapsed: false, // Panel collapse state
            searchQuery: '',
            searchResults: [],
            searching: false,
        });
    }

    toggleDetails() {
        this.state.showDetails = !this.state.showDetails;
    }

    get currentOrder() {
        return this.pos.get_order();
    }

    get orderSource() {
        return this.currentOrder?.order_source || 'walk_in';
    }

    get fulfilmentType() {
        return this.currentOrder?.fulfilment_type || 'dine_in';
    }

    get isDelivery() {
        return this.fulfilmentType === 'delivery';
    }

    get showPanel() {
        return this.pos?.config?.show_channel_panel !== false;
    }

    // --- Source Selector ---
    onSourceChange(source) {
        const order = this.currentOrder;
        if (!order) return;
        order.setOrderSource(source);
        // Trigger re-render
        this.state.showDelivery = this.isDelivery;
    }

    // --- Fulfilment Selector ---
    onFulfilmentChange(type) {
        const order = this.currentOrder;
        if (!order) return;
        order.setFulfilmentType(type);
        this.state.showDelivery = (type === 'delivery');
    }

    // --- Partner Address Search ---
    async onAddressSearch(ev) {
        const query = ev.target.value;
        this.state.searchQuery = query;
        if (query.length < 2) {
            this.state.searchResults = [];
            return;
        }
        this.state.searching = true;
        const results = await jsonrpc('/dine360/order_channels/partners', { query, limit: 8 });
        this.state.searchResults = results;
        this.state.searching = false;
    }

    onSelectPartner(partner) {
        const order = this.currentOrder;
        if (!order) return;
        order.setDeliveryAddress({
            street: partner.street,
            city: partner.city,
            zip: partner.zip,
            phone: partner.phone,
        });
        this.state.searchQuery = partner.display_name;
        this.state.searchResults = [];
    }

    onDeliveryFieldChange(field, ev) {
        const order = this.currentOrder;
        if (!order) return;
        // Ad-hoc street/city/zip/phone/notes editing
        const current = {
            street: order.delivery_street,
            city: order.delivery_city,
            zip: order.delivery_zip,
            phone: order.delivery_phone,
            notes: order.delivery_notes,
        };
        current[field] = ev.target.value;
        order.setDeliveryAddress(current);
    }
}
