/**
 * Widgets API client
 * @since 1.1.0
 */

import { apiClient } from './client'
import type {
    WidgetFormData,
    WidgetResponse,
    WidgetsListResponse,
    WidgetTypesResponse,
    FormField,
    FieldResponse,
    FieldsReorderResponse,
} from '@/types/widget'

export const widgetsApi = {
    /**
     * Get all widgets
     */
    async getWidgets(page = 1, perPage = 50, type?: string, language?: string): Promise<WidgetsListResponse> {
        const params: Record<string, unknown> = { page, per_page: perPage }
        if (type) params.type = type
        if (language) params.language = language
        return apiClient.get('widgets', params)
    },

    /**
     * Get available widget and field types
     */
    async getWidgetTypes(): Promise<WidgetTypesResponse> {
        return apiClient.get('widgets/types')
    },

    /**
     * Get a single widget by ID
     */
    async getWidget(id: number): Promise<WidgetResponse> {
        return apiClient.get(`widgets/${id}`)
    },

    /**
     * Create a new widget
     */
    async createWidget(data: WidgetFormData): Promise<WidgetResponse> {
        return apiClient.post('widgets', data)
    },

    /**
     * Update an existing widget
     */
    async updateWidget(id: number, data: Partial<WidgetFormData>): Promise<WidgetResponse> {
        return apiClient.put(`widgets/${id}`, data)
    },

    /**
     * Delete a widget
     */
    async deleteWidget(id: number): Promise<{ success: boolean; data: { deleted: boolean } }> {
        return apiClient.delete(`widgets/${id}`)
    },

    /**
     * Bulk delete multiple widgets
     */
    async bulkDeleteWidgets(ids: number[]): Promise<{ 
        success: boolean; 
        data: { 
            deleted_count: number; 
            failed_count: number; 
            failed_ids: number[] 
        } 
    }> {
        return apiClient.delete('widgets/bulk', { ids })
    },

    // Form field operations

    /**
     * Add a field to a form widget
     */
    async addField(widgetId: number, field: Partial<FormField>): Promise<FieldResponse> {
        return apiClient.post(`widgets/${widgetId}/fields`, field)
    },

    /**
     * Update a field
     */
    async updateField(widgetId: number, fieldId: number, field: Partial<FormField>): Promise<FieldResponse> {
        return apiClient.put(`widgets/${widgetId}/fields/${fieldId}`, field)
    },

    /**
     * Delete a field
     */
    async deleteField(widgetId: number, fieldId: number): Promise<{ success: boolean; data: { deleted: boolean } }> {
        return apiClient.delete(`widgets/${widgetId}/fields/${fieldId}`)
    },

    /**
     * Reorder fields
     */
    async reorderFields(widgetId: number, fieldIds: number[]): Promise<FieldsReorderResponse> {
        return apiClient.post(`widgets/${widgetId}/reorder-fields`, { field_ids: fieldIds })
    },

    // Chatbot assignment operations

    /**
     * Assign a widget to a chatbot
     */
    async assignToChatbot(
        widgetId: number, 
        chatbotId: number, 
        customConfig?: Record<string, unknown>
    ): Promise<{ success: boolean; data: { assigned: boolean } }> {
        return apiClient.post(`widgets/${widgetId}/assign/${chatbotId}`, { custom_config: customConfig })
    },

    /**
     * Unassign a widget from a chatbot
     */
    async unassignFromChatbot(
        widgetId: number, 
        chatbotId: number
    ): Promise<{ success: boolean; data: { unassigned: boolean } }> {
        return apiClient.delete(`widgets/${widgetId}/assign/${chatbotId}`)
    },

    /**
     * Get widgets for a chatbot
     */
    async getWidgetsForChatbot(chatbotId: number): Promise<WidgetsListResponse> {
        if (!chatbotId || isNaN(chatbotId) || chatbotId <= 0) {
            return { success: true, data: { widgets: [], total: 0, page: 1, per_page: 50 } } as WidgetsListResponse
        }
        return apiClient.get(`widgets/for-chatbot/${chatbotId}`)
    },
}

export default widgetsApi

